tags:

views:

76

answers:

2

Hi,

I would like to combine a numeric range query with a term query in Lucene. For example, I want to search for documents that I have indexed that contain between 10 and 20 pages and have the title "Hello World".

It does not seem possibly to use the QueryParser to generate this query for me; the range query that the QueryParser generates appears to be a text one.

I definitely would appreciate an example of how to combine a numeric range query with a term query. I would also be open taking an alternative to searching my index.

Thanks

A: 
RangeQuery amountQuery = new RangeQuery(lowerTerm, upperTerm, true);

Lucene treats numbers as words, so the numbers are ordered alphabetically.

1
12
123
1234
etc.

That being said, you can still use the range query, you just need to be more clever about it.

In order to query numeric values correctly, you need to pad your integers so the same lengths (whatever your maximum supported value is)

0001
0012
0123
1234

Obviously, this doesn't work for negative numbers (since -2 < -1), and hopefully you won't have to deal with them. Here's a useful article for negatives if you do encounter them: http://wiki.apache.org/lucene-java/SearchNumericalFields

Cambium
A: 

Well it looks like I figured this one out on my own. You can use Query.combine() to OR queries together. I have included an example below.

String termQueryString = "title:\"hello world\"";
Query termQuery = parser.parse(termQueryString);

Query pageQueryRange = NumericRangeQuery.newIntRange("page_count", 10, 20, true, true);

Query query = termQuery.combine(new Query[]{termQuery, pageQueryRange});
Chris J