首页 > 解决方案 > Elasticsearch,查找不匹配包含值的文档的文档

问题描述

编辑:简化了我的问题

我将事件日志流式传输到单个弹性搜索索引“日志”中。

这些事件日志中的每一个都代表某个长时间运行的进程中的单个事件。事件日志有一个状态,其值为 queued->init->running->finished。

现在我想查询我的索引以查找正在运行的作业……但如果我搜索 status="running",我还将获得稍后完成的作业。作为一个人,我会寻找带有“正在运行”事件但没有“已完成”事件的日志。

假设我必须耙树叶,打扫房子,然后吃午饭。我的事件日志看起来像:即

{job: "rake leaves" status: "queued"}
{job: "clean house" status: "queued"}
{job: "eat lunch" status: "queued"}
{job: "rake leaves" status: "started"}
{job: "rake leaves" status: "running"}
{job: "rake leaves" status: "finished"}
{job: "clean house" status: "started"}
{job: "clean house" status: "finished"}
{job: "clean house" status: "running"}
{job: "eat lunch" status: "started"}
{job: "eat lunch" status: "running"}

如何找到尚未完成的正在运行的作业?在这种情况下,吃午饭是唯一的工作。我将扩展它以查找尚未开始的排队作业。状态无关紧要。

我目前的思路是使用反向嵌套聚合来冒泡所有状态,然后从那里过滤掉我不想要的项目。我也可能会滥用带有 min_doc_count 的术语聚合器来获得我需要的东西。

例子

    curl -H "Content-Type: application/json" -XPUT "http://localhost:9200/jobs/" -d'
    {
       "mappings": {
          "event": {
             "properties": {
                "name": {
                   "type": "keyword"
                },
                "status": {
                   "type": "keyword"
                }
             }
          }
       }
    }'

    curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/jobs/_bulk" -d'
    {"index":{"_index":"jobs","_type":"event"}}
    {"name":"job0", "status":"init"}
    {"index":{"_index":"jobs","_type":"event"}}
    {"name":"job1", "status":"init"}
    {"index":{"_index":"jobs","_type":"event"}}
    {"name":"job2", "status":"init"}
    {"index":{"_index":"jobs","_type":"event"}}
    {"name":"job0", "status":"running"}
    {"index":{"_index":"jobs","_type":"event"}}
    {"name":"job1", "status":"running"}
    {"index":{"_index":"jobs","_type":"event"}}
    {"name":"job0", "status":"finished"}
    '
    curl -H "Content-Type: application/json" -XGET http://localhost:9200/logs/event/_search -d'
    {
        "aggs": {
            "duplicateNames": {
                "terms": {
                    "field": "name",
                    "min_doc_count": 2
                }
            }
        }
    }' | python -m json.tool

标签: elasticsearchelasticsearch-aggregation

解决方案


所以,说实话!我确实了解上下文和所有内容。但是,我不太明白你的问题。你想在这里实现的目标是什么?

也许在你(做得很好的解释:D)的最后把问题清楚地放在最后。:)


推荐阅读