Elastic:查询数组时如何只返回匹配的数组元素

简介: 简单数组是不能设置inner_hits的,这一点我查询了很多资料,并没有找到答案,最后咨询了业界大佬,了解到,简单数组目前并不支持这种操作

1 数组查询

ES的查询单位是doc,所以当我们查询数组时,虽然满足条件的是其中某一部分元素,但是这些元素以及这个数组都是属于这个doc的,所以会全部返回。如果我们要返回匹配的元素。那么就需要借助一些手段来实现。
查询

GET arr_json/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "match": {
          "tags.name": "ok"
        }
      }
    }
  }
}

查询结果

"hits" : [
      {
        "_index" : "arr_json",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.9808291,
        "_source" : {
          "tags" : [
            {
              "id" : 1,
              "name" : "ok"
            },
            {
              "id" : 2,
              "name" : "fine"
            },
            {
              "id" : 3,
              "name" : "right"
            }
          ]
        }
      }
    ]

2 json型数组只返回匹配的数组元素

添加数据

PUT arr_json
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "nested"
      }
    }
  }
}

PUT arr_json/_doc/1
{"tags":[
  {"id":1,"name":"ok"},
  {"id":2,"name":"fine"},
  {"id":3,"name":"right"}
  ]
}

通过inner_hit以及ignore_unmapped来实现:

GET arr_json/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "match": {
          "tags.name": "ok"
        }
      },
      "inner_hits": {
        "ignore_unmapped": false
      }
    }
  }
}

返回结果,注意要求的数据是在inner_hit中

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "arr_json",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.9808291,
        "_source" : {
          "tags" : [
            {
              "id" : 1,
              "name" : "ok"
            },
            {
              "id" : 2,
              "name" : "fine"
            },
            {
              "id" : 3,
              "name" : "right"
            }
          ]
        },
        "inner_hits" : {
          "tags" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 0.9808291,
              "hits" : [
                {
                  "_index" : "arr_json",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "tags",
                    "offset" : 0
                  },
                  "_score" : 0.9808291,
                  "_source" : {
                    "name" : "ok",
                    "id" : 1
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

3 inner_hits解读

The parent-join and nested features allow the return of documents that have matches in a different scope. In the parent/child case, parent documents are returned based on matches in child documents or child documents are returned based on matches in parent documents. In the nested case, documents are returned based on matches in nested inner objects.

In both cases, the actual matches in the different scopes that caused a document to be returned are hidden. In many cases, it’s very useful to know which inner nested objects (in the case of nested) or children/parent documents (in the case of parent/child) caused certain information to be returned. The inner hits feature can be used for this. This feature returns per search hit in the search response additional nested hits that caused a search hit to match in a different scope.

Inner hits can be used by defining an inner_hits definition on a nested, has_child or has_parent query and filter. The structure looks like this:

"<query>" : {
  "inner_hits" : {
    <inner_hits_options>
  }
}

译文:
父连接和嵌套功能允许返回在不同范围内有匹配的文档。在父/子的情况下,父文档根据子文档中的匹配结果被返回,或者子文档根据父文档中的匹配结果被返回。在嵌套的情况下,文件是根据嵌套的内部对象中的匹配来返回的。

在这两种情况下,导致文档被返回的不同作用域中的实际匹配是隐藏的。在许多情况下,知道哪个内部嵌套对象(在嵌套的情况下)或子/父文档(在父/子的情况下)导致某些信息被返回是非常有用的。内部命中特征可以用于此。这个功能在搜索响应中返回每一个导致搜索命中在不同范围内匹配的额外嵌套命中。

内部命中可以通过在一个嵌套的,has_child或者has_parent查询和过滤器上定义一个inner_hits定义来使用。该结构看起来像这样。

"<query>" : {
  "inner_hits" : {
    <inner_hits_options>
  }
}

所以可以看出,inner_hits只适用nested和join结构的数据

4 简单数组只返回匹配的数组元素

PUT test3/_doc/1
{
  "tags": [
    "ok",
    "fine",
    "right"
  ]
}

简单数组是不能设置inner_hits的,这一点我查询了很多资料,并没有找到答案,最后咨询了业界大佬,了解到,简单数组目前并不支持这种操作

如果你有任何想法,也可以留言探讨

目录
相关文章
|
20天前
|
算法 前端开发 索引
过滤数组中的元素
过滤数组中的元素
28 0
|
20天前
【力扣】28. 找出字符串中第一个匹配项的下标
【力扣】28. 找出字符串中第一个匹配项的下标
|
20天前
|
容器
06-数据容器(序列列表-元组-字符串)的切片操作
06-数据容器(序列列表-元组-字符串)的切片操作
|
20天前
|
算法 索引 Python
如何实现二分查找算法? 要求:编写一个Python函数,输入一个有序列表和一个目标值,返回目标值在列表中的索引。如果目标值不在列表中,返回-1。
如何实现二分查找算法? 要求:编写一个Python函数,输入一个有序列表和一个目标值,返回目标值在列表中的索引。如果目标值不在列表中,返回-1。
|
10月前
es6查询数组某元素出现次数
es6查询数组某元素出现次数
86 1
|
JSON 数据格式 Python
一日一技:包含非hashable元素的列表如何去重并保持顺序?
一日一技:包含非hashable元素的列表如何去重并保持顺序?
87 0
|
算法 Python
一日一技:包含元组的列表,对第一个元素升序第二个元素降序
一日一技:包含元组的列表,对第一个元素升序第二个元素降序
81 0
|
算法
LeetCode:28. 找出字符串中第一个匹配项的下标
题目描述:给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。
|
算法 Java C语言
找出字符串中第一个匹配项的下标 (LeetCode 28)
找出字符串中第一个匹配项的下标 (LeetCode 28)
183 0
统计字符串中元素的个数(多种方法)
统计字符串中元素的个数(多种方法)
142 0
统计字符串中元素的个数(多种方法)

热门文章

最新文章