php - 在Elasticsearch中使用嵌套文档聚合多个存储桶

标签 php json elasticsearch nested

我目前正在研究Elasticsearch项目。我想汇总现有文档中的数据。

(简化的)结构如下:

{
  "products" : {
    "mappings" : {
      "product" : {
        "properties" : {
          "created" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss"
          },
          "description" : {
            "type" : "text"
          },
          "facets" : {
            "type" : "nested",
            "properties" : {
              "facet_id" : {
                "type" : "long"
              }
              "name_slug" : {
                "type" : "keyword"
              },
              "value_slug" : {
                "type" : "keyword"
              }
            }
          },
       }
      }
    }
   }
}

想要我想用一个查询实现:
  • 选择唯一的facet_name值
  • 在facet_names下,我想要所有对应的facet_values

  • 像这样:
    - facet_name
    -- facet_sub_value (counter?)
    -- facet_sub_value (counter?)
    -- facet_sub_value (counter?)
    - facet_name
    -- facet_sub_value (counter?)
    -- facet_sub_value (counter?)
    -- facet_sub_value (counter?)
    

    你们能指出我正确的方向吗?我看过aggs查询,但是文档不够清楚,不足以实现这一点。

    最佳答案

    您将使用nested terms aggregations。由于构面名称和值位于同一路径下,因此您可以尝试以下操作:

    GET products/_search
    {
      "size": 0,
      "aggs": {
        "by_facet_names_parent": {
          "nested": {
            "path": "facets"
          },
          "aggs": {
            "by_facet_names_nested": {
              "terms": {
                "field": "facets.name_slug",
                "size": 10
              },
              "aggs": {
                "by_facet_subvalues": {
                  "terms": {
                    "field": "facets.value_slug",
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
    

    而且您的响应应该类似于以下内容:
    {
      "took": 26,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 30,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "by_facet_names_parent": {
          "doc_count": 90,
          "by_facet_names_nested": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 80,
            "buckets": [
              {
                "key": "0JDcya7Y7Y",     <-------- your facet name keyword
                "doc_count": 4,
                "by_facet_subvalues": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "3q4E9R6h5k",    <-------- one of the facet values + its count
                      "doc_count": 3
                    },
                    {
                      "key": "1q4E9R6h5k",   <-------- another facet value & count
                      "doc_count": 1
                    }
                  ]
                }
              },
              {
                "key": "0RyRKWugU1",
                "doc_count": 1,
                "by_facet_subvalues": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": "Af7qeCsXz6",
                      "doc_count": 1
                    }
                  ]
                }
              }
              .....
            ]
          }
        }
      }
    }
    

    请注意,嵌套存储区的数量可能>> =您实际产品文档的数量。这是因为嵌套聚合将嵌套子文档视为separate documents within the parent documents。这需要一些时间来消化,但是当您与它们一起玩足够长的时间时,这才有意义。

    关于php - 在Elasticsearch中使用嵌套文档聚合多个存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60194062/

    相关文章:

    python - Elasticsearch:如何找到未分配的分片并分配它们?

    apache-spark - 我的 spark sql 限制很慢

    php - 在字符串中查找 HTML 标签

    java - 即使正确的JSON JAVA Volley,凭证响应也无效

    java - 比较Json Array的对象并显示具有相同参数的对象

    iphone - 使用 JSON 问题在 UITableView 中导航缓慢

    elasticsearch - 向 Elasticsearch 服务器发送 curl 请求时,不支持错误application/x-www-form-urlencoded

    php - YII : How to Change datetime format displayed on the View

    php - 如果已经使用 FILTER_VALIDATE_EMAIL,FILTER_SANITIZE_EMAIL 是否毫无意义?

    c# - Active Directory OU 树到 jqTree