ElasticSearch 在同一索引内连接数据

ElasticSearch join data within the same index

我对 ElasticSearch 很陌生,我正在收集具有这种格式的同一索引中的一些应用程序日志

{
    "_index" : "app_logs",
    "_type" : "_doc",
    "_id" : "JVMYi20B0a2qSId4rt12",
    "_source" : {
      "username" : "mapred",
      "app_id" : "application_1569623930006_490200",
      "event_type" : "STARTED",
      "ts" : "2019-10-02T08:11:53Z"
}

我可以有不同的事件类型。在这种情况下,我对 STARTEDFINISHED 感兴趣。我想查询 ES 以获得在某一天开始的所有应用程序,并用结束时间丰富它们。基本上,我想创建几个 start/end (也可能缺少结尾,但这没关系)。

我已经意识到 sql 中的连接关系不能在 ES 中使用,我想知道我是否可以利用其他一些功能来在一个查询中获得这个结果。

编辑:这些是索引映射的详细信息

{ 
 “app_logs" : {
  "mappings" : {
   "_doc" : {
    "properties" : {
      "event_type" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      “app_id" : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      },
      "ts" : {
        "type" : "date"
      },
      “event_type” : {
        "type" : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
          }
        }
      }
    }
  }}}}

我的理解是,您希望整理具有与 STARTEDFINISHED 相同 app_id 以及 status 的文档列表。

我不认为 Elasticsearch 不是用来执行 JOIN 操作的。我的意思是你可以,但是你必须按照 link 中提到的那样设计你的文档。

您需要的是 Aggregation query

下面是示例映射、文档、聚合查询和响应的显示方式,这实际上可以帮助您获得所需的结果。

映射:

PUT mystatusindex
{
  "mappings": {
    "properties": {
      "username":{
        "type": "keyword"
      },
      "app_id":{
        "type": "keyword"
      },
      "event_type":{
        "type":"keyword"
      },
      "ts":{
        "type": "date"
      }
    }
  }
}

示例文档

POST mystatusindex/_doc/1
{
    "username" : "mapred",
    "app_id" : "application_1569623930006_490200",
    "event_type" : "STARTED",
    "ts" : "2019-10-02T08:11:53Z"
}

POST mystatusindex/_doc/2
{
    "username" : "mapred",
    "app_id" : "application_1569623930006_490200",
    "event_type" : "FINISHED",
    "ts" : "2019-10-02T08:12:53Z"
}

POST mystatusindex/_doc/3
{
    "username" : "mapred",
    "app_id" : "application_1569623930006_490201",
    "event_type" : "STARTED",
    "ts" : "2019-10-02T09:30:53Z"
}

POST mystatusindex/_doc/4
{
    "username" : "mapred",
    "app_id" : "application_1569623930006_490202",
    "event_type" : "STARTED",
    "ts" : "2019-10-02T09:45:53Z"
}

POST mystatusindex/_doc/5
{
    "username" : "mapred",
    "app_id" : "application_1569623930006_490202",
    "event_type" : "FINISHED",
    "ts" : "2019-10-02T09:45:53Z"
}

POST mystatusindex/_doc/6
{
  "username" : "mapred",
  "app_id" : "application_1569623930006_490203",
  "event_type" : "STARTED",
  "ts" : "2019-10-03T09:30:53Z"
}

POST mystatusindex/_doc/7
{
  "username" : "mapred",
  "app_id" : "application_1569623930006_490203",
  "event_type" : "FINISHED",
  "ts" : "2019-10-03T09:45:53Z"
}

查询:

POST mystatusindex/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "ts": {
              "gte": "2019-10-02T00:00:00Z",
              "lte": "2019-10-02T23:59:59Z"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "event_type": "STARTED"
          }
        },
        {
          "match": {
            "event_type": "FINISHED"
          }
        }
      ]
    }
  },
  "aggs": {
    "application_IDs": {
      "terms": {
        "field": "app_id"
      },
      "aggs": {
        "ids": {
          "top_hits": {
            "size": 10,
            "_source": ["event_type", "app_id"],
            "sort": [
              { "event_type": { "order": "desc"}}
              ]
          }
        }
      }
    }
  }
}

请注意,我使用了 Range Query 进行过滤,因为您只想过滤该日期的文档,并且还添加了一个 bool should 逻辑以根据 STARTED 进行过滤和 FINISHED

获得文件后,我使用 Terms Aggregation and Top Hits Aggregation 获得了想要的结果。

结果

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "application_IDs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "application_1569623930006_490200",       <----- APP ID
          "doc_count" : 2,
          "ids" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "mystatusindex",
                  "_type" : "_doc",
                  "_id" : "1",                     <--- Document with STARTED status
                  "_score" : null,
                  "_source" : {
                    "event_type" : "STARTED",     
                    "app_id" : "application_1569623930006_490200"
                  },
                  "sort" : [
                    "STARTED"
                  ]
                },
                {
                  "_index" : "mystatusindex",
                  "_type" : "_doc",
                  "_id" : "2",                    <--- Document with FINISHED status
                  "_score" : null,
                  "_source" : {
                    "event_type" : "FINISHED",     
                    "app_id" : "application_1569623930006_490200"
                  },
                  "sort" : [
                    "FINISHED"
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "application_1569623930006_490202",
          "doc_count" : 2,
          "ids" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "mystatusindex",
                  "_type" : "_doc",
                  "_id" : "4",
                  "_score" : null,
                  "_source" : {
                    "event_type" : "STARTED",
                    "app_id" : "application_1569623930006_490202"
                  },
                  "sort" : [
                    "STARTED"
                  ]
                },
                {
                  "_index" : "mystatusindex",
                  "_type" : "_doc",
                  "_id" : "5",
                  "_score" : null,
                  "_source" : {
                    "event_type" : "FINISHED",
                    "app_id" : "application_1569623930006_490202"
                  },
                  "sort" : [
                    "FINISHED"
                  ]
                }
              ]
            }
          }
        },
        {
          "key" : "application_1569623930006_490201",
          "doc_count" : 1,
          "ids" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "mystatusindex",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : null,
                  "_source" : {
                    "event_type" : "STARTED",
                    "app_id" : "application_1569623930006_490201"
                  },
                  "sort" : [
                    "STARTED"
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}

请注意,最后一个只有 STARTED 的文档也出现在聚合结果中。

更新答案

{ 
   "size":0,
   "query":{ 
      "bool":{ 
         "must":[ 
            { 
               "range":{ 
                  "ts":{ 
                     "gte":"2019-10-02T00:00:00Z",
                     "lte":"2019-10-02T23:59:59Z"
                  }
               }
            }
         ],
         "should":[ 
            { 
               "term":{ 
                  "event_type.keyword":"STARTED"   <----- Changed this 
               }
            },
            { 
               "term":{ 
                  "event_type.keyword":"FINISHED"  <----- Changed this 
               }
            }
         ]
      }
   },
   "aggs":{ 
      "application_IDs":{ 
         "terms":{ 
            "field":"app_id.keyword"               <----- Changed this 
         },
         "aggs":{ 
            "ids":{ 
               "top_hits":{ 
                  "size":10,
                  "_source":[ 
                     "event_type",
                     "app_id"
                  ],
                  "sort":[ 
                     { 
                        "event_type.keyword":{    <----- Changed this 
                           "order":"desc"
                        }
                     }
                  ]
               }
            }
         }
      }
   }
}

注意我所做的更改。每当您需要精确匹配或想要使用聚合时,您都需要使用 keyword 类型。

在您共享的映射中,没有 username 字段,只有两个 event_type 字段 .我假设这只是一个人为错误,其中一个字段应该是 username.

现在,如果您仔细观察,字段 event_type 有一个 text 及其兄弟字段 keyword。我刚刚修改了查询以使用关键字字段,当我这样做时,我使用 Term Query

试试这个,如果有帮助请告诉我!