1

不知道如何提出问题。我正在使用 Elasticsearch 2.2。

让我们从一个由 5 个文档组成的数据集示例开始:

[
  {
    "header": {
      "called_entity": { "uuid": "a" },
      "coverage_entity": {},
      "sucessful_transfers": 1
    }
  },
  {
    "header": {
      "called_entity": { "uuid": "a" },
      "coverage_entity": { "uuid": "b" },
      "sucessful_transfers": 1
  }
  },
  {
    "header": {
      "called_entity": { "uuid": "b" },
      "coverage_entity": { "uuid": "a" },
      "sucessful_transfers": 1
    }
  },
  {
    "header": {
      "called_entity": { "uuid": "b" },
      "coverage_entity": { "uuid": "a" },
      "sucessful_transfers": 0
    }
  }
]

called_entity总是有一个uuidcoverage_entity可以是空的,或者有一个uuid

我使用脚本在called_entity.uuidcoverage_entity.uuid上进行聚合:

{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "dim1": {
      "terms": {
        "script" : "return doc['header.called_entity.uuid'] + doc['header.coverage_entity.uuid']",
        "size": 10
      },
      "aggs": {
        "successful_transfers": {
          "sum": {
            "field": "header.successful_transfers"
          }
        }
      }
    }
  }
}

所以现在,聚合已经从header.called_entity.uuidheader.coverage_entity.uuid生成了术语。

如何使用聚合键的值过滤我的聚合?例如,如果我想计算,对于每个存储桶,有多少文档的uuid仅取自header.called_entity.uuid。像这样的东西:

{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "dim1": {
      "terms": {
        "script" : "return doc['header.called_entity.uuid'] + doc['header.coverage_entity.uuid']",
        "size": 10
      },
      "aggs": {
        "successful_transfers": {
          "sum": {
            "field": "header.successful_transfers"
          }
        },
        "from_called_entity": {
          "filter": {
            "term": { "header.called_entity.uuid": BUCKET_KEY }
          }
        }
      }
    }
  }
}
4

1 回答 1

0

不确定这是可能的。密钥本身仅可用作排序选项。

你能用这样的东西吗:

{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "dim1": {
      "terms": {
        "script": "return doc['header.called_entity.uuid'] + doc['header.coverage_entity.uuid']",
        "size": 10
      },
      "aggs": {
        "successful_transfers": {
          "sum": {
            "field": "header.sucessful_transfers"
          }
        }
      }
    },
    "called_entity_source": {
      "terms": {
        "field": "header.called_entity.uuid",
        "size": 10
      }
    },
    "coverage_entity_source": {
      "terms": {
        "field": "header.coverage_entity.uuid",
        "size": 10
      }
    }
  }
}

输出将是这样的:

  "called_entity_source": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "a",
           "doc_count": 2
        },
        {
           "key": "b",
           "doc_count": 2
        }
     ]
  },
  "coverage_entity_source": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "a",
           "doc_count": 2
        },
        {
           "key": "b",
           "doc_count": 1
        }
     ]
  },
  "dim1": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "a",
           "doc_count": 4,
           "successful_transfers": {
              "value": 3
           }
        },
        {
           "key": "b",
           "doc_count": 3,
           "successful_transfers": {
              "value": 2
           }
        }
     ]
  }

如果您确实需要以这种特定方式获取 json,请在您的应用程序中添加另一个最后一步,您可以在其中对结果进行一些后期处理。上面的结果确实包含您需要的信息,但来自聚合的键coverage_entity_sourcecalled_entity_source不在dim聚合下的键。

于 2016-07-29T14:12:09.413 回答