gpt4 book ai didi

linq - Sitecore 搜索 : Get results term by term

转载 作者:行者123 更新时间:2023-12-01 13:26:49 25 4
gpt4 key购买 nike

在这里,我试图获取多个术语的搜索结果。说 fulltext="Lee jeans",然后 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);

排序基于站点核心字段:

  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”的结果并对它们进行排序,然后找到“jeans”的结果并对其进行排序。最终的搜索结果将首先包含“lee”的串联排序项目集,然后是“jeans”的排序项目。

因此我们必须首先获得“lee”的结果,然后是“jeans”的结果

有没有一种方法可以逐项获得结果?

最佳答案

您可以使用 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);
}

注意:以上没有考虑结果集之间的重复。

关于linq - Sitecore 搜索 : Get results term by term,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com