elastic search join/scripted 查询,使用子查询的输出

elastic search join/scripted query ,using output of subquery

我有一种情况要在 elasticsearch 中编写具有如下数据的搜索查询

{id:"p1",person:{name:"name",age:"12"},relatedTO:{id:"p2"}}
{id:"p2",person:{name:"name2",age:"15"},relatedTO:{id:"p3"}}
{id:"p3",person:{name:"name3",age:"17"},relatedTO:{id:"p1"}}

场景:- 用户想要搜索与 p2 相关的人,并使用每个相关的人找到与他们相关的人

1.first 查找与 p2 answer= p1 相关的人
2.Now 找到与 p1 answer=p3 相关的人。 (现在的要求是只去1级) 所以不用找相关的人 p3.the 最终结果应该是p2,p1,p3。
正常情况下,我们将编写一个嵌套的 sql 来获取 results.How 我们是否使用弹性查询语言一次性实现此目的

一次拍摄需要使用 Parent-Child-Relationships, but I wouldn't recommand it to you in the first place, because it is not very performant. Btw: also Grandparents and Grandchildren 受支持。

您也可以使用 Application Side Joins - 这意味着您执行多个查询,直到获得您想要的结果。 (请注意,第一个结果集应该非常小,否则成本会很高)


我真正向您推荐的是阅读此 docu 并重新考虑您的用例。 如果您想像在 facebook 或 google+ 中建立关系模型,我会告诉您寻找 NoSQL Graph Database.


注意:理想情况下,在 Elasticsearch 中数据是平坦的,这意味着 denormalized