[英]How to lower the score of documents that match a specific field and value using ElasticSearch DSL C#
When I carry out a search I look for documents with a lang code of 'en' and 'gen', im trying to ensure that the 'en' ones appear before the 'gen' ones do.当我进行搜索时,我会查找语言代码为“en”和“gen”的文档,我试图确保“en”出现在“gen”之前。 So after looking around, I found this Boosting that might fit the bill as I don't want to completely exclude the 'gen' results..just drop the score a tad.
所以环顾四周后,我发现这个 Boosting 可能符合要求,因为我不想完全排除 'gen' 结果......只需稍微降低分数。
Here's what i have so far.. any help would be greatly appreciated (still learning about ElasticSearch).这是我到目前为止所拥有的......任何帮助将不胜感激(仍在学习 ElasticSearch)。
SearchDescriptor<ExpandoObject> searchDescriptor = new SearchDescriptor<ExpandoObject>()
.Index(!searchModel.IndexesToSearch.Any() ? Indices.All : Indices.Index(searchModel.IndexesToSearch))
.From(searchModel.IndexOfLastResult)
.Size(searchModel.AmountOfResults)
.MinScore(searchModel.MinnimumResultScore)
.SearchType(SearchType.DfsQueryThenFetch);
IPromise<IList<ISort>> SortDescriptor(SortDescriptor<ExpandoObject> s) => s.Field(searchModel.SortByField, searchModel.SortByOrder);
List<Func<QueryContainerDescriptor<ExpandoObject>, QueryContainer>> filters = new List<Func<QueryContainerDescriptor<ExpandoObject>, QueryContainer>>();
foreach (var filter in searchModel.FilterValues)
{
filters.Add(fq => fq.Terms(t => t.Field(filter.FieldToSearch.Trim()).Terms(filter.Values)));
}
searchDescriptor
.Query(q => q
//.Boosting(b => b
// .Name("language")
// .Negative(qq => qq
// .MatchAll(m => m
// .Name("gen")))
// .NegativeBoost(0.2)
.Bool(b => b
.Should(s => s
.MultiMatch(m => m
.Fields( string.Join(',', searchModel.FieldsToSearch))
.Query(searchModel.SearchTerm)))
.Filter(filters)
)).Sort(SortDescriptor);
This query appears as:此查询显示为:
{
"from": 0,
"min_score": 0.1,
"query": {
"bool": {
"filter": [
{
"terms": {
"language": [
"en",
"gen"
]
}
}
],
"should": [
{
"multi_match": {
"fields": [
"title"
],
"query": "foo"
}
}
]
}
},
"size": 100,
"sort": [
{
"_score": {
"order": "desc"
}
}
]
}
You can try using negative boosting query to demote certain documents without excluding them from search results您可以尝试使用否定提升查询来降级某些文档而不将它们从搜索结果中排除
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html#query-dsl-boosting-query https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html#query-dsl-boosting-query
Another approach (I am writing the query, you can replace the same logic in C++):另一种方法(我正在编写查询,您可以在 C++ 中替换相同的逻辑):
You can also apply less weightage to the docs which match lang code as 'gen'您还可以对将 lang 代码匹配为“gen”的文档应用较少的权重
{
"filter": {
"bool": {
"must":{"match": {"language": "gen}}
}
},
"weight": 0.50
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.