Elasticsearch Query DSL-joining queries

 

Join queries 

由于join query有一定的开销,当设置 search.allow_expensive_queries  false时不会工作。 

 

Nested 对nested类型的查询

直接看例子:

创建index mapping

PUT /drivers
{
  "mappings": {
    "properties": {
      "driver": {
        "type": "nested",
        "properties": {
          "last_name": {
            "type": "text"
          },
          "vehicle": {
            "type": "nested",
            "properties": {
              "make": {
                "type": "text"
              },
              "model": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}

创建文档 

PUT /drivers/_doc/1
{
  "driver" : {
        "last_name" : "McQueen",
        "vehicle" : [
            {
                "make" : "Powell Motors",
                "model" : "Canyonero"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
}

PUT /drivers/_doc/2?refresh
{
  "driver" : {
        "last_name" : "Hudson",
        "vehicle" : [
            {
                "make" : "Mifune",
                "model" : "Mach Five"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
}

搜索,允许多层nested query嵌套

GET /drivers/_search
{
  "query": {
    "nested": {
      "path": "driver",
      "query": {
        "nested": {
          "path": "driver.vehicle",
          "query": {
            "bool": {
              "must": [
                { "match": { "driver.vehicle.make": "Powell Motors" } },
                { "match": { "driver.vehicle.model": "Canyonero" } }
              ]
            }
          }
        }
      }
    }
  }
}

 

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.7349272,
    "hits" : [
      {
        "_index" : "drivers",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.7349272,
        "_source" : {
          "driver" : {
            "last_name" : "McQueen",
            "vehicle" : [
              {
                "make" : "Powell Motors",
                "model" : "Canyonero"
              },
              {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
              }
            ]
          }
        }
      }
    ]
  }
}

 支持参数:

path(必须, string) 查询哪个nested object.

query(必须, query object) 内嵌query

score_mode(可选, string) 指示匹配子对象的分数如何影响根文档的相关性分数。

  avg (默认)所有matching child的平均值
  max 所有matching child的最大值
  min 所有matching child的最小值
  none 0分
  sum 所有matching child相加

ignore_unmapped (可选, Boolean) 当path的对象没有映射时用返回空数据替代报错,默认false。

 

Has child 对于join类型,查询哪些parent有指定child特征的文档

has_child查询比其他查询速度慢,随着child的数量增多而查询性能下降。

看例子:

创建index mapping

PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "my-join-field": {
        "type": "join",
        "relations": {
          "parent": "child"
        }
      }
    }
  }
}

查询

GET /_search
{
  "query": {
    "has_child": {
      "type": "child",
      "query": {
        "match_all": {}
      },
      "max_children": 10,
      "min_children": 2,
      "score_mode": "min"
    }
  }
}

对child的query部分是查询条件自由发挥

has_child内的参数支持:

type(必须, string) child的字段名称。

query(必须, query object) query体

ignore_unmapped(可选, Boolean) 当对象没有映射时用返回空数据替代报错,默认false。

max_children(可选, integer) 过滤parent最多含有的child数量,超过的则过滤此parent。

min_children(可选, integer) 过滤parent至少含有的child数量,不足的则过滤此parent。

score_mode(可选, string) 指示匹配子对象的分数如何影响根文档的相关性分数。

  avg (默认)所有matching child的平均值
  max 所有matching child的最大值
  min 所有matching child的最小值
  none 0分
  sum 所有matching child相加

 

关于排序,has_child不支持标准的sort,可以使用function_score按_score排序:

GET /_search
{
  "query": {
    "has_child": {
      "type": "child",
      "query": {
        "function_score": {
          "script_score": {
            "script": "_score * doc['click_count'].value"
          }
        }
      },
      "score_mode": "max"
    }
  }
}

 

Has parent 与Has child类似

PUT /my-index-000001
{
  "mappings": {
    "properties": {
      "my-join-field": {
        "type": "join",
        "relations": {
          "parent": "child"
        }
      },
      "tag": {
        "type": "keyword"
      }
    }
  }
}
GET /my-index-000001/_search
{
  "query": {
    "has_parent": {
      "parent_type": "parent",
      "query": {
        "term": {
          "tag": {
            "value": "Elasticsearch"
          }
        }
      }
    }
  }
}
has_parent内支持的参数:

parent_type(必须, string) parent字段的名称.

query(必须, query object) query体。

score(可选, Boolean) 指示是否将匹配父文档的相关性得分聚合到其子文档中。默认为false。如果false,child的评分1。

ignore_unmapped(可选, Boolean) 当parent_type对象没有映射时用返回空数据替代报错,默认false。

 

排序:

GET /_search
{
  "query": {
    "has_parent": {
      "parent_type": "parent",
      "score": true,
      "query": {
        "function_score": {
          "script_score": {
            "script": "_score * doc['view_count'].value"
          }
        }
      }
    }
  }
}

 

Parent ID 通过parent_id查询子文档。

GET /my-index-000001/_search
{
  "query": {
      "parent_id": {
          "type": "my-child",
          "id": "1"
      }
  }
}

parent_id内支持的参数:

type(必须, string) child字段的名称.

id(必须, string) parent文档的id

ignore_unmapped(可选, Boolean) 当type对象没有映射时用返回空数据替代报错,默认false。

 

posted on 2021-11-03 15:38  icodegarden  阅读(119)  评论(0编辑  收藏  举报