简体   繁体   中英

Linq is not working with sitecore solr

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: http://i.stack.imgur.com/liw59.png

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")

  • This is telling us to find any term in the list that matches this given value. If our term was "scooter loan" , we would not get any results back.

Contains : ?q=keywordstest_t:(*bike loan*)

  • This is telling us to return any result item where any term in the list contains any of the words/sub-words. This isn't search performant. If our term was "scooter L" , we would get a result back because of the L .


A couple things to check

: Is this value set to true in the 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" :

  1. Search on scooter and loan separately, where each search query term is an exact match or is contained in an indexed term
  2. Search on scooter loan as a whole

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM