ElasticSearch _search查询带来过滤和嵌套文档

时间:2017-01-13 17:14:50

标签: scala elasticsearch nested documents

我有以下文件类型:

"_source": {
    "name": "abc",
    "email": "abc@gmail.com",
    "surname": "abcdn",
    "custom_attributes": {
        "custom1": "2bdwfwefgwef",
        "custom2": "2015-08-03 00:43:00",
        "custom3": "United States (English)"
    },
    "language": "US",
    "gender": "m"
    }

我希望过滤 名称姓氏,并且还带有 的嵌套元素过滤(在这种情况下,它是" custom_attributes")

这是我正在使用的查询;

{
  "from" : 0, "size" : 10,
  "query": {
        "filtered": {
          "filter": {
            "and" : [
               {"missing" : { "field" : "name" } },
               {"missing" : { "field" : "surname" } }
            ]
         }
        }
      }
}

但问题是它没有带来子元素(它是空的Map()而我无法弄清楚如何使用&#34;嵌套&#34;符号来带来custom_attributes < / p>

1 个答案:

答案 0 :(得分:0)

您应该使用bool查询。我尝试过以下简单示例(和动态映射):

POST my_index/my_type/
{
  "name": "a",
  "surname": "a",
  "email": "abc@gmail.com",
  "custom_attributes": {
    "custom1": "2bdwfwefgwef",
    "custom2": "2015-08-03 00:43:00",
    "custom3": "United States (English)"
  },
  "language": "US",
  "gender": "m"
}
POST my_index/my_type/
{
  "name": "b",
  "email": "abc@gmail.com",
  "custom_attributes": {
    "custom1": "2bdwfwefgwef",
    "custom2": "2015-08-03 00:43:00",
    "custom3": "United States (English)"
  },
  "language": "US",
  "gender": "m"
}
POST my_index/my_type/
{
  "surname": "c",
  "email": "abc@gmail.com",
  "custom_attributes": {
    "custom1": "2bdwfwefgwef",
    "custom2": "2015-08-03 00:43:00",
    "custom3": "United States (English)"
  },
  "language": "US",
  "gender": "m"
}
POST my_index/my_type/
{
  "email": "abc@gmail.com",
  "custom_attributes": {
    "custom1": "2bdwfwefgwef",
    "custom2": "2015-08-03 00:43:00",
    "custom3": "United States (English)"
  },
  "language": "US",
  "gender": "m"
}
GET my_index/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "name"
          }
        },
        {
          "exists": {
            "field": "surname"
          }
        }
      ]
    }
  }
}

结果是:

{
  "took": 51,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "AVmfDTiBdjT9iO0jiYdN",
        "_score": 1,
        "_source": {
          "email": "abc@gmail.com",
          "custom_attributes": {
            "custom1": "2bdwfwefgwef",
            "custom2": "2015-08-03 00:43:00",
            "custom3": "United States (English)"
          },
          "language": "US",
          "gender": "m"
        }
      }
    ]
  }
}