码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearch的join查询

时间:2018-12-06 20:23:37      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:log   guid   返回   包含   doc   html   ast   fresh   mat   

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查询

标签:log   guid   返回   包含   doc   html   ast   fresh   mat   

原文地址:https://www.cnblogs.com/davidwang456/p/10078301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!