【问题标题】:Elasticsearch.Net calculate custom document item score with NESTElasticsearch.Net 使用 NEST 计算自定义文档项目分数
【发布时间】:2020-11-29 20:32:30
【问题描述】:

我有一个问题需要您帮忙。我在文档上的客户来源有 unitid 字段。我想首先列出当前单位客户。所以我尝试使用脚本评分,但所有文档项目评分计算为 0,它从未奏效。成功评分后,我将按分数对结果进行排序。

这是我的过滤器脚本;

.ScriptScore(s => s
    .Script(ss => ss
        .Source($"doc['unitid'].value == params.unitid ? _score = 1 : _score = 0")
        .Params(new Dictionary<string, object>() { { "unitid", filter.UnitId } })
    )
)

然后这是我的全部查询;

.Query(q => q
.Bool(b => b
    .Filter(m => m
        .Prefix(t => t.CustomerName, filter.CustomerName?.ToUpper()) && m
        .Terms(t => t
            .Field(f => f.CustomerTypeId)
            .Terms(new List<int> { 2, 3 })
        ) && m
        .ScriptScore(s => s
            .Script(ss => ss
                .Source($"doc['unitid'].value == params.unitid ? _score = 1 : _score = 0")
                .Params(new Dictionary<string, object>() { { "unitid", 272 } })
            )
        )
    )
))
.Aggregations(a => a
    .Terms("distinct_accounts", c => c
        .Field(x => x.AccountId
            .Suffix("keyword")
        )
    ))
.Sort(s => s
    .Descending("_score")
    .Descending(a => a.ModifyDate));

结果,我哪里做错了,你能帮帮我吗?

【问题讨论】:

    标签: sorting elasticsearch .net-core nest scoring


    【解决方案1】:

    脚本分数查询的源脚本需要返回分数值,而不是把它分配给像_score这样的变量

    .ScriptScore(s => s
        .Script(ss => ss
            .Source($"doc['unitid'].value == params.unitid ? 1 : 0")
            .Params(new Dictionary<string, object>() { { "unitid", filter.UnitId } })
        )
    )
    

    编辑:

    刚刚注意到脚本分数查询没有.Query() 部分,但它需要一个完整的查询。另外,查询是inside a bool query filter clause, meaning that scoring is ignored。

    更多关于您尝试解决的潜在问题,通过提高某些查询的分数来实现比其他人更高的分数更高的分数。例如

     var client = new ElasticClient();
    
    var filter = new 
    {
        CustomerName = "foo",
        UnitId = 1
    };
    
    var response = client.Search<Product>(s => s
        .Query(q => q
            .Bool(b => b
                .Should(s => s
                    .Term(t => t
                        .Boost(10)
                        .Field("unitid")
                        .Value(filter.UnitId)
                    )
                )
                .Filter(fi => fi
                    .Prefix(t => t.CustomerName, filter.CustomerName?.ToUpper()), 
                        fi => fi
                    .Terms(t => t
                        .Field(f => f.CustomerTypeId)
                        .Terms(new List<int> { 2, 3 })
                    )
                )
            )
        )
        .Aggregations(a => a
            .Terms("distinct_accounts", c => c
                .Field(x => x.AccountId
                    .Suffix("keyword")
                )
            )
        )
        .Sort(s => s
            .Descending("_score")
            .Descending(a => a.ModifyDate)
        )
    );
      pre>
    

    产生查询

    {
      "aggs": {
        "distinct_accounts": {
          "terms": {
            "field": "accountId.keyword"
          }
        }
      },
      "query": {
        "bool": {
          "filter": [{
              "prefix": {
                "customerName": {
                  "value": "FOO"
                }
              }
            },
            {
              "terms": {
                "customerTypeId": [
                  2,
                  3
                ]
              }
            }
          ],
          "should": [{
            "term": {
              "unitid": {
                "value": 1,
                "boost": 10.0
              }
            }
          }]
        }
      },
      "sort": [{
          "_score": {
            "order": "desc"
          }
        },
        {
          "modifyDate": {
            "order": "desc"
          }
        }
      ]
    }
    

    此处,如果文档具有unitid值为1,那么这为此的分数是增强的十倍。无论如何,我都不认为实际上需要提升,因为没有unitid值为1的文件将没有分数,而另一个查询处于过滤器分数,因此不会为其贡献分数。在 should 子句中存在 filter 子句的 unitid 上的术语查询,此查询充当增强信号,因为文档不需要匹配要返回的此查询,但如果它们匹配,则should 子句中的分数将与其他查询的分数相加,形成一个总分,我认为这将达到您想要的效果。

    【讨论】:

    • @ waayd添加了一些其他详细信息。 span>
    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多