过滤elasticsearch 2.2中的嵌套对象无法正常工作

时间:2016-03-14 16:37:54

标签: elasticsearch

我已经阅读了一些关于嵌套对象中的查询/过滤器的文章和文档,但我无法使这个示例工作。希望你能帮我检查一下是什么问题。 Bellow是我正在使用的索引和映射设置:

  # Create Index
  PUT agency

  # Mapping
  PUT agency/site/_mapping
  {
        "site": {
            "properties": {
              "name":{
                "type":"string"
              },
              "phones": {
                 "type": "nested",
                 "properties":{
                   "is_confidential":    { "type": "string"  },
                   "number":    { "type": "string"  },
                   "description": {"type" : "string"}

                 }
              }
           }
        }

  }

  # Indexing one document
  PUT agency/site/1
  {
    "site":{
      "name":"Burger Queen",
      "phones":[
        {
          "is_confidential":"true",
          "number":"10000000000",
          "description":"Manager Phone"
        },
        {
          "is_confidential":"false",
          "number":"10000000001",
          "description":"Public Line"
        },
        {
          "is_confidential":"false",
          "number":"10000000002",
          "description":"Public Line 2"
        },
        {
          "is_confidential":"false",
          "number":"10000000003",
          "description":"Complains Phone"
        }
      ]
    }
  }


  # Query the nested document (https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html)

  GET /agency/site/_search
  {
    "query": {
      "bool": {
        "must": [
          { "match": { "site.name": "Burger" }}, 
          {
            "nested": {
              "path": "phones", 
              "query": {
                "bool": {
                  "must": [ 
                    { "match": { "phones.is_confidential": "false" }}
                  ]
          }}}}
        ]
  }}}


  # Results
  {
    "took": 4,
    "timed_out": false,
    "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
    },
    "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
    }
  }

为什么我无法获得任何结果?

我要做的是按一些术语过滤父文档,例如:name = Burger并过滤嵌套文档以仅获取is_confidential = false的手机。

在嵌套文档中未应用任何过滤器的示例结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "agency",
        "_type": "site",
        "_id": "1",
        "_score": 1,
        "_source": {
          "site": {
            "name": "Burger Queen",
            "phones": [
              {
                "is_confidential": "true",
                "number": "10000000000",
                "description": "Manager Phone"
              },
              {
                "is_confidential": "false",
                "number": "10000000001",
                "description": "Public Line"
              },
              {
                "is_confidential": "false",
                "number": "10000000002",
                "description": "Public Line 2"
              },
              {
                "is_confidential": "false",
                "number": "10000000003",
                "description": "Complains Phone"
              }
            ]
          }
        }
      }
    ]
  }
}

如果网站在手机阵列中包含多个嵌套对象,则只应由elasticsearch返回非保密手机。

应用is_confidential = false过滤器时的示例结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "agency",
        "_type": "site",
        "_id": "1",
        "_score": 1,
        "_source": {
          "site": {
            "name": "Burger Queen",
            "phones": [
              {
                "is_confidential": "false",
                "number": "10000000001",
                "description": "Public Line"
              },
              {
                "is_confidential": "false",
                "number": "10000000002",
                "description": "Public Line 2"
              },
              {
                "is_confidential": "false",
                "number": "10000000003",
                "description": "Complains Phone"
              }
            ]
          }
        }
      }
    ]
  }
}

is_confidential = true时的示例结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "agency",
        "_type": "site",
        "_id": "1",
        "_score": 1,
        "_source": {
          "site": {
            "name": "Burger Queen",
            "phones": [
              {
                "is_confidential": "true",
                "number": "10000000000",
                "description": "Manager Phone"
              }
            ]
          }
        }
      }
    ]
  }
}

是否可以使用elasticsearch嵌套过滤器(查询)获取上面显示的样本结果?如果有可能,请告诉我样品吗?

1 个答案:

答案 0 :(得分:0)

您需要更新PUT子句

索引一个文档

 PUT agency/site/1
  {
    "site":{ // <-- need to remove this as it will alter the mapping definition
      "name":"Burger Queen",
      "phones":[
        {
          "is_confidential":"true",
          "number":"10000000000",
          "description":"Manager Phone"
        },
        {
          "is_confidential":"false",
          "number":"10000000001",
          "description":"Public Line"
        },
        {
          "is_confidential":"false",
          "number":"10000000002",
          "description":"Public Line 2"
        },
        {
          "is_confidential":"false",
          "number":"10000000003",
          "description":"Complains Phone"
        }
      ]
    }
  }

GET agency/site/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "burger"
          }
        },
        {
          "nested": {
            "path": "phones",
            "query": {
              "term": {
                "phones.is_confidential": {
                  "value": "true"
                }
              }
            },
            "inner_hits":{}
          }
        }
      ]
    }
  }
}

匹配的嵌套文档将出现在内部命中响应中。

示例回复:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 2.2019112,
      "hits": [
         {
            "_index": "agency",
            "_type": "site",
            "_id": "1",
            "_score": 2.2019112,
            "_source": {
               "name": "Burger Queen",
               "phones": [
                  {
                     "is_confidential": "true",
                     "number": "10000000000",
                     "description": "Manager Phone"
                  },
                  {
                     "is_confidential": "false",
                     "number": "10000000001",
                     "description": "Public Line"
                  },
                  {
                     "is_confidential": "false",
                     "number": "10000000002",
                     "description": "Public Line 2"
                  },
                  {
                     "is_confidential": "false",
                     "number": "10000000003",
                     "description": "Complains Phone"
                  }
               ]
            },
            "inner_hits": {
               "phones": {
                  "hits": {
                     "total": 1,
                     "max_score": 1.9162908,
                     "hits": [
                        {
                           "_index": "agency",
                           "_type": "site",
                           "_id": "1",
                           "_nested": {
                              "field": "phones",
                              "offset": 0
                           },
                           "_score": 1.9162908,
                           "_source": {
                              "is_confidential": "true",
                              "number": "10000000000",
                              "description": "Manager Phone"
                           }
                        }
                     ]
                  }
               }
            }
         }
      ]
   }
}