3

I was building query on the basis of the occurence/relevancy of the terms present in a field and got one issue , suppose i searched for a keyword "text" in the content field , and i want to sort the results on the basis of the relevancy of the term present in the field , for ex , if that keyword is present more times then that document will be listed on the top . here is my code

 var queryableResultItems = context.GetQueryable<GlobalSearchResultItem>()
              .Where(x => x.Language.Equals(Sitecore.Context.Language.Name, StringComparison.InvariantCultureIgnoreCase)).Where(UseSearchComponents.GetTemplatesFilterPredicate(Guid.Parse(TemplateConstants.BlogPreviewTemplate))).Where(UseSearchComponents.GetSearchFilters(keyword, searchType)).Skip((Int32.Parse(pagenumber) - 1) * 12).Take(12).GetResults();


 public static Expression<Func<GlobalSearchResultItem , bool>> GetSearchFilters(string Keyword,string searchType)
    {
        Expression<Func<GlobalSearchResultItem, bool>> filterSearch = x => true;
        if(Keyword.IsTrimmedNullOrEmpty() || searchType.IsTrimmedNullOrEmpty())
        {
            return filterSearch;
        }
        Expression<Func<GlobalSearchResultItem, bool>> filterSearchText = x => true;
        if (!string.IsNullOrEmpty(Keyword) && searchType=="exactly")
        {    
            filterSearchText = filterSearchText.Or(x => x.Content.Contains(Keyword));
            filterSearch = filterSearch.And(filterSearchText);
        }
        if(!string.IsNullOrEmpty(Keyword) && searchType == "allwords")
        {
            float slop = 1000000;
            filterSearchText = filterSearchText.Or(x => x.Content.Like(Keyword,slop));
            filterSearch = filterSearch.And(filterSearchText);
        }
        return filterSearch;
    }
}

and then i used the termfreq("content_t","text") desc function in the solr console , but i don`t know how to do it programatically in solr using the contentsearch api. Some help would be apppreciated. Thanks in advance.

1 Answer 1

7

You can do it with:

var queryableResultItems = context.GetQueryable<GlobalSearchResultItem >()
                    .Where(expression)
                    .OrderByDescending(x => x["termfreq(content_t, \""+ term+ "\")"])

But I suggest you to setup logic that you need on Solr side. Sorl sorts the results based on the relevancy of the term present in the field OOTB (it uses tf-idf algorithm to calculate scoring).

You should check query analizer of your field type in managed-schema (your field should be tokenized):

<analyzer type="query">
      <tokenizer class="solr.WhitespaceTokenizerFactory"/>
      ...
    </analyzer>

enter image description here

You can check it in Solr by adding "score" in fl textbox. You can also see how scoring is calculating by checking debugQuery: enter image description here

As you can see, Solr uses termFreq OOTB while scoring: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.