2

I want to search terms which is in [0, 10], and terms are from the field "CityId" that is stored as Integer. What I have written is below:

String queryStr = "CityId : [0, 10]";
Query query = parser.parse(queryStr);
TopDocs topDocs = searcher.search(queryStr);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Fairy_LFen
  • 61
  • 1
  • 3
  • And what is the reult of your querry? If you dont want to parse a querry string you can build a NumericRangeQuery like this: NumericRangeQuery. newIntRange(0,10,true, true) – TobiSH Apr 09 '16 at 15:38

1 Answers1

2

The standard QueryParser does not support numeric ranges. The range queries supported by the QueryParser are Lexicographic, rather than numeric. You will need to use a NumericRangeQuery for this:

Query query = NumericRangeQuery.newIntRange("CityId", 0, 10, true, true);

Also, might be good to refer to the QueryParser documentation for what Lucene's query syntax looks like. Range queries upper and lower bounds are not separated by a comma. It should look like myField:[begin TO end] (again, though, this will not be a numeric range).

femtoRgon
  • 32,893
  • 7
  • 60
  • 87