0

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) {

    }
}
Yanick Nedderhoff
  • 1,174
  • 3
  • 18
  • 35

2 Answers2

1

You need to first use StandardQueryParser, then provide the parser with a PointsConfig map, essentially hinting which fields are to be treated as Points. You'll now get 2 results.

// Change this line to the following
StandardQueryParser parser = new StandardQueryParser(analyzer);
IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(dir));

/* Added code */
PointsConfig longConfig = new PointsConfig(new DecimalFormat(), Long.class);
Map<String, PointsConfig> pointsConfigMap = new HashMap<>();
pointsConfigMap.put("my_range_field", longConfig);
parser.setPointsConfigMap(pointsConfigMap);
/* End of added code */

String luceneQuery = "+my_text_field:test* +my_range_field:[1 TO 200]";
// Change the query to the following
Query query = parser.parse(luceneQuery, "text");
  • Please accept this as the answer. The current accepted answer indicates that the query parser cannot parse the string you attempted. This answer shows you how to set it up to correctly parse your string. – Thomas Wilde Apr 23 '22 at 04:20
0

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.

Yanick Nedderhoff
  • 1,174
  • 3
  • 18
  • 35