【问题标题】:How to perform JOIN operation in elasticsearchelasticsearch中如何进行JOIN操作
【发布时间】:2016-07-11 15:42:43
【问题描述】:

如何在elasticsearch中对同一个索引进行JOIN操作?

这是每个文档的字段集:

      "@version": "1",
      "@timestamp": "2016-04-26T15:56:05.379Z",
      "phone": "..."
      "path": "...",
      "host": "...",
      "type": "...",
      "clientip": "...",
      "ident": "-",
      "auth": "-",
      "timestamp": "...",
      "verb": "...",
      "uripath": "...",
      "httpversion": "1.1",
      "response": "200",
      "bytes": "515",
      "timetaken": "383",
      "event_type": "type1"
    }

如果我想获得 type1event_typetimestamptimestamp 之间的文档的 phone em>date1 和 date2) 和 (event_type of type2, timestamp 介于 date3 date4)

在 mysql 中认为是两个视图之间的连接

【问题讨论】:

标签: join elasticsearch querydsl


【解决方案1】:

我可能不是最优化的请求,但它确实有效:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "timestamp ": {
                        "lte": date1,
                        "gte": date2
                      }
                    }
                  },
                  {
                    "term": {
                      "event_type ": "type1"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "timestamp ": {
                        "lte": date3,
                        "gte": date4
                      }
                    }
                  },
                  {
                    "term": {
                      "event_type ": "type2"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

它将返回所有 event_type 为 type1、timestamp 在 date1 和 date2 之间的文档以及所有 event_type 为 type2、timestamp 在 date3 和 date4 之间的文档。

bool should 将返回尊重其任何部分的所有文档。

【讨论】:

  • 这就像(type1的event_type,date1和date2之间的时间戳)或(type2的event_type,date3和date4之间的时间戳)
  • @M_M 我会添加细节,但如果你测试过,我想知道细节。
  • @M_M 是的,这是具有(type1 的 event_type,date1 和 date2 之间的时间戳)或(type2 的 event_type,date3 和 date4 之间的时间戳)的文档,但是使用 AND,你不会有有结果吗?
  • 如果它们是由同一个用户执行的?就像你想添加一个接近的地方,像这样
  • select id from (select * from A where event_type = type1, date1 < timestamp < date2) join (select * from B where event_type = type2, date3 < timestamp < date4) on a.user = b.user
猜你喜欢
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多