如何在elasticsearch中进行join查询?

How to do join query in elasticsearch?

我在 ES 连接查询中遇到了困难,我用谷歌搜索了很多但没有找到任何帮助。所以我在这里问一下。

1。映射

我有2个索引,其映射是:

索引日志的映射:

"logs": {
        "properties": {
            "timestamp": {"type": "date"},
            "host": {"type": "keyword"},
            "log": {"type": "text"}

}

索引版本的映射:

"versions": {
        "properties": {
            "host": {"type": "keyword"},
            "version": {"type": "keyword"}

}

2。示例数据

假设我有这样的数据:

日志数据:

timestamp:1, host:a1, log: "sample log1"
timestamp:2, host:a1, log: "sample log2"
timestamp:3, host:a1, log: "sample log3"
timestamp:1, host:a2, log: "sample log4"
timestamp:2, host:a2, log: "sample log5"
timestamp:3, host:a2, log: "sample log6"
timestamp:1, host:a3, log: "sample log7"
timestamp:2, host:a3, log: "sample log8"
timestamp:3, host:a3, log: "sample log9"

版本数据:

host:a1, version:v1
host:a2, version:v1
host:a3, version:v2

3。目的和预期结果

我想查询的是: "Find out all logs of host's version equal to v1"

结果应该是:

timestamp:1, host:a1, log: "sample log1"
timestamp:2, host:a1, log: "sample log2"
timestamp:3, host:a1, log: "sample log3"
timestamp:1, host:a2, log: "sample log4"
timestamp:2, host:a2, log: "sample log5"
timestamp:3, host:a2, log: "sample log6"

我应该怎么做?请帮忙。

Elasticsearch 不是关系型数据库,因此不支持join。

解决此问题的唯一方法是使用 parent-child (or join data type in v6) or nested docs

** 这两个选项都不是可扩展的,并且可能会引入性能问题。