tags:

views:

940

answers:

7

I want my Lucene query to contain something similar to:

companyNam:mercedes trucks

Where it will do an exact match for the string "mercedes trucks" in the companyName field.
The companyName is an untokenized field, but anything with a space returns null results..

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

Always results 0 results if there is a space involved. Otherwise my program is working fine.

A: 

I'm guessing here - does exactMask add quotes around the string? You should simply use the string "mercedes truck", without manipulating it.

new TermQuery(new Term("companyName", "mercedes trucks"));
itsadok
Indeed a bit unclear, will edit original post. I tried the mask with both quotes and not quotes. None worked.
borisCallens
A: 

Have you considered using a PhraseQuery? Does the field have to be untokenized? I believe untokenized is for ids etc. and not for fields having several words as their content.

Yuval F
I added an untokenized one only for this. Because I figured I need the spaces.
borisCallens
+3  A: 

You may be using different analyzer while searching than the one with which you created the index.

Try using KeywordAnalyzer while searching. It will create single token of the search string which is probably what you are looking for.

I will check that out. Thanks.
borisCallens
Had the same issue and using KeywordAnalyzer fixed it for me.
Jason Rowe
A: 

Even I am facing the same issue. You have to do the following thing to get rid of from this issue. 1)When add the field value to the document remove the spaces in between. 2)Make the field value in lowercase. 3)Make the search text in lowercase. 4)Remove the white spaces in the search text. Regards ~shef

+1  A: 

Use a PhraseQuery like this:

//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!

kirk.burleson
+1 for the split. worked very nice.
mathieu
A: 

maybe replace mercedes trucks with mercedes?trucks works for me...

derCris
A: 

The best way that I found that works is to parse the query using the keyword analyzer with the following query "mercedes?trucks".

Ruggs