Elasticsearch聚合嵌套内部匹配
问题描述:
我在Elasticsearch中获得了大量数据。我的douments有一个名为“记录”的嵌套字段,其中包含具有多个字段的对象列表。Elasticsearch聚合嵌套内部匹配
我希望能够从记录列表中查询特定对象,因此我在查询中使用inner_hits字段,但它没有帮助,因为聚合使用大小0,因此不会返回结果。
我没有成功地仅为inner_hits进行聚合工作,因为聚合返回记录中所有对象的结果,而不管查询。
这是我使用的查询: (每个文档都有first_timestamp和last_timestamp领域,并在记录列表中的每个对象都有一个时间戳字段)
curl -XPOST 'localhost:9200/_msearch?pretty' -H 'Content-Type: application/json' -d'
{
"index":[
"my_index"
],
"search_type":"count",
"ignore_unavailable":true
}
{
"size":0,
"query":{
"filtered":{
"query":{
"nested":{
"path":"records",
"query":{
"term":{
"records.data.field1":"value1"
}
},
"inner_hits":{}
}
},
"filter":{
"bool":{
"must":[
{
"range":{
"first_timestamp":{
"gte":1504548296273,
"lte":1504549196273,
"format":"epoch_millis"
}
}
}
],
}
}
}
},
"aggs":{
"nested_2":{
"nested":{
"path":"records"
},
"aggs":{
"2":{
"date_histogram":{
"field":"records.timestamp",
"interval":"1s",
"min_doc_count":1,
"extended_bounds":{
"min":1504548296273,
"max":1504549196273
}
}
}
}
}
}
}'
答
您的查询是非常复杂的。 短,这里是你的请求的查询:
{
"size": 0,
"aggregations": {
"nested_A": {
"nested": {
"path": "records"
},
"aggregations": {
"bool_aggregation_A": {
"filter": {
"bool": {
"must": [
{
"term": {
"records.data.field1": "value1"
}
}
]
}
},
"aggregations": {
"reverse_aggregation": {
"reverse_nested": {},
"aggregations": {
"bool_aggregation_B": {
"filter": {
"bool": {
"must": [
{
"range": {
"first_timestamp": {
"gte": 1504548296273,
"lte": 1504549196273,
"format": "epoch_millis"
}
}
}
]
}
},
"aggregations": {
"nested_B": {
"nested": {
"path": "records"
},
"aggregations": {
"my_histogram": {
"date_histogram": {
"field": "records.timestamp",
"interval": "1s",
"min_doc_count": 1,
"extended_bounds": {
"min": 1504548296273,
"max": 1504549196273
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
现在,让我来解释每一步聚集的名字:
- 大小:0 - >我们不感兴趣的命中,只集合
- nested_A - >
data.field1
是下记录,以便我们深入我们的范围,以记录 - bool_aggregation_A - >过滤器由
data.field1
:数值 - reverse_aggregation - >
first_timestamp
不是嵌套文档中,我们需要范围出从记录 - bool_aggregation_B - >过滤器由
first_timestamp
范围 - nested_B - >现在,我们范围再次成用于
timestamp
字段记录(位于下记录) - my_histogram - >最后,聚合日期直方图由
timestamp
字段
漂亮!这正是我的意思。 – hanetz