代码之家  ›  专栏  ›  技术社区  ›  Anil Bhaskaran

弹性搜索从数组字段中选取元素

  •  0
  • Anil Bhaskaran  · 技术社区  · 6 年前

    我有以下弹性搜索格式的数据。

    {
    "fname": "FN1",
      "lname": "LN1",
      "images": [
        {
          "type": "passport",
          "url": ".."
        },
        {
          "type": "full",
          "url": ".."
        },
        {
          "type": "full",
          "url": ".."
        }
        ]
    }
    

    在从弹性搜索获取数据时,是否可以从数组字段中有选择地选择项,例如,是否可以在图像数组中仅包含type=“passport”的对象?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nishant    6 年前

    Nested inner hits 可以来救你,但顾名思义,你处理的字段的数据类型应该是 nested . 假设字段图像的数据类型是 嵌套的 ,您可以根据 images.type 以及杠杆作用 nested_hits 只返回匹配的嵌套文档。

    因此,查询结果如下:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "nested": {
                "path": "images",
                "query": {
                  "term": {
                    "images.type": "passport"
                  }
                },
                "inner_hits": {}
              }
            }
          ]
        }
      }
    }