我有一个问题需要从 elasticsearch 获取基于 id 的文档并使用它来进行另一个查询。这行得通,但我不得不往返 elasticsearch 集群两次。我能否以某种方式在一个查询中执行此操作(例如查询 elasticsearch)并将其输出用作另一个查询的输入以避免往返?
如果您不明白这个问题,请告诉我。
最佳答案
我想借此机会宣传解决给定问题的不同方法。事实上,ElasticSearch: The Definitive Guide它自己做得很好,我只需要引用它:
Four common techniques are used to manage relational data in Elasticsearch:
- Application-side joins
- Data denormalization
- Nested objects
- Parent/child relationships
Often the final solution will require a mixture of a few of these techniques.
实践中的数据反规范化意味着数据以一种方式存储,即一个查询执行的技巧与您之前使用 2 个连续查询执行的技巧相同。
这里我展开the example从上述书中。假设您有以下两个索引,并且您希望查找任何名为 John 的人撰写的所有博客文章:
PUT /my_index/user/1
{
"name": "John Smith",
"email": "john@smith.com",
"dob": "1970/10/24"
}
PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"userID": 1
}
没有其他选择,只能先获取数据库中所有 John 的 ID。您可以做的是将一些用户信息移动到 blogpost 对象上:
PUT /my_index/user/1
{
"name": "John Smith",
"email": "john@smith.com",
"dob": "1970/10/24"
}
PUT /my_index/blogpost/2
{
"title": "Relationships",
"body": "It's complicated...",
"user": {
"id": 1,
"name": "John Smith"
}
}
因此可以在索引 blogpost
的 user.name
上进行搜索。
除了传统的 ElasticSearch 方法之外,您还可以考虑使用第三方插件,例如 Siren Join :
This join is used to filter one document set based on a second document set, hence its name. It is equivalent to the EXISTS() operator in SQL.
关于ElasticSearch:使用一个查询的输出作为另一个查询的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26993375/