new QueryParser(.... ).parse (somequery);
it works only for string indexed fields. Say i have a field called count where count is a integer field (while indexing the field I considered the data type)
new QueryParser(....).parse("count:[1 TO 10]");
The above one is not works. Instead If i used "NumericRangeQuery.newIntRange" which is working. But, i need the above one only...
Had the same issue and solved it, so here I share my solution:
To create a custom query parser that will parse the following query "INTFIELD_NAME:1203" or "INTFIELD_NAME:[1 TO 10]" and handle the field INTFIELD_NAME as an Intfield, I overrided newTermQuery with the following:
I took the code quoted in that thread from http://www.mail-archive.com/search?l=java-user@lucene.apache.org&q=subject:%22Re%3A+How+do+you+properly+use+NumericField%22&o=newest&f=1 and made 3 modifications :
rewrote newRangeQuery a little more nicely
replaced in newTermQuery method
NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()),NumericUtils.PRECISION_STEP_DEFAULT)));
by
NumericUtils.intToPrefixCoded(Integer.parseInt(term.text()), 0, byteRefBuilder);
when I used this method for the first time in a filter on the same numeric field, I put 0 as I found it as a default value in a lucene class and it just worked.
replaced on newTermQuery
TermQuery tq = new TermQuery(new Term(field,
by
TermQuery tq = new TermQuery(new Term(term.field(),
using "field" is wrong, because if your query has several clauses (FIELD:text OR INTFIELD:100), it is taking the first or previous clause field.
QueryParser won't create a NumericRangeQuery as it has no way to know whether a field was indexed with NumericField. Just extend the QueryParser to handle this case.
I adapted Jeremies answer for C# and Lucene.Net 3.0.3. I also needed type double instead of int. This is my code:
I improved my code to also allow queries like greater than and lower than when an asterisk is passed. E.g.
p:[* TO 5]
You need to inherit from
QueryParser
and overrideGetRangeQuery(string field, ...)
. Iffield
is one of your numeric field names, return an instance ofNumericRangeQuery
, otherwise returnbase.GetRangeQuery(...)
.There is an example of such an implementation in this thread: http://www.mail-archive.com/java-user@lucene.apache.org/msg29062.html
In Lucene 6, the protected method
QueryParser#getRangeQuery
still exists with the argument list(String fieldName, String low, String high, boolean startInclusive, boolean endInclusive)
, and overriding it to interpret the range as a numeric range is indeed possible, as long as that information is indexed using one of the newPoint
fields.When indexing your field:
At your custom query parser (extending
queryparser.classic.QueryParser
), override the method with something like this: