elasticsearch - ElasticSearch高级聚合

标签 elasticsearch

我目前有以下结构的索引文件:

"ProductInteractions": {
    "properties": {
        "SKU": {
            "type": "string"
        },
        "Name": {
            "type": "string"
        },
        "Sources": {
            "properties": {
                "Source": {
                    "type": "string"
                },
                "Type": {
                    "type": "string"
                },
            }
        }
    }
}

在此类型上搜索时,我想汇总结果。我最初只是想要Source字段中的术语,这很容易。我只是对terms字段使用了Source聚合。

现在,我也想聚合Type字段。但是,类型与源有关。例如,我可以有两个像这样的Sources:
{
    "Source": "The Store",
    "Type": "Purchase"
}


{
    "Source": "The Store",
    "Type": "Return"
}

我想显示每个不同来源的不同类型及其数量。换句话说,我希望我的回应是这样的:
{
    "aggs": {
        "Sources": [
            {
                "Key": "The Store",
                "DocCount": 2,
                "Aggregations": {
                    "Types": [
                        {
                            "Key": "Purchase",
                            "DocCount": 1
                        },
                        {
                            "Key": "Return",
                            "DocCount": 1
                        }
                    ]
                }
            }
        ]
    }
}

有没有办法获得这些子集合?

最佳答案

是的,有,但是您需要稍微更改映射以使字段“not_analyzed”

"ProductInteractions": {
    "properties": {
        "SKU": {
            "type": "string"
        },
        "Name": {
            "type": "string"
        },
        "Sources": {
            "properties": {
                "Source": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "Type": {
                    "type": "string",
                    "index": "not_analyzed"
                },
            }
        }
    }
}

然后,您可以使用以下聚合来获得所需的内容:
{
  "aggs": {
    "sources": {
      "terms": {
        "field": "Sources.Source"
      },
      "aggs": {
        "types": {
          "terms": {
            "field": "Sources.Type"
          }
        }
      }
    }
  }
}

关于elasticsearch - ElasticSearch高级聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704092/

相关文章:

amazon-web-services - 如何在速度模板中使用字符串替换(AWS appsync + elasticsearch)?

elasticsearch - ElasticSearch的策展人可以使用 'atomic alias move'滚动索引吗?

elasticsearch - 如何将Jenkins的工作排队时间发送到Elasticsearch?

html - Elasticsearch作为图像服务器与Apache

elasticsearch - ElasticSearch忽略只有一个字母的单词

c# - ElasticSearch 7.1删除索引C#API

amazon-web-services - Elasticsearch-按多个数组中数组匹配的分数排序

mongodb - Elasticsearch MongoDb河引用处理

elasticsearch - 最新的Elasticsearch 7.x是否支持在不同节点中的集群之间滚动

elasticsearch - 在NiFi中使用 “ScrollElasticSearchHttp”处理器时出错