我正在尝试为我正在从事的项目探索 Elasticsearch ,但坚持如何加入两种类型的文档。
例如,如果我有 10 个文件是酒店可用性价格,还有 10 个文件都是飞往酒店所在目的地的航类。
通常在 MySQL 中,我会根据日期、酒店和航类的持续时间等进行连接。
我将如何返回一个包含 10 个可用航类中最便宜航类的酒店文件?
最佳答案
我能想到做你想做的最接近的事情是Composite Aggregations .它不是真正的连接,而是 可以 让你接近你想要的。
规定:
这是一个最小的示例(在 Kibana 控制台中被破解):
使用文档:
POST my-test1/_doc/_bulk
{"index": {}}
{"entityID":"entity1", "value": 12}
{"index": {}}
{"entityID":"entity1", "value": 22}
{"index": {}}
{"entityID":"entity2", "value": 2}
{"index": {}}
{"entityID":"entity2", "value": 12}
POST my-test2/_doc/_bulk
{"index": {}}
{"entityID":"entity1", "otherValue": 5}
{"index": {}}
{"entityID":"entity1", "otherValue": 1}
{"index": {}}
{"entityID":"entity2", "otherValue": 3}
{"index": {}}
{"entityID":"entity2", "otherValue": 7}
我们将围绕公共(public)实体字段
entityID
进行聚合GET my-test*/_search
{
"size": 0,
"aggs": {
"by-entity": {
"composite": {
"sources": [
{
"entityID": {
"terms": {
"field": "entityID.keyword"
}
}
}
]
},
"aggs": {
"value": {
"avg": {
"field": "value"
}
},
"otherValue": {
"avg": {
"field": "otherValue"
}
}
}
}
}
}
这将导致响应:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"by-entity" : {
"after_key" : {
"entityID" : "entity2"
},
"buckets" : [
{
"key" : {
"entityID" : "entity1"
},
"doc_count" : 4,
"otherValue" : {
"value" : 3.0
},
"value" : {
"value" : 17.0
}
},
{
"key" : {
"entityID" : "entity2"
},
"doc_count" : 4,
"otherValue" : {
"value" : 5.0
},
"value" : {
"value" : 7.0
}
}
]
}
}
}
您可以围绕许多不同的字段和不同的存储桶聚合创建复合聚合。因此,您可以创建一个
terms
为您的 hotel_id
聚合并将其与 date_histogram
结合使用围绕您的timestamp
.
关于elasticsearch - Elasticsearch - 如何在两种类型之间连接数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075886/