Lucene LongPoint Range search doesn’t work

I am using Lucene 8.2.0 in Java 11.

I am trying to index a Long value so that I can filter by it using a range query, for example like so: +my_range_field:[1 TO 200]. However, any variant of that, even my_range_field:[* TO *], returns 0 results in this minimal example. As soon as I remove the + from it to make it an OR, I get 2 results.

So I am thinking I must make a mistake in how I index it, but I can’t make out what it might be.

From the LongPoint JavaDoc:

An indexed long field for fast range filters. If you also need to store the value, you should add a separate StoredField instance. Finding all documents within an N-dimensional shape or range at search time is efficient. Multiple values for the same field in one document is allowed.

This is my minimal example:

public static void main(String[] args) {
    Directory index = new RAMDirectory();
    StandardAnalyzer analyzer = new StandardAnalyzer();

    try {
        IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(analyzer));

        Document document1= new Document();
        Document document2= new Document();

        document1.add(new LongPoint("my_range_field", 10));
        document1.add(new StoredField("my_range_field", 10));
        document2.add(new LongPoint("my_range_field", 100));
        document2.add(new StoredField("my_range_field", 100));

        document1.add(new TextField("my_text_field", "test content 1", Field.Store.YES));
        document2.add(new TextField("my_text_field", "test content 2", Field.Store.YES));

        indexWriter.deleteAll();
        indexWriter.commit();
        indexWriter.addDocument(document1);
        indexWriter.addDocument(document2);
        indexWriter.commit();
        indexWriter.close();

        QueryParser parser = new QueryParser("text", analyzer);
        IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(index));

        String luceneQuery = "+my_text_field:test* +my_range_field:[1 TO 200]";
        Query query = parser.parse(luceneQuery);

        System.out.println(indexSearcher.search(query, 10).totalHits.value);
    } catch (IOException e) {

    } catch (ParseException e) {

    }
}

Answer

I found the solution to my problem.

I was under the impression that the query parser could just parse any query string correctly. That doesn’t seem to be the case.

Using

Query rangeQuery = LongPoint.newRangeQuery("my_range_field", 1L, 11L);
Query searchQuery = new WildcardQuery(new Term("my_text_field", "test*"));
Query build = new BooleanQuery.Builder()
    .add(searchQuery, BooleanClause.Occur.MUST)
    .add(rangeQuery, BooleanClause.Occur.MUST)
    .build();

returned the correct result.

Leave a Reply

Your email address will not be published. Required fields are marked *