当前位置: 首页 > 面试题库 >

Elasticsearch指标聚合:数组中的元素数

萧琛
2023-03-14
问题内容

我想做一个相当复杂的查询/聚合。我看不到该怎么做,因为我刚刚开始使用ES。我的文档看起来像这样:

{
  "keyword": "some keyword",
  "items": [
    {
      "name":"my first item",
      "item_property_1":"A",
      ( other properties here )
    },
    {
      "name":"my second item",
      "item_property_1":"B",
      ( other properties here )
    },
    {
      "name":"my third item",
      "item_property_1":"A",
      ( other properties here )
    }
  ]
  ( other properties... )
},
{
  "keyword": "different keyword",
  "items": [
    {
      "name":"cool item",
      "item_property_1":"A",
      ( other properties here )
    },
    {
      "name":"awesome item",
      "item_property_1":"C",
      ( other properties here )
    },
  ]
  ( other properties... )
},
( other documents... )

现在,我想为每个关键字计算property_1可以具有的几个可能值中有多少个。也就是说,我需要一个具有以下响应的存储桶聚合:

{
  "keyword": "some keyword",
  "item_property_1_aggretation": [
    {
      "key":"A",
      "count": 2,
    },
    {
      "key":"B",
      "count": 1,
    }
  ]
},
{
  "keyword": "different keyword",
  "item_property_1_aggretation": [
    {
      "key":"A",
      "count": 1,
    },
    {
      "key":"C",
      "count": 1,
    }
  ]
},
( other keywords... )

如果需要映射,您还可以指定哪个吗?我没有任何非默认映射,我只是将所有内容都转储在那里。

编辑:通过在此处发布上一个示例的批量PUT为您节省了麻烦

PUT /test/test/_bulk
{ "index": {}}
{  "keyword": "some keyword",  "items": [    {      "name":"my first item",      "item_property_1":"A"    },    {      "name":"my second item",      "item_property_1":"B"    },    {      "name":"my third item",      "item_property_1":"A"     }  ]}
{ "index": {}}
{  "keyword": "different keyword",  "items": [    {      "name":"cool item",      "item_property_1":"A"    },    {      "name":"awesome item",      "item_property_1":"C"    }  ]}

编辑2:

我只是试过这个:

POST /test/test/_search
{
    "size":2,
    "aggregations": {
        "property_1_count": {
            "terms":{
                "field":"item_property_1"
            }
        }
    }
}

并得到了这个:

"aggregations": {
   "property_1_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
         {
            "key": "a",
            "doc_count": 2
         },
         {
            "key": "b",
            "doc_count": 1
         },
         {
            "key": "c",
            "doc_count": 1
         }
      ]
   }
}

关闭但没有雪茄。您可以看到发生了什么,item_property_1无论keyword它们属于哪个,它都在进行存储。我确定该解决方案涉及正确添加一些映射,但是我无法全力以赴。有什么建议吗?

EDIT3:基于此:https ://www.elastic.co/guide/zh-
cn/elasticsearch/reference/current/mapping-nested-type.html
我想尝试将一个nested类型添加到property items。为此,我尝试:

PUT /test/_mapping/test
{
    "test":{
        "properties": {
            "items": {
                "type": "nested",
                "properties": {
                    "item_property_1":{"type":"string"}
                }
            }
        }
    }
}

但是,这将返回错误:

{
   "error": "MergeMappingException[Merge failed with failures {[object mapping [items] can't be changed from non-nested to nested]}]",
   "status": 400
}

这可能与该URL上的警告有关:“将对象类型更改为嵌套类型需要重新索引。”

那么,我该怎么做呢?


问题答案:

不错的尝试,您快到了!这是我想出的。根据您的映射建议,我正在使用的映射如下:

curl -XPUT localhost:9200/test/_mapping/test -d '{
  "test": {
    "properties": {
      "keyword": {
        "type": "string",
        "index": "not_analyzed"
      },
      "items": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "string"
          },
          "item_property_1": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
}'

注意:您需要擦除数据并重新编制索引,因为您无法将字段类型从不是更改nestednested

然后,我使用您共享的批量查询创建了一些数据:

curl -XPOST localhost:9200/test/test/_bulk -d '
{ "index": {}}
{  "keyword": "some keyword",  "items": [    {      "name":"my first item",      "item_property_1":"A"    },    {      "name":"my second item",      "item_property_1":"B"    },    {      "name":"my third item",      "item_property_1":"A"     }  ]}
{ "index": {}}
{  "keyword": "different keyword",  "items": [    {      "name":"cool item",      "item_property_1":"A"    },    {      "name":"awesome item",      "item_property_1":"C"    }  ]}
'

最后,这是可用于获取期望结果的聚合查询。我们首先keyword使用terms聚合来进行存储,然后针对每个关键字通过嵌套item_property_1字段进行存储。由于items现在是一个nested类型的,关键是用nested聚合的items,然后一个terms子聚集的item_property_1领域。

{
  "size": 0,
  "aggregations": {
    "by_keyword": {
      "terms": {
        "field": "keyword"
      },
      "aggs": {
        "prop_1_count": {
          "nested": {
            "path": "items"
          },
          "aggs": {
            "prop_1": {
              "terms": {
                "field": "items.item_property_1"
              }
            }
          }
        }
      }
    }
  }
}

在您的数据集上运行该查询将产生以下结果:

{
  ...
  "aggregations" : {
    "by_keyword" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "different keyword",       <---- keyword 1
        "doc_count" : 1,
        "prop_1_count" : {
          "doc_count" : 2,
          "prop_1" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {                <---- buckets for item_property_1
              "key" : "A",
              "doc_count" : 1
            }, {
              "key" : "C",
              "doc_count" : 1
            } ]
          }
        }
      }, {
        "key" : "some keyword",            <---- keyword 2
        "doc_count" : 1,
        "prop_1_count" : {
          "doc_count" : 3,
          "prop_1" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [ {                <---- buckets for item_property_1
              "key" : "A",
              "doc_count" : 2
            }, {
              "key" : "B",
              "doc_count" : 1
            } ]
          }
        }
      } ]
    }
  }
}


 类似资料:
  • 如何编写Elasticsearch术语聚合,将存储桶按整个术语而不是单个标记拆分?例如,我想按州进行聚合,但以下返回的是纽约、纽约、泽西和加利福尼亚作为单个桶,而不是纽约、新泽西和加利福尼亚作为预期的桶: 我的用例就像这里描述的那样https://www.elastic.co/guide/en/elasticsearch/guide/current/aggregations-and-analysi

  • 我做了一个代码,应该显示数组中元素排列的整个组合。 应该是什么: 123 213 231 132 312 321 但结果是这样的: 231 312 123 231 312 123 如何以应有的方式进行排列?

  • 问题内容: 我正在尝试对数组中的值进行聚合,并且还过滤由前缀返回的存储桶。不知道这是否可行,或者我滥用过滤桶。 3份文件: 目的是获取带有字母B开头颜色的文档数量: 不幸的是,返回的结果包括Red。显然是因为带有红色的文档仍然按过滤器匹配,因为它们也具有蓝色和/或黑色。 有没有一种方法可以只过滤存储桶结果? 问题答案: 尝试此操作,它将过滤为存储桶本身创建的值:

  • 我必须在Elasticsearch中构造一个非常重要的查询(现在看来是这样)。假设我有两个实体,每个实体都有一个数组元素,由字符串组成: 数组元素的映射如下(使用动态模板): 实体的Json表示如下: 然后我有了用户输入:['A','B','C']。 我想要实现的是找到只包含输入中指定元素的实体——预期结果是:[A'、[B']、[A'、[C']、[A'],但不是['A'、[E'](因为用户输入中不

  • 本文向大家介绍如何在MongoDB聚合中将数组元素的子集相加?,包括了如何在MongoDB聚合中将数组元素的子集相加?的使用技巧和注意事项,需要的朋友参考一下 要将数组元素的子集加在一起,请使用$first和$sum。让我们创建一个包含文档的集合- 在find()方法的帮助下显示集合中的所有文档- 这将产生以下输出- 这是将MongoDB聚合中数组元素的子集加在一起的查询- 这将产生以下输出-

  • 问题内容: 我想按地址分组,然后按日期获取最新地址,然后按状态过滤此结果。 我在elasticsearch中有此查询,但它最多只能按地址分组并获取最新日期。我无法按状态过滤此结果。 我想使用Elasticsearch从该结果中获得出售状态 问题答案: 使用ES 使用ES可以做到这一点。首先,我们需要汇总地址。然后,我们使用两种汇总,一种汇总获取最新日期,另一种汇总获取销售状态的最新日期。然后,我们