ES中nest join

付威     2019-02-25   3334   9min  

nested 类型是一个特殊object数据类型,允许数组的object的字段可以被独立的查询出来。

数据类型是如何被封装的

在lucene中没有嵌套object的概念,所以ES的用一个简单的数据数据列表来表示一个复杂的层次数据实体,例如一个博客和评论的实体:

PUT  nesttest/_doc
{
    "blog_title": "开篇-es的nest的使用",
    "blog_content": "从本篇文章开始,我会分享给大家canvas绘制的各种基础图形和酷炫的图形",
    "tags": [
        "Java",
        "es"
    ],
    "hit_count": 5,
    "commet": [
        {
            "commet_user": "tom",
            "commet_content": "good Job",
            "commet_time": "2019-02-26",
             "commet_location": "beijing"
        },
        {
            "commet_user": "john",
            "commet_content": "clearly,tks",
            "commet_time": "2019-02-23"
            ,
             "commet_location": "shanghai"
        },
        {
            "commet_user": "lily",
            "commet_content": "it's too hard ",
            "commet_time": "2019-02-22"
           ,
             "commet_location": "shenzhen"
        }
    ],
    "create_time": "2019-02-26"
}

其中commet类型会被转化成一个数组的形式如下:

{
  ... 
  "commet.commet_user":["tom","john","lily"],
  ...
   "commet.commet_location":["beijing","shanghai","shenzhen"],
}

而当执行查询的时候:

GET nesttest/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "commet.commet_user": "john" }},
        { "match": { "commet.commet_location":  "shenzhen" }}
      ]
    }
  }
}

发现查询结果如下:

{
...
 
    "hits" : [
      {
        "_index" : "nest",
        "_type" : "_doc",
        "_id" : "pe0iKWkBulkJdQfMSgyV",
        "_score" : 0.5753642,
        "_source" : {
          "blog_title" : "2019-01-05",
          "blog_content" : "从本篇文章开始,我会分享给大家canvas绘制的各种基础图形和酷炫的图形",
          "tags" : [
            "Java",
            "es"
          ],
          "hit_count" : 5,
          "commet" : [
            {
              "commet_user" : "tom",
              "commet_content" : "good Job",
              "commet_time" : "2019-02-26",
              "commet_location" : "beijing"
            },
            {
              "commet_user" : "john",
              "commet_content" : "clearly,tks",
              "commet_time" : "2019-02-23",
              "commet_location" : "shanghai"
            },
            {
              "commet_user" : "lily",
              "commet_content" : "it's too hard ",
              "commet_time" : "2019-02-22",
              "commet_location" : "shenzhen"
            }
          ],
          "create_time" : "2019-02-26"
        }
      }
    ]
  }
}  

上面把所有的结果都列出来了, 但是 john的不在shenzhen啊?所以需要把commet定义为nested 类型。

定义nested类型

PUT nest_new/_mapping/_doc
{
    "properties": {
        "blog_title": {
            "type": "text"
        },
        "blog_content": {
            "type": "text"
        },
        "tags": {
            "type": "keyword"
        },
        "hit_count": {
            "type": "keyword"
        },
        "commet": {
            "type": "nested",//这里
            "properties": {
                "commet_user": {
                    "type": "text"
                },
                "commet_content": {
                    "type": "text"
                },
                "commet_time": {
                    "type": "date"
                },
                "commet_location": {
                    "type": "keyword"
                }
            }
        },
        "create_time": {
            "type": "date"
        },
        "datachange_lasttime": {
            "format": "strict_date_optional_time||epoch_millis",
            "type": "date"
        }
    }
}

ES嵌套深度被限制在50层

(本文完)

作者:付威

博客地址:http://blog.laofu.online

如果觉得对您有帮助,可以下方的RSS订阅,谢谢合作

如有任何知识产权、版权问题或理论错误,还请指正。

本文是付威的网络博客原创,自由转载-非商用-非衍生-保持署名,请遵循:创意共享3.0许可证

交流请加群113249828: 点击加群   或发我邮件 laofu_online@163.com

付威

获得最新的博主文章,请关注上方公众号