Abhishek Parikh
Abhishek Parikh

Reputation: 181

Lucene: I would like to do a search on the index which I have built using Lucene but while searching I would like to ignore case

Lucene: I would like to do a search on the index which I have built using Lucene but while searching I would like to ignore case of the search criteria , how can i do it ? For example i have in my index(lucene) either http or HTTP so when I search using http i should get both the results back.

Upvotes: 0

Views: 1247

Answers (2)

Christopher Currens
Christopher Currens

Reputation: 30735

That depends on a few things. First, if the analyzer you used to index didn't already tokenize the text into lowercase, you'll find it extremely difficult to do this.

The KeywordAnalyzer, I believe, preserves case. So, the word "Hello" would be left as "Hello" as a term in the index, meaning the only way you could find that word, is to use the exact same case.

The StandardAnalyzer, however, converts all terms to lowercase. So, "Hello" would be "hello" in the terms list, but if you're storing the text as well, not just analyzing it, it would preserve the case of the string if you were to retrieve it from the index. However, a search for "HEllo", "heLLO", "HeLlO" would all be the same as "hello", as it would convert all of those to lowercase.

You are not stuck with these analyzers, either, you can always create your own.

Finally, it is highly recommended that you use the same Analyzer you used to create the index when you are searching it.

Upvotes: 4

Jayendra
Jayendra

Reputation: 52809

Should add LowerCaseFilter to the analysis filter chain.
Using Lucene’s LowerCaseFilter the tokens could be lowercased before indexing and searching to make searches case-insensitive.
Would suggest it to be done during both indexing and search.

Upvotes: 1

Related Questions