【问题标题】:Sub Queries in ElasticSearchElasticSearch 中的子查询
【发布时间】:2017-02-14 06:09:51
【问题描述】:

在弹性搜索中,我需要其用户 ID 是执行另一个类似这样的弹性搜索查询的结果的所有文档

{
"size": 200,
"query": {
    "bool": {
        "must": {
            "match": {
                "targetId": {
                    "query": 1234
                }
            }
        }
    }
},
"_source": {
    "includes": [
        "userId"
    ],
    "excludes": []
}
}

如果在 sql 中执行,将会有一个类似于下面的查询。


select * from mytable where userId in (select userId from mytable where targetId = 1234);

但我无法形成类似的弹性搜索查询,还有其他的吗 在elasticsearch中实现子查询的方式。

为了详细说明问题,我在下面添加了数据

"hits": [
     {
        "_index": "idx0",
        "_type": "1234",
        "_id": "1235-1486716882293",
        "_source": {
           "targetid": "42644",
           "userid": "15784334830333693",
        }
     },
     {
        "_index": "idx0",
        "_type": "1234",
        "_id": "1235-1486716882293",
        "_source": {
           "data": {
              "info":"user data available"
           },
           "userid": "15784334830333693",
        }
     },
     {
        "_index": "idx0",
        "_type": "1234",
        "_id": "1235-1486716882293",
        "_source": {
           "data": {
              "info":"user data available"
           },
           "userid": "00000034830333693",
        }
     }
  ]

从上面的数据可以看出,用户文档中的数据只包含了userid,而达到目标的用户的信息存储在另一个具有targetid和userid的文档中。

要找出达到目标的用户,我需要执行两个查询 1.使用我拥有的targetid获取userids ES, 2.从上一个查询中获取所有具有用户ID的文档。

有没有其他方法可以在单个查询中完成。

【问题讨论】:

标签: elasticsearch elasticsearch-2.0


【解决方案1】:

ES 中不允许内连接。来自 RDBMS 世界很难与之关联,但关系是速度的权衡。

ES 还有其他各种处理关系的方法,详细讨论here

【讨论】:

    【解决方案2】:

    除了原生方式之外,还可以使用SIREn Join Plugin

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2020-02-26
      相关资源
      最近更新 更多