从弹性搜索中选择与数组匹配的对象 [英] select matching objects from array in elasticsearch

查看:184
本文介绍了从弹性搜索中选择与数组匹配的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
    class: 1,
    users: [{
        name: 'abc',
        surname: 'def'
    }, {
        name: 'xyz',
        surname: 'wef'
    }, {
        name: 'abc',
        surname: 'pqr'
    }]
}

我有一个像上面的对象和我的文档结构只想返回只有名称为abc的用户,但问题是匹配名称abc,但返回
全部数组。我只想匹配的用户。

I have a document structure like above object and I want to return only users who have name 'abc' but problem is it matches name 'abc' but returns all array. I want only matched users .

映射 -

{
        "class":"string",
        "users" : {
            "type" : "nested",
            "properties": {
                "name" : {"type": "string" },
                "surname"  : {"type": "string" }
            }
        }
    }


推荐答案

然后如果您有用户字段映射作为嵌套类型,这是一个很好的开始!

Then if you have your users field mapped as nested type, it's a good start!

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

Using nested inner_hits, you can retrieve only the matching user names with a query like this one:

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

这篇关于从弹性搜索中选择与数组匹配的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆