Bharathi Reddy
Bharathi Reddy

Reputation: 11

Space issue in Lucene.NET C#

I want to search sentence which has space in full text search.

Ex: Tom is a very good boy in class. I want to Search the key word "very good".

I'm using white space tokenizer to create/search index. But it is not finding the keyword if it is separated by space.

Code:

 Query searchItemQuery = new WildcardQuery(new Term(string-field-name, searchkeyword.ToLower()));

I've tried with split but it is not working properly.

Do anyone suggest me a solution for this problem?

Thanks, Vijay

Upvotes: 1

Views: 1089

Answers (1)

Alexey Shcherbachev
Alexey Shcherbachev

Reputation: 237

Since, you are working with tokenized string, every word is a separate term. In order too find a phrase consisting of multiple terms, you would need to use PhraseQuery instead of WildcardQuery. Like this:

PhraseQuery phraseQuery = new PhraseQuery();
phraseQuery.Add(new Term(string-field-name, "very"));
phraseQuery.Add(new Term(string-field-name, "good"));

Note also, that you are using wildcard query. Wildcards in phrase query are a bit complex. Check this post for details: Lucene - Wildcards in phrases

And finally, I would suggest to consider using QueryParser instead of constructing query manually.

Upvotes: 2

Related Questions