user1459641
user1459641

Reputation: 458

Lucene phase query case insensitive

I am writing a query to do an exact match on a 'city' field. The field/property is defined as:

@org.hibernate.search.annotations.Field(index = Index.YES, analyze = Analyze.NO, store = Store.NO)
private String city;

If I have the value of "New York", I want to find a match if user enters "new york", or some variation of case. I am using the StandardAnalyzer for the entity, so I know that will lowercase all the tokens. I don't tokenize since I want to match the phrase (Analyze.NO).

I tried to lowercase my search value, but no luck.

Query query = qb.phrase().onField(.....).sentence(location.toLowerCase()).createQuery();

If I don't lowercase the search term and the value is 'New York', results are returned. Searching for 'new york' does not return any result.

If I tokenize (Analyze.YES), then other cities like 'New Jersey' are returned. I know I can use a wildcard query (searchTerm*), but I was hoping to be able to do a case insensitive search on a phrase. Just not sure if that's possible unless you use the wildcard.

thanks

Upvotes: 0

Views: 1306

Answers (1)

RandomMooCow
RandomMooCow

Reputation: 734

It sounds like you would want to use an analyzer which emits the entire text as a single token while lower-casing the input. In this case, you would want to use analyze=Analyze.YES, while specifying the appropriate analyzer (the answer here has code that looks like what you need) using analyzer=@Analyzer(impl=your.fully.qualified.Analyzer.class).

Upvotes: 2

Related Questions