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.

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • 1
    I think we need to see the code that adds this field to the index documents – itsadok Mar 10 '09 at 13:32
  • Does companyName only contain "mercedes trucks"? Have you considered that there might be a casing problem? Or maybe some sort of StemmingFilter that strips out the ending s's ? – jishi Jan 10 '12 at 16:57
  • @jishi: I am no longer working on this project and have no access to the code anymore. I think it might be a casing issue but I cannot check this anymore – Boris Callens Jan 11 '12 at 08:23

7 Answers7

10

Maybe replace:

mercedes trucks 

with

mercedes?trucks

Works for me.

kamranicus
  • 4,207
  • 2
  • 39
  • 57
derCris
  • 101
  • 1
  • 2
  • In the indexed term or the query? What's the significance of the question mark? – DCShannon Feb 12 '15 at 00:11
  • 1
    More than three years late, but for the benefit of anyone reading this in the future: The question mark is a wildcard for a single character in Lucene's query syntax – Zout Aug 28 '15 at 16:00
9

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,211
  • 2
  • 18
  • 29
  • This can be dangerous - http://msdn.microsoft.com/en-us/library/ms973837.aspx "String.Split is going to create an array of strings, which means one new string object for every keyword originally in your keywords string plus one more object for the array. Yikes! If we're doing this in the context of a sort, that's a lot of comparisons and your two-line comparison function is now creating a very large number of temporary objects. Suddenly the garbage collector is going to be working very hard on your behalf, and even with the cleverest collection scheme there is just a lot of trash to clean up." – digiguru Oct 22 '12 at 09:20
4

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.

0

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

Ruggs
  • 1,600
  • 2
  • 16
  • 25
0

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
  • 28,822
  • 30
  • 126
  • 171
0

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
  • 20,565
  • 5
  • 44
  • 69
-1

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

  • This will ignore the spaces, which is the opposite of including them in the search. In other words, "One Two" should match "One Two", not "OneTwo". – DCShannon Feb 12 '15 at 00:10