David Thibault
David Thibault

Reputation: 8736

How to index numeric fields and search them by range in Lucene.Net?

I'm looking for an efficient way to index and search numeric fields in Lucene.Net. Right now my need is for integer values only.

I want to search by ranges of values (between x and y, more than x...). Right now I'm indexing the number as is and manually creating clauses for each value in between the two values, but it quickly generates a lot of clauses and hitting the default limit (1024) when I'm searching for a value between 1000 and 5000, for example. I'm sure there is a better way to do that...

Upvotes: 3

Views: 2062

Answers (1)

David Thibault
David Thibault

Reputation: 8736

Solved it with a RangeQuery. It can also be done with the query parser with the following syntax:

FieldName:[startValue TO endValue]

Keep in mind that the range query operates on strings, so your numbers must be normalized both in the index and in the query. In my case I normalized to 5 digits with .ToString("00000"). Special handling would be necessary for negative numbers and decimals, but it wasn't an issue in my case.

Upvotes: 6

Related Questions