仅返回对象中包含特定值的数组元素

时间:2020-04-03 13:08:08

标签: elasticsearch elasticsearch-query elasticsearch-nested

我在弹性搜索索引中获得了以下文档:

nc

现在,我正在尝试查找此文档的映射和查询,因此结果将仅包含我所插入的组和measure_names。到目前为止,我仍然可以进行查询,但是我将始终检索整个文档这是不可行的文档,因为测度的数组可能很大,而且大多数时候我想要一个小的子集。

例如,我正在搜索包含{ "type": "foo", "components": [{ "id": "1234123", , "data_collections": [{ "date_time": "2020-03-02T08:14:48+00:00", "group": "1", "group_description": "group1", "measures": [{ "measure_name": "MEASURE_1", "actual": "23.34" }, { "measure_name": "MEASURE_2", "actual": "5" }, { "measure_name": "MEASURE_3", "actual": "string_message" }, { "measure_name": "MEASURE_4", "actual": "another_string" } ] }, { "date_time": "2020-03-03T08:14:48+00:00", "group": "2", "group_description": "group2", "measures": [{ "measure_name": "MEASURE_1", "actual": "23.34" }, { "measure_name": "MEASURE_4", "actual": "foo" }, { "measure_name": "MEASURE_5", "actual": "bar" }, { "measure_name": "MEASURE_6", "actual": "4" } ] } ] } ] } "group": "1"的文档,而我想要获得的结果如下所示:

"measure_name": "MEASURE_"

我认为与我正在寻找的参数最接近的是{ "_id": "oiqwueou8931283u12", "_source": { "type": "foo", "components": [{ "id": "1234123", , "data_collections": [{ "date_time": "2020-03-02T08:14:48+00:00", "group": "1", "group_description": "group1", "measures": [{ "measure_name": "MEASURE_1", "actual": "23.34" } ] } ] } ] } } 参数,但据我所知,没有办法过滤诸如source的值

谢谢。

1 个答案:

答案 0 :(得分:2)

想到的最简单的映射是

PUT timo
{
  "mappings": {
    "properties": {
      "components": {
        "type": "nested",
        "properties": {
          "data_collections": {
            "type": "nested",
            "properties": {
              "measures": {
                "type": "nested"
              }
            }
          }
        }
      }
    }
  }
}

,搜索查询应为

GET timo/_search
{
  "_source": ["inner_hits", "type", "components.id"], 
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "components.data_collections",
            "query": {
              "term": {
                "components.data_collections.group.keyword": {
                  "value": "1"
                }
              }
            },
            "inner_hits": {}
          }
        },
        {
          "nested": {
            "path": "components.data_collections.measures",
            "query": {
              "term": {
                "components.data_collections.measures.measure_name.keyword": {
                  "value": "MEASURE_1"
                }
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

请注意,每个子查询下的inner_hits参数都受到限制,_source参数是有限的,因此我们不会返回整个匹配,而只会返回比赛。在嵌套字段中无法“看到” typecomponent.id,因此我们将其明确包含在内。

然后,响应应如下所示: enter image description here

您现在已经精确地拥有了所需的属性,因此进行一些后期处理将为您提供所需的格式!


我不熟悉使用更干净的方法来进行此操作,但是如果大家都愿意,我会很高兴学习它。

相关问题