基于查询

时间:2016-01-28 12:18:49

标签: elasticsearch nested

我有多个嵌套文档的doc。嵌套查询工作正常但它们仍然会返回所有嵌套对象(即整个文档),即使搜索查询只匹配几个嵌套对象。然而,它确实过滤了整个文档。

以下是一个例子:

PUT /demo
{
  "mappings": {
    "company": {
      "properties": {
        "employees": {
          "type": "nested"
        }
      }
    }
  }
}

PUT
/demo/company/1
{
  "id": 1,
  "name": "Google",
  "emp_count": 3,
  "employees": [{
    "id": 1,
    "name": "John",
    "address": {
      "city": "Mountain View",
      "state": "California",
      "country": "United States"
    }
  }]
}

PUT
/demo/company/2
{
  "id": 1,
  "name": "Facebook",
  "emp_count": 3,
  "employees": [{
    "id": 1,
    "name": "Amber",
    "address": {
      "city": "Bangalore",
      "state": "Karnataka",
      "country": "India"
    }
  }, {
    "id": 1,
    "name": "Adrian",
    "address": {
      "city": "Palo Alto",
      "state": "California",
      "country": "United States"
    }
  }]
}

PUT
/demo/company/3
{
  "id": 1,
  "name": "Microsoft",
  "emp_count": 3,
  "employees": [{
    "id": 1,
    "name": "Aman",
    "address": {
      "city": "New York",
      "state": "New York",
      "country": "United States"
    }
  }]
}

在地址中搜索India时,我理想情况下只能使用一个嵌套对象获取Facebook,但我会获得所有嵌套对象。如何过滤返回的嵌套对象?

示例查询:

{
  "query": {
    "function_score":{
      "query":{
        "nested":{
         "path":"employees",
         "score_mode":"max",
         "query": {
            "multi_match":{
              "query":"India",
              "type":"cross_fields",
              "fields":[
                "employees.address.city",
                "employees.address.country",
                "employees.address.state"
              ]
            }
          }
        }
      }
    }
  }
}

此查询的输出为Facebook,所有员工只需要Amber

1 个答案:

答案 0 :(得分:0)

您可以使用inner_hits获取所需的结果。使用以下查询:

GET /demo/company/_search
{
"query" : {
    "nested" : {
        "path" : "employees",
        "query" : {
            "match" : {"employees.address.country" : "India"}
        },
        "inner_hits" : {} 
    }
  }
}

输出将是:

"hits": {
  "total": 1,
  "max_score": 1.4054651,
  "hits": [
     {
        "_index": "demo",
        "_type": "company",
        "_id": "2",
        "_score": 1.4054651,
        "_source": {
           "id": 1,
           "name": "Facebook",
           "emp_count": 3,
           "employees": [
              {
                 "id": 1,
                 "name": "Amber",
                 "address": {
                    "city": "Bangalore",
                    "state": "Karnataka",
                    "country": "India"
                 }
              },
              {
                 "id": 1,
                 "name": "Adrian",
                 "address": {
                    "city": "Palo Alto",
                    "state": "California",
                    "country": "United States"
                 }
              }
           ]
        },
        "inner_hits": {
           "employees": {
              "hits": {
                 "total": 1,
                 "max_score": 1.4054651,
                 "hits": [
                    {
                       "_index": "demo",
                       "_type": "company",
                       "_id": "2",
                       "_nested": {
                          "field": "employees",
                          "offset": 0
                       },
                       "_score": 1.4054651,
                       "_source": {
                          "id": 1,
                          "name": "Amber",
                          "address": {
                             "city": "Bangalore",
                             "state": "Karnataka",
                             "country": "India"
                          }
                       }
                    }
                 ]
              }
           }
        }
     }
    ]
  }

您可以看到,inner_hits部分只包含符合条件的员工。 但inner_hits是在elasticsearch 1.5.0中引入的。因此版本应该大于elasticsearch 1.5.0。您可以参考here了解更多信息。