Sitecore搜索:按术语获取结果

时间:2015-07-28 09:58:06

标签: linq search sitecore iqueryable predicatebuilder

在这里,我正在尝试获取多个术语的搜索结果。说全文=“李牛仔裤”,然后regexresult = {“lee”,“jeans”}。

代码:

IProviderSearchContext searchContext = index.CreateSearchContext();
IQueryable<SearchItem> scQuery = searchContext.GetQueryable<SearchItem>();
var predicate = PredicateBuilder.True<SearchItem>();

   //checking if the fulltext includes terms within " "
                    var regexResult = SearchRegexHelper.getSearchRegexResult(fulltext);

                    regexResult.Remove(" ");

                    foreach (string term in regexResult)
                    {
                        predicate = predicate.Or(p => p.TextContent.Contains(term));
                    }
                    scQuery = scQuery.Where(predicate);

IEnumerable<SearchHit<SearchItem>> results = scQuery.GetResults().Hits;

results=sortResult(results);

排序基于sitecore字段:

  switch (query.Sort)
  {
    case SearchQuerySort.Date:
    results = results.OrderBy(x => GetValue(x.Document, FieldNames.StartDate));
    break;
    case SearchQuerySort.Alphabetically:
    results = results.OrderBy(x => GetValue(x.Document, FieldNames.Profile));
    break;
    case SearchQuerySort.Default:
    default:
    results = results.OrderByDescending(x => GetValue(x.Document, FieldNames.Updated));
    break;
}

现在,我需要的是首先获得“lee”的结果并对它们进行排序,然后找到“牛仔裤”的结果并对它们进行排序。最终的搜索结果将首先包含“lee”的排序项集合,然后是“jeans”。

因此,我们必须首先获得“lee”的结果,然后获得“牛仔裤”的结果

有没有办法按期限获得结果?

1 个答案:

答案 0 :(得分:6)

您可以使用Query-Time Boosting为术语提供更多相关性,从而影响排名:

你想给第一个词提供最高的提升,然后逐渐减少每个额外的词:

var regexResult = SearchRegexHelper.getSearchRegexResult(fulltext);
regexResult.Remove(" ");
float boost = regexResult.Count();

foreach (string term in regexResult)
{
    predicate = predicate.Or(p => p.TextContent.Contains(term)).Boost(boost--);
}

修改 在同一查询中进行提升和排序是不可能的,至少,排序将撤消由于提升而返回的“相关性”排序。

替代方法是多次搜索并连接返回单个列表的结果。由于您基本上进行了多次搜索,因此效率不高:

IProviderSearchContext searchContext = index.CreateSearchContext();
var items = new List<SearchResultItem>();

var regexResult = SearchRegexHelper.getSearchRegexResult(fulltext);

regexResult.Remove(" ");

foreach (string term in regexResult)
{
    var results = searchContext.GetQueryable<SearchResultItem>()
                                   .Where(p => p.Content.Contains(term));
    SortSearchResults(results); //results passed in by reference, no need to return object to set it back to itself

    items.AddRange(results);
}

注意:上述内容未考虑结果集之间的重复。