• Elasticsearch使用篇 - 关联查询


    Nest数据类型

    nest 类型是对象数据类型的一种特殊版本,允许以一种可以独立查询对象数组元素的方式对对象数组进行索引。

    The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

    nest类型的官方文档介绍

    • dynamic:todo — 待更新
    • properties:todo — 待更新
    • include_in_parent:todo — 待更新
    • include_in_root:todo — 待更新

    nested类型的限制:

    // todo — 待更新

    e.g.

    PUT my-index-000001/_doc/1
    {
      "user": [
        {
          "first": "John",
          "last": "Smith"
        },
        {
          "first": "Alice",
          "last": "White"
        }
      ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    上述的插入文档的操作在内部会将其转换成如下:

    {
    	"user.first": ["john", "alice"],
    	"user.last": ["smith", "white"]
    }
    
    • 1
    • 2
    • 3
    • 4

    可以看出 user.first、user.last 字段都会平铺成多值的字段,alice、white 之间还有 john、smith 之间的关联关系会丢失。

    导致下面的查询的结果也是不准确的:

    GET my-index-000001/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "user.first": "Alice"
              }
            },
            {
              "match": {
                "user.last": "Smith"
              }
            }
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    对此,可以采用 nested 类型解决这种问题。

    PUT my-index-000001
    {
      "mappings": {
        "properties": {
          "user": {
            "type": "nested",
            "properties": {
              "first": {
                "type": "text"
              },
              "last": {
                "type": "text"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    PUT my-index-000001/_doc/1
    {
      "user": [
        {
          "first": "John",
          "last": "Smith"
        },  
        {
          "first": "Alice",
          "last": "White"
        }
      ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    GET my-index-000001/_search
    {
      "query": {
        "nested": {
          "path": "user",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "user.first": "Alice"
                  }
                },
                {
                  "match": {
                    "user.last": "White"
                  }
                }
              ]
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    Nested Query

    嵌套查询。针对 nested 类型字段的查询。

    自己的理解:前面说到 nested 类型是允许以一种可以独立查询对象数组元素的方式对对象数组进行索引,也就是说对象数组元素即子对象作为一个整体进行匹配,如果匹配成功,则返回父文档。

    nested query 的官方文档介绍

    • path:(必须)想要查询的嵌套对象

    • query:(必须)基于嵌套对象,想要查询的内容

    • score_mode:匹配的子对象集合的分数如何影响父文档的关联性分数

      • avg:(默认值)取匹配的子对象集合的分数的平均值作为父文档的关联性分数
  • 相关阅读:
    【Reinforcement Learning】策略学习
    springboot幼儿园幼儿基本信息管理系统设计与实现毕业设计源码201126
    各大电商API接口明细,API文档返回值说明
    气球派对服务小程序商城的效果是什么
    实现动态大数结构
    【科技素养】蓝桥杯STEMA 科技素养组模拟练习试卷E
    2023秋招——大数据研发工程师提前批一面
    金九银十!Java基础常见知识点&面试题总结(二),2022最新版!
    测试人员如何提交一条高质量的bug
    利用jar包构建Docker 镜像
  • 原文地址:https://blog.csdn.net/qq_34561892/article/details/128130833