Let Lucene include spaces in terms for exact match

I want my Lucene query to contain something similar to:

companyNam: mercedes trucks

Where it will exactly match the line "mercedes trucks" in the companyName field.
CompanyName is an unclosed field, but all with a space returns zero results.

new TermQuery(new Term("companyName", "mercedes trucks")); 

Always prints 0 results if there is a space. Otherwise, my program works fine.

+7
source share
7 answers

Use PhraseQuery as follows:

 //create the query objects BooleanQuery query = new BooleanQuery(); PhraseQuery q2 = new PhraseQuery(); //grab the search terms from the query string string[] str = Sitecore.Context.Request.QueryString[BRAND_TERM].Split(' '); //build the query foreach(string word in str) { //brand is the field I'm searching in q2.Add(new Term("brand", word.ToLower())); } //finally, add it to the BooleanQuery object query.Add(q2, BooleanClause.Occur.MUST); //Don't forget to run the query Hits hits = searcher.Search(query); 

Hope this helps!

+9
source share

Maybe replace:

 mercedes trucks 

from

 mercedes?trucks 

It works for me.

+8
source share

You can use a different analyzer during the search than the one with which you created the index.

Try using KeywordAnalyzer while searching. It will create the only search string token that you are probably looking for.

+4
source share

I assume here is exactMask add quotes around the string? You just have to use the string "mercedes truck" without manipulating it.

 new TermQuery(new Term("companyName", "mercedes trucks")); 
0
source share

Do you find using PhraseQuery ? Should the field be unpainted? I believe that untokenized for identifiers, etc., and not for fields containing multiple words as their contents.

0
source share

The best way I found work was to analyze the query using a keyword analyzer with the following query "mercedes? Trucks".

0
source share

Even I ran into the same problem. You must do the following to get rid of this problem. 1) When you add a field value to the document, remove the spaces between them. 2) Enter a lowercase field value. 3) Make the search text lowercase. 4) Remove the spaces in the search text. Regards ~ Shef

-one
source share

All Articles