简体   繁体   English

如何使用 ElasticSearch DSL C# 降低与特定字段和值匹配的文档的分数

[英]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.

相关问题 如何使用MongoDB和C#使用另一个字段的值更新文档? - How to update documents using another field's value using MongoDB and C#? 尝试使用NEST搜索c#中所有弹性搜索文档中的任何单词(没有指定字段) - Trying to search any word present in all of my elasticsearch documents (no field specified) in c# using NEST 如何从C#中的正则表达式匹配分组中获取特定值 - How to get specific value from Regex match grouping in C# 如何在C#中使用linq从xml读取特定字段值 - How to read specific field value from xml using linq in C# 如何使用 Mongodb c# 驱动程序更新文档的数组字段的特定索引值? - How to update document's array field's specific indexed value using Mongodb c# driver? 如何使用 C# 驱动程序 2.10.4 查找 MongoDB 中特定字段的最小值 - How to find the min value of a specific field in MongoDB using C# driver 2.10.4 如何使用RegEx(C#)在特定字符串之后匹配字符串 - How to match a string after a specific string using RegEx (C#) 如何在特定字段值上触发动态 c# 插件 - How to fire a dynamics c# plugin on specific field value 如何使用C#将表值与文本框值匹配 - how to match table value to a text box value using c# 使用C#作为我的DSL-如果可能,这怎么可能? - Using C# as my DSL — is this possible, if so, how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM