2
votes

im trying to demo lucene numeric range query and cant get it to work properly. here's is what im trying to do:

public static void main( String[] args ) throws Exception{
    Path indexDir = Files.createTempDirectory("index");
    Directory directory = FSDirectory.open(indexDir.toFile());
    Analyzer analyzer = new EnglishAnalyzer(Version.LUCENE_36);
    IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
    writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(directory, writerConfig);
    for (int i=0; i<100; i++) {
        Document doc = new Document();
        for (int j = 0; j<10; j++) {
            NumericField field = new NumericField("numericField", Field.Store.YES, true);
            field.setDoubleValue(Math.random());
            doc.add(field);
        }
        writer.addDocument(doc);
    }
    writer.close(true);
    directory.close(); //just to be safe

    analyzer = new EnglishAnalyzer(Version.LUCENE_36);
    directory = FSDirectory.open(indexDir.toFile());
    IndexReader reader = IndexReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_36, new String[] {"someField", "someOtherField"}, analyzer);
    Query q = parser.parse("numericField:[0 TO 0.5]");
    TopDocs results = searcher.search(q, 100);
    System.out.println("got "+results.scoreDocs.length+" results");
}

basically im creating a new FS directory, indexing 100 documents, each document with 10 numeric fields with random values, under the same name (lucene docs say this is allowed?) i then try and search this newly-created index with a range query.

i'd expect to get almost 100 hits every time, yet it comes out 0 every time.

i'm obviously doing something wrong, but i have no idea what. any clues/ideas would be very welcome.

im using java 7 and lucene 3.6.1. this code needs the lucene core and lucene analyzers artifacts to compile

1

1 Answers

2
votes

From NumericField: "To perform range querying or filtering against a NumericField, use NumericRangeQuery or NumericRangeFilter." The query parser doesn't know a priori that this is a numeric field, so its output is string-based:

<TermRangeQuery: numericField:[0 TO 0.5]>

instead of:

<NumericRangeQuery: numericField:[0.0 TO 0.5]>

It's generally recommended to construct queries programmatically whenever possible, to avoid parsing problems like this.