elasticsearch的join查询
Posted davidwang456
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch的join查询相关的知识,希望对你有一定的参考价值。
1.概述
官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/current/joining-queries.html
两种类型的查询
嵌套查询
has_child和has_parent
其中,has_child返回包含特定查询字段文档的父文档;
has_parent返回包含特定查询字段的父文档的子文档。
2.实例
2.1 嵌套查询
GET /_search { "query": { "nested" : { "path" : "obj1", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "match" : {"obj1.name" : "blue"} }, { "range" : {"obj1.count" : {"gt" : 5}} } ] } } } } }
2.2 has_child
查询
GET /_search { "query": { "has_child" : { "type" : "blog_tag", "query" : { "term" : { "tag" : "something" } } } } }
2.3 has_parent
GET /_search { "query": { "has_parent" : { "parent_type" : "blog", "query" : { "term" : { "tag" : "something" } } } } }
2.4 parent_id
查询
PUT my_index { "mappings": { "_doc": { "properties": { "my_join_field": { "type": "join", "relations": { "my_parent": "my_child" } } } } } } PUT my_index/_doc/1?refresh { "text": "This is a parent document", "my_join_field": "my_parent" } PUT my_index/_doc/2?routing=1&refresh { "text": "This is a child document", "my_join_field": { "name": "my_child", "parent": "1" } }
GET /my_index/_search { "query": { "parent_id": { "type": "my_child", "id": "1" } } }
以上是关于elasticsearch的join查询的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch object nested join 数据类型
Elasticsearch es join 多表关联如何设计