繁体   English   中英

弹性搜索 - 有没有办法在聚合期间根据条件忽略某些文档?

[英]Elastic Search - Is there a way to ignore certain documents based on condition during aggregation?

我正在记录流的分析。 对于流开始时将字段“start”设置为“true”,流结束时将“true”设置为字段“end”。 很少有流可能不包含“结束”字段“真”。 我想找到流量完全停止的位置。

我尝试使用嵌套聚合,但无法获取非结束流的文档。

这是存储在弹性搜索中的数据

    [
    {
        "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"
    }
]

使用的查询:

 {
"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
                }
            }
        }
    }
}

}

我得到以下输出

    {
      "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
                      ]
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }

但由于流程已完成,我不想获得文档 #3、#4 和 #5。

我对弹性搜索很陌生。 由于核心人员休假,我正在尝试解决一些问题。 请指导我获取文档 #1、#2 和 #6。

我知道每个流中有 2 条消息 - 一条带有"start":true ,一条带有"end":true 为了找到只有开始但没有结束的流,您需要在每个流上都有一个唯一的标识符,比如flow-id

如果消息将包含flow-id您可以在流 ID 上运行术语聚合,以计算每个流存在多少消息,然后根据聚合结果的 _count 对结果进行升序排序 - 第一个聚合结果将计数=1,因此只有开始没有结束的流。

查询应如下所示:

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
                   }
                }
            }
          }
       }
    }
}

在这里看看一个类似的需求: Elasticsearch termaggregation and querying

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM