将查询结果拆分为多个结果

时间:2015-12-24 17:58:44

标签: elasticsearch querydsl

我目前有以下查询:

{
    "size": 0,
    "query": {
        "bool": {
            "should": [
                {
                    "query": {
                        "type": {
                            "value": "ods"
                        }
                    }
                },
                {
                    "query": {
                        "type": {
                            "value": "pds"
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "total_per_day": {
            "date_histogram": {
                "field": "createdAt",
                "interval": "day"
            },
            "aggs": {
                "cumulative": {
                    "cumulative_sum": {
                        "buckets_path": "_count"
                    }
                }
            }
        }
    }
}

这会产生很好的结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 6,
    "failed": 0
  },
  "hits": {
    "total": 12425,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "total_per_day": {
      "buckets": [
        {
          "key_as_string": "1450828800",
          "key": 1450828800000,
          "doc_count": 1379,
          "cumulative": {
            "value": 1379
          }
        },
        {
          "key_as_string": "1450915200",
          "key": 1450915200000,
          "doc_count": 11046,
          "cumulative": {
            "value": 12425
          }
        }
      ]
    }
  }
}

然而,这给了我一个"总计"匹配类型odspds的所有文档的结果(我打算首先考虑)。在对此进行可视化时,我实际上并没有清楚地了解它的构造(有多少pds个文档和多少ods个文档)。

我修改过的查询如下:

{
    "size": 0,
    "query": {
        "bool": {
            "should": [
                {
                    "query": {
                        "type": {
                            "value": "ods"
                        }
                    }
                },
                {
                    "query": {
                        "type": {
                            "value": "pds"
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "total_per_day": {
            "date_histogram": {
                "field": "createdAt",
                "interval": "day"
            },
            "aggs": {
                "types": {
                    "terms": {
                        "field": "_type"
                    }
                },
                "cumulative": {
                    "cumulative_sum": {
                        "buckets_path": "_count"
                    }
                }
            }
        }
    }
}

产生以下结果

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 6,
    "failed": 0
  },
  "hits": {
    "total": 12963,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "total_per_day": {
      "buckets": [
        {
          "key_as_string": "1450828800",
          "key": 1450828800000,
          "doc_count": 1379,
          "types": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "pds",
                "doc_count": 834
              },
              {
                "key": "ods",
                "doc_count": 545
              }
            ]
          },
          "cumulative": {
            "value": 1379
          }
        },
        {
          "key_as_string": "1450915200",
          "key": 1450915200000,
          "doc_count": 11584,
          "types": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "pds",
                "doc_count": 5840
              },
              {
                "key": "ods",
                "doc_count": 5744
              }
            ]
          },
          "cumulative": {
            "value": 12963
          }
        }
      ]
    }
  }
}

这已经让我对我的数据有了更好的了解,但是,我还希望每个odspds类型的累积计数。这甚至可能吗?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您正在寻找filters aggregation,您可以为此pds{ "size": 0, "aggs": { "type_buckets": { "filters": { "filters": { "ods_type": { "term": { "_type": "ods" } }, "pds_type": { "term": { "_type": "pds" } } } }, "aggs": { "users_per_day": { "date_histogram": { "field": "createdAt", "interval": "day" }, "aggs": { "cumulative": { "cumulative_sum": { "buckets_path": "_count" } } } } } } } } 创建单独的存储桶

filter

编辑1 :执行相同操作的另一种方法是sub-aggregation{ "size": 0, "aggs": { "ods_type": { "filter": { "term": { "_type": "ods" } }, "aggs": { "users_per_day": { "date_histogram": { "field": "created_at", "interval": "month" }, "aggs": { "cumulative": { "cumulative_sum": { "buckets_path": "_count" } } } } } }, "pds_type": { "filter": { "term": { "_type": "pds" } }, "aggs": { "users_per_day": { "date_histogram": { "field": "created_at", "interval": "month" }, "aggs": { "cumulative": { "cumulative_sum": { "buckets_path": "_count" } } } } } } } } 这样

IEnumerable<Account> accounts = context.Accounts;
foreach(Account accout = accounts)
  account.Number = Guid.Empty;
return accounts;

这有帮助吗?