是否有与Elasticsearch中的联接类似的查询? [英] Is there a query similar to joins in Elasticsearch?
问题描述
我知道在Elasticsearch中使用"JOIN"效率不高,但是我需要使用它. 我必须通过找到索引A和索引B的相同字段来提取值. 下面有一个例子.
I know its not efficient to use "JOIN" in Elasticsearch, but I need to use it. I have to extract values by find same field of index A and index B. there is an example below.
A/type1/1
{
"serial":"abc",
"member":"jack"
}
A/type1/2
{
"serial":"def",
"member":"jack"
}
B/type2/1
{
"serial":"abc",
"temp":1
}
B/type3/2
{
"serial":"abc",
"water":0
}
B/type2/3
{
"serial":"def",
"temp":10
}
我需要过滤A索引的'member'字段的值以找到对应的serial
,然后我想获取B中temp
和water
字段的值指数.
例如)过滤器:{member:jack}
===> temp:1, water:0, temp:10
I need to filter the value of the ‘member’ field of the A index to find the corresponding serial
, and then I want to get the values of the temp
and water
fields in the B index.
ex) filter: {"member":"jack"}
===> temp:1, water:0, temp:10
我想知道是否可以得到这个结果,如果可以的话,如何建立数据结构(索引结构).
I wonder if I can get this result, and if so, how do i establish the data structure (index structure).
推荐答案
如果可能的话,您绝对应该执行注释者Val的建议,对数据进行非规范化(扁平化).我建议,例如,您可以使用这样的文档(基本上,在索引之前进行连接 之前):
You should definitely do what the commenter Val suggests, denormalize (flatten) your data, if it is at all possible. I would suggest, for example, you could use documents like this (basically, do the join before indexing):
B/type2/1 {"serial": "abc", "temp": 1, "member": "jack"}
B/type2/2 {"serial": "abc", "water": 0, "member": "jack"}
B/type2/3 {"serial": "def", "temp": 10, "member": "jack"}
然后,如果您搜索{"match": {"member": "jack"}}
,则将获得所有这些文档.在Elasticsearch中,有两种方法可以完成类似"joins"的操作,嵌套对象.这是如何使用嵌套对象创建映射的示例:
Then if you search {"match": {"member": "jack"}}
, you'll get all those documents. There are two ways of doing something like "joins" in Elasticsearch, parent-child relationships and nested objects. Here's the example of how you could create your mapping with nested objects:
{
"type1": {
"properties": {
"serial": {"type": "keyword"},
"member": {"type": "keyword"},
"type2s": {
"type": "nested",
"properties": {
"temp": {"type": "integer"},
"water": {"type": "integer"}
}
}
}
}
}
然后,您将存储如下记录:
Then you would store a record like this:
{
"serial": "abc",
"member": "jack",
"type2s": [
{
"temp": 1
},
{
"water": 0
}
}
}
但是,我强烈敦促您不要这样做,除非您绝对必须这样做!一个好主意的用例很少见.它使查询数据变得更加复杂,并且效率低下(因此,随着数据规模的扩大,您将更快地遇到问题).
However, I would strongly urge you not to do this unless you absolutely have to! Use cases where this is a good idea are rare. It makes querying your data more complex, and it's inefficient (so as your data scales, you are going to have issues much sooner).
我知道复制"数据感觉不对.在关系数据库中,这将是一个可怕的实践.为了在Elasticsearch中进行有效的数据建模,您实际上必须开发一种不同的思维方式,其中的区别之一是您不必为复制数据而过分担心.
I know it feels wrong to "duplicate" data. It would be a terrible practice in a relational database. You really have to develop a different way of thinking, for effective and efficient data modeling in Elasticsearch, and one of the differences is that you shouldn't worry too much about duplicating data.
这篇关于是否有与Elasticsearch中的联接类似的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!