Incorrect Range Search Query Example in Solr Documentation
19 01 2009Many of you may have seen this exception when trying out Solr’s range search query based on Solr’s documentation.
HTTP Status 400 - org.apache.lucene.queryParser.ParseException: Cannot parse 'rank:[1 to 10]': Encountered "10" at line 1, column 11. Was expecting: "]" ...
This exception is saying that Lucene’s query parser does not recognize a third value “10” in the range filter query. The parser only expects two values. It’s kind of confusing because Solr’s documentation gives an example of “field:[1 to 100]”. Interestingly, the documentation is consistent between Solr and Lucene’s documentation regarding the range search query format:
http://wiki.apache.org/solr/SolrQuerySyntax
http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Range Searches
As you can see, they both describe range search query’s syntax as
- Return results with value in field “rank” between 1 and 100
- fq=rank:[1 100]
- Return results with value in field “rank” less than or equal to 10
- fq=rank:[* 10]
- Return results with value in field “rank” greater than or equal to 10
- fq=rank:[10 *]
Join the forum discussion on this post - (2) Posts
These range queries definitely don’t work correctly in solr 1.4.
When using the range query on an integer field, when you use [1 to 2] or [1 2] it actually seems to do a wildcard and grab everything that STARTS with a 1, or is EQUAL to 2. It is very strange, no matter how you slice it, that is wrong.
When you do [1 TO 50] or [1 50] it gets everything that starts with a 1, 2, 3, 4 or is EQUAL to 50. Makes no sense to me!!!!
To verify, I tried searching for integer years in our index that are of the form 1998, 2010, etc, and when I use [1 TO 2] or [1 2] it seems to match all of the 19xx years, and none of the 20xx years.
Just to answer the initial problem identified in this post, the documentation says [1 TO 10] not [1 to 10] . The parser seems to be case sensitive in that regard. Found this problem the other day.
Don’t these examples mean – filtering a query based on a field’s range, rather than giving a rangequery itself?
Filter Query: Get all results and just filter them based on the Range of a particular field.
Range Query: The query itself includes this range issue and all documents inside this range will be considered more relevant than others, in the ranking process itself.
So, is there a facility in Solr to do that? I tried &bq=…. but I wonder if the boosting with something like… &bq=XYZ:[20 TO 25]^10 – really works!