如何使用 NEST 提升嵌套对象

how to boost nested object with NEST

我已将我的索引映射如下。简单地说,我有一个产品索引,其中包含属性 Id、Number、ManufactureNumber、shortDescription、名称和嵌套对象,如 SubProduct

"mappings": {
 "Product": {
"properties": {
    "id": { "index": "no","store": true,"type": "integer"},
    "name": { "store": true,"type": "string"},
    "image": { "properties": { "fileName": { "index": "no","store": true,"type": "string"},"virtualPath": { "index": "no","store": true,"type": "string"}}},
    "number": { "index": "not_analyzed","store": true,"type": "string"},
    "manufactureNumber": { "index": "not_analyzed","store": true,"type": "string"},
    "subProduct": { "type": "nested","properties": { "name": { "store": true,"type": "string"},"number": { "index": "not_analyzed", "store": true,"type": "string"},"Id": { "index": "no","store": true,"type": "integer"}}}
    }
    }
    }

我在这里想要的是在 name、Number、ManufactureNumber、shortDescription 和 SubProduct.name、SubProduct.number 的属性中搜索关键字。因此,如果找到此文档中的任何关键字,应返回以下优先级(分数)

基于这些要求并经过我的研究,我认为带有提升的多匹配查询是我唯一的选择。我对此是否正确?或者任何 query_string 查询也可以做到这一点? 这就是我尝试的方式,但我陷入了嵌套对象的部分。我不知道如何提升它们?下面的代码会给出错误,数字和名称不是子产品的属性。

var results = Client.Search<Product> (body => body.Query(query => query.MultiMatch(qs =>
qs.OnFieldsWithBoost(d => d.Add(entry => entry.manufactureNumber , 5.0)
   .Add(entry => entry.number , 4.0)
   .Add(entry => entry.name,3.0)
   .Add(entry => entry.subproduct.number,2.0)
   .Add(entry => entry.subproduct.name,1.0)
 ).Type(TextQueryType.BestFields).Query(key))));

查询嵌套字段时需要使用Nested Query。在您的例子中,它们是 subProduct.numbersubProduct.name。您可能感兴趣的查询如下:

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "type": "best_fields",
            "query": "key",
            "fields": [
              "manufactureNumber^5",
              "number^4",
              "name^3"
            ]
          }
        },
        {
          "nested": {
            "query": {
              "multi_match": {
                "type": "best_fields",
                "query": "key",
                "fields": [
                  "subProduct.number^2",
                  "subProduct.name^1"
                ]
              }
            },
            "path": "subProduct"
          }
        }
      ]
    }
  }
}

对应的Nest查询如下:

var results = client.Search<Product>(s => s
    .Query(q => q
        .Bool(b => b
            .Should(
                sh => sh.MultiMatch(qs => qs
                    .OnFieldsWithBoost(d => d
                        .Add("manufactureNumber", 5.0)
                        .Add("number", 4.0)
                        .Add("name", 3.0))
                    .Type(TextQueryType.BestFields)
                    .Query(key)),
                sh => sh.Nested(n => n
                    .Path("subProduct")
                    .Query(nq => nq
                        .MultiMatch(qs => qs
                            .OnFieldsWithBoost(d => d
                                .Add("subProduct.number", 2.0)
                                .Add("subProduct.name", 1.0))
                            .Type(TextQueryType.BestFields)
                            .Query(key))))))));