【发布时间】: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的文档。
有没有其他方法可以在单个查询中完成。
【问题讨论】:
-
你可能会使用terms lookup mechanism
标签: elasticsearch elasticsearch-2.0