Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Not A Problem
-
4.10.2
-
Windows 7, Java 1.7.0_55
-
New
Description
Hi Lucene Team,
The below code creates an index with three documents, each document has one BirthDate field. Searching does not return expected results.
Regards
Slav
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class LuceneTest {
private static final java.text.SimpleDateFormat DATE_PARSER = new java.text.SimpleDateFormat("yyyy/MM/dd");
private static final String INDEX_PATH = "/tmp/lucene";
private static Date parseDate(String dateSt) {
synchronized(DATE_PARSER) {
try
catch (java.text.ParseException e)
{ throw new IllegalArgumentException(e); } }
}
public static void main(String[] args) {
//Create an index
try {
Directory dir = FSDirectory.open(new File(INDEX_PATH));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_2, analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new LongField("BirthDate", parseDate("1989/11/01").getTime(), Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new LongField("BirthDate", parseDate("1973/03/02").getTime(), Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new LongField("BirthDate", parseDate("1969/01/31").getTime(), Field.Store.YES));
writer.addDocument(doc);
writer.close();
//Now do searching
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(INDEX_PATH)));
IndexSearcher searcher = new IndexSearcher(reader);
Query query = NumericRangeQuery.newLongRange("BirthDate", 4, parseDate("1969/01/30").getTime(),
parseDate("1973/03/03").getTime(), true, true);
TopDocs results = searcher.search(query, null, 100);
ScoreDoc[] scoreDocs = results.scoreDocs;
int hits = scoreDocs.length;
int count = results.totalHits;
for(int i = 0; i < hits; i++)
{ doc = searcher.doc(scoreDocs[i].doc); String value = doc.get("BirthDate"); System.out.println(new Date(Long.parseLong(value))); }} catch (Exception e)
{ e.printStackTrace(); } }
}