如何使用NEST

时间:2016-01-19 12:44:25

标签: c# elasticsearch nest

我已将我的索引映射如下。我只有一个产品索引,其中包含属性Id,Number,ManufactureNumber,shortDescription,name和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属性中搜索关键字。因此,如果找到关键字,则应返回任何此文档,并返回以下优先级(分数)

  • manufactureNumber = 5.0
  • number = 4.0
  • 名= 2.0
  • subproduct.number = 2.0
  • subproduct.name = 1.0

基于这些要求并在我的研究之后,我认为使用提升的多匹配查询是我唯一的选择。我对此是否正确?或任何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))));

1 个答案:

答案 0 :(得分:2)

在查询嵌套字段时需要使用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))))))));