从elasticsearch中的数组中选择匹配对象

Pra*_*ale 7 elasticsearch

{
    class: 1,
    users: [{
        name: 'abc',
        surname: 'def'
    }, {
        name: 'xyz',
        surname: 'wef'
    }, {
        name: 'abc',
        surname: 'pqr'
    }]
}
Run Code Online (Sandbox Code Playgroud)

我有一个像上面的对象的文档结构,我想只返回名称为'abc'的用户,但问题是它匹配名称'abc'但返回所有数组.我只想要匹配的用户.

映射 -

{
        "class":"string",
        "users" : {
            "type" : "nested",
            "properties": {
                "name" : {"type": "string" },
                "surname"  : {"type": "string" }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Val*_*Val 10

然后,如果您将users字段映射为nested类型,那么这是一个好的开始!

使用嵌套inner_hits,您只能使用如下所示的查询检索匹配的用户名:

{
  "_source": false,
  "query": {
    "nested": {
      "path": "users",
      "inner_hits": {        <---- this is where the magic happens
        "_source": [
          "name"
        ]
      },
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "users.name": "abc"
              }
            }
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)