zoukankan      html  css  js  c++  java
  • 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"
        }
      }
    }
  • 相关阅读:
    杂题
    [ AtCoder Grand Contest 031] C
    [ AtCoder Regular Contest 107 ] C
    [ 实验舱 CSP/NOIP新赛制内部挑战赛4 ] 树上询问
    [ 2020牛客NOIP赛前集训营-提高组(第六场) ] 补题
    [NOI Online #2 提高组]子序列问题
    洛谷P4317 花神的数论题
    [AHOI2009]同类分布
    洛谷P3413 SAC#1
    数位dp小结
  • 原文地址:https://www.cnblogs.com/davidwang456/p/10078301.html
Copyright © 2011-2022 走看看