[英]Elastic Search - Is there a way to ignore certain documents based on condition during aggregation?
I am logging analytics for the flow.我正在记录流的分析。 For the field "start" set to "true" when a flow starts and "true" will be set to the field "end" on flow ends.对于流开始时将字段“start”设置为“true”,流结束时将“true”设置为字段“end”。 Few flows may not contain "end" field "true".很少有流可能不包含“结束”字段“真”。 I want to find where the flow exactly stops.我想找到流量完全停止的位置。
I have tried with nested aggregation, but not able to get the documents for the flows which are not ends.我尝试使用嵌套聚合,但无法获取非结束流的文档。
This is the data stored in the elastic search这是存储在弹性搜索中的数据
[
{
"date": 1565094409535,
"start": "true",
"end": "",
"message": "Select Option",
"context": "third",
"account_id": "123"
},
{
"date": 1565094411152,
"start": "",
"end": "",
"message": "Select Me",
"context": "third",
"account_id": "123"
},
{
"date": 1565094409652,
"start": "true",
"end": "",
"message": "Select option",
"context": "third",
"account_id": "123"
},
{
"date": 1565094409751,
"start": "",
"end": "",
"message": "Select Me",
"context": "third",
"account_id": "123"
},
{
"date": 1565094411187,
"start": "",
"end": "true",
"message": "Bye Bye",
"context": "third",
"account_id": "123"
},
{
"date": 1565094411211,
"start": "true",
"end": "",
"message": "Select option",
"context": "third",
"account_id": "123"
},
{
"date": 1565094411311,
"start": "true",
"end": "",
"message": "How are you",
"context": "second",
"account_id": "123"
}
]
Query used:使用的查询:
{
"size": 0,
"query": {
"bool": {
"must": [{
"term": {
"context.keyword": "third"
}
}
]
}
},
"aggs": {
"sessions": {
"terms": {
"field": "account_id.keyword",
"size": 25000
},
"aggs": {
"top_sessions_hits": {
"top_hits": {
"sort": [{
"date": {
"order": "asc"
}
}],
"_source": {
"includes": ["date", "message", "account_id", "start", "end", "context"]
},
"size": 10000
}
}
}
}
}
} }
I am getting the below output我得到以下输出
{
"took": 37,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"sessions": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "123",
"doc_count": 6,
"top_sessions_hits": {
"hits": {
"total": 6,
"max_score": null,
"hits": [
{
"_index": "messages",
"_type": "doc",
"_id": "********",
"_score": null,
"_source": {
"date": 1565094409535,
"start": "true",
"end": "",
"message": "Select Option",
"context": "third",
"account_id": "123"
},
"sort": [
1565094409535
]
},{
"_index": "messages",
"_type": "doc",
"_id": "********",
"_score": null,
"_source": {
"date": 1565094411152,
"start": "",
"end": "",
"message": "Select Me",
"context": "third",
"account_id": "123"
},
"sort": [
1565094411152
]
},
{
"_index": "messages",
"_type": "doc",
"_id": "********",
"_score": null,
"_source": {
"date": 1565094409652,
"start": "true",
"end": "",
"message": "Select option",
"context": "third",
"account_id": "123"
},
"sort": [
1565094409652
]
},
{
"_index": "messages",
"_type": "doc",
"_id": "********",
"_score": null,
"_source": {
"date": 1565094409751,
"start": "",
"end": "",
"message": "Select Me",
"context": "third",
"account_id": "123"
},
"sort": [
1565094409751
]
},
{
"_index": "messages",
"_type": "doc",
"_id": "********",
"_score": null,
"_source": {
"date": 1565094411187,
"start": "",
"end": "true",
"message": "Bye Bye",
"context": "third",
"account_id": "123"
},
"sort": [
1565094411187
]
},
{
"_index": "messages",
"_type": "doc",
"_id": "********",
"_score": null,
"_source": {
"date": 1565094411211,
"start": "true",
"end": "",
"message": "Select option",
"context": "third",
"account_id": "123"
},
"sort": [
1565094411211
]
}
]
}
}
}
]
}
}
}
But I don't want to get the document #3, #4 & #5 since the flow was completed.但由于流程已完成,我不想获得文档 #3、#4 和 #5。
I am very new to elastic search.我对弹性搜索很陌生。 Since the core guy went on leave, I am trying to fix a few issues.由于核心人员休假,我正在尝试解决一些问题。 Please guide me to get the document #1, #2 & #6.请指导我获取文档 #1、#2 和 #6。
I understand that there are 2 messages in each flow - one with "start":true
and one with "end":true
.我知道每个流中有 2 条消息 - 一条带有"start":true
,一条带有"end":true
。 in order to find the flows that have only start, but no end, you need to have a unique identifier on each flow, like lets say flow-id
.为了找到只有开始但没有结束的流,您需要在每个流上都有一个唯一的标识符,比如flow-id
。
In case the messages will contain flow-id
you coult run a term aggregation on the flow-id, to count how many messages exists for each flow, and then sort ascending the results based on the _count of aggregation result - the first agg results would be with count=1, therefore flows which have only start and no end.如果消息将包含flow-id
您可以在流 ID 上运行术语聚合,以计算每个流存在多少消息,然后根据聚合结果的 _count 对结果进行升序排序 - 第一个聚合结果将计数=1,因此只有开始没有结束的流。
The query should look like:查询应如下所示:
GET /flows_index/_search {
"size": 0,
"aggs": {
"flow_id_agg": {
"terms": {
"field": "flow_id",
"order": {
"_count": "asc"
},
"aggs": {
"flow_id_samples": {
"top_hits": {
"sort": [{
"date": {
"order": "asc"
}
}],
"_source": {
"includes": ["date", "message", "account_id", "start", "end", "context"]
},
"size": 10000
}
}
}
}
}
}
}
Take a look at a similar need here: Elasticsearch terms aggregation and querying在这里看看一个类似的需求: Elasticsearch termaggregation and querying
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.