Reputation: 75
I am displaying search results using sitecore and solr. The scenario is if user search for something, search code first will check if that keyword is in "keywords" field of sitecore items, if it finds it will show the result and if it does not find, it will check for that keyword in title and description fields.
Now the problem is -
below condition is always false and never gives the result, in spite of values in the keywords field.
var Query = searchContext.GetQueryable<SearchResultItem>()
.Where(i => (i["keywords"].Contains(SearchQuery)));
where as the same query for title and description works fine
var Query2 = searchContext.GetQueryable<SearchResultItem>()
.Where(i => (i["title"].Contains(SearchQuery)
|| i["description"].Contains(SearchQuery)));
for each sitecore item, I have title, description and keywords fields.
Below is the detail code snippet.
public class SearchModel
{
public string SearchQuery { get; set; }
List<WebSiteSearchResult> lst = new List<WebSiteSearchResult>();
public IEnumerable<WebSiteSearchResult> SiteSearchResult()
{
var searchContext = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext();
var Query = searchContext.GetQueryable<SearchResultItem>().Where(i => (i["keywords"].Contains(SearchQuery)));
var result = Query.GetResults();
if (result.TotalSearchResults != 0)
{
//some operations
}
else
{
var Query2 = searchContext.GetQueryable<SearchResultItem>().Where(i => (i["title"].Contains(SearchQuery) || i["description"].Contains(SearchQuery)));
var result2 = Query2.GetResults();
if (result2.TotalSearchResults != 0)
{
//some operations
}
}
lst = lst.OrderBy(i => i.itemBoostingRatio).ToList();
return lst;
}
}
public class WebSiteSearchResult
{
public string itemKeywords { get; set; }
public int itemBoostingRatio { get; set; }
public string itemTitle { get; set; }
public string itemDescription { get; set; }
}
And, here my sitecore items: https://i.sstatic.net/liw59.png
Upvotes: 1
Views: 2077
Reputation: 2661
Given your Sitecore item keywords are in a SLT field ("Loans, Car Loan, Bike Loan"), keep in mind that this field is a tokenized index. This means during storage processing, the query is split into tokens using whitespace and punctuation characters as delimiters, seen below:
Now if we search using Equals
and Contains
, the search log will give us the appropriate serialized query to test in solr:
Equals: ?q=keywordstest_t:("bike loan")
"scooter loan"
, we would not get any results back.Contains: ?q=keywordstest_t:(*bike loan*)
"scooter L"
, we would get a result back because of the L.
Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config
?
<indexAllFields>true</indexAllFields>
For best practices, create a new Search Result Item class that inherits from Sitecore's base SearchResultItem
so that you can use it this instance to directly call the field property 'Keywords':
public class BasePageSearchResultItem : SearchResultItem
{
[IndexField("keywords")]
public string Keywords { get; set; }
}
The Equals
query should be used to find one of these terms in the indexed field:
var Query = searchContext.GetQueryable<SearchResultItem>()
.Where(i => i.Keywords.Equals(SearchQuery));
The result set will depend on the requirements, dictated by the search query "scooter loan"
and given a sample item's keyword field value "Loans, Car Loan, Bike Loan"
:
Upvotes: 3