elasticsearch的join查询

本文深入探讨了Elasticsearch中的关联查询技术,包括嵌套查询、has_child、has_parent和parent_id查询。通过实例展示了如何使用这些查询来提高搜索效率和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"
    }
  }
}

 

转载于:https://www.cnblogs.com/davidwang456/p/10078301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值