首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastic Search:特定字段上的聚合和

Elastic Search:特定字段上的聚合和
EN

Stack Overflow用户
提问于 2016-08-19 14:12:04
回答 2查看 1.1K关注 0票数 1

我对elastic search是个新手,正在寻求一些帮助。基本上,我的弹性搜索中有大约200万个文档,这些文档如下所示:

代码语言:javascript
复制
{
  "_index": "flipkart",
  "_type": "PSAD_ThirdParty",
  "_id": "430001_MAM_2016-02-04",
  "_version": 1,
  "_score": 1,
  "_source": {
    "metrics": [
      {
        "id": "Metric1",
        "value": 70
      },
      {
        "id": "Metric2",
        "value": 90
      },
      {
        "id": "Metric3",
        "value": 120
      }
    ],
    "primary": true,
    "ticketId": 1,
    "pliId": 206,
    "bookedNumbers": 15000,
    "ut": 1454567400000,
    "startDate": 1451629800000,
    "endDate": 1464589800000,
    "tz": "EST"
  }
}

我想写一个满足以下条件的聚合查询:

1)首先根据"_index""_type""pliId"进行查询。2)基于metrics.id = "Metric1"对metrics.value进行聚合求和。

基本上,我需要根据一些字段查询记录,并根据指标id对特定指标值进行汇总。请你能帮我把我的问题弄对吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-19 16:11:56

metrics字段的类型必须为nested

代码语言:javascript
复制
    "metrics": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }

如果您希望Metric1匹配,也就是大写字母,那么正如您在上面看到的,id需要是not_analyzed

然后,如果你只想要metrics.id = "Metric1"聚合,你需要这样的东西:

代码语言:javascript
复制
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "pliId": 206
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "by_metrics": {
      "nested": {
        "path": "metrics"
      },
      "aggs": {
        "metric1_only": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "metrics.id": {
                      "value": "Metric1"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "by_metric_id": {
              "terms": {
                "field": "metrics.id"
              },
              "aggs": {
                "total_delivery": {
                  "sum": {
                    "field": "metrics.value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
票数 1
EN

Stack Overflow用户

发布于 2016-08-19 19:15:57

已创建新索引:方法: PUT,URL:http://localhost:9200/google/

正文:

代码语言:javascript
复制
    {
      "mappings": {
        "PSAD_Primary": {
          "properties": {
            "metrics": {
              "type": "nested",
              "properties": {
            "id": {
              "type": "string",
              "index": "not_analyzed"
            },
            "value": {
              "type": "integer",
              "index": "not_analyzed"
            }
          }
            }
          }
        }
      }
    }

然后我插入了大约200,000个文档,然后运行查询,它起作用了。

响应:

代码语言:javascript
复制
{
  "took": 34,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "google",
        "_type": "PSAD_Primary",
        "_id": "383701291_MAM_2016-01-06",
        "_score": 1,
        "_source": {
          "metrics": [
            {
              "id": "Metric1",
              "value": 70
            },
            {
              "id": "Metric2",
              "value": 90
            },
            {
              "id": "Metric3",
              "value": 120
            }
          ],
          "primary": true,
          "ticketId": 1,
          "pliId": 221244,
          "bookedNumbers": 15000,
          "ut": 1452061800000,
          "startDate": 1451629800000,
          "endDate": 1464589800000,
          "tz": "EST"
        }
      }
    ]
  },
  "aggregations": {
    "by_metrics": {
      "doc_count": 3,
      "metric1_only": {
        "doc_count": 1,
        "by_metric_id": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "Metric1",
              "doc_count": 1,
              "total_delivery": {
                "value": 70
              }
            }
          ]
        }
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39032199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档