The Solr search provider in Sitecore 7 works quite well if the search query contains a single term. However, if the search query contains multiple terms, you will need to tweak your code a little bit to get the expected results.
The following codes performs a search using a query that contains a single term.
var indexName = "sitecore_web_index"; var index = ContentSearchManager.GetIndex(indexName); using (var context = index.CreateSearchContext()) { var query = "paint"; var dataQuery = context.GetQueryable() .Where(i => i.Title == query); var results = dataQuery.GetResults().Hits.Select(h => h.Document); }
The Sitecore search provider generates the following Solr query:
title_t:(paint)
However, if you replace the
var query = "paint";
With
var query = "purple paint";
The generated Solr query will be:
title_t:("purple paint")
If the search query contains whitespace characters, the search provider will automatically wrap the query terms in double quotes. This will cause Solr to skip the query analysers – such as tokenization and stemming. So, it will only return results containing the string “purple paint” in the title, and will fail to match titles such as “purple-paint”, or “purple and red paint” and so on.
In order to work around the whitespace problem, you can implement one of the following solutions:
Recent Comments