当前位置:   article > 正文

ES新建与查询父子文档,嵌套文档_java es 8.x 父子结构嵌套

java es 8.x 父子结构嵌套

ES新建父子文档与查询

  • 新建索引scrm_staff_rel_index
put scrm_staff_rel_index
{ 
  "mappings":{
    "properties":{
      
      "staff_office_relation":{
        
        "type":"join",
        "relations":{
          "scrm_staff":"scrm_staff_office_rel"
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

  • 索引父文档
post scrm_staff_rel_index/_doc/staff1
{
  
  "staff_id":1,
  "staff_name":"windDrift",
   "staff_office_relation":{
     "name":"scrm_staff"
   }
  
  
}
post scrm_staff_rel_index/_doc/staff2
{
  
  "staff_id":2,
  "staff_name":"tangya",
   "staff_office_relation":{
     "name":"scrm_staff"
   }
  
  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

  • 索引子文档
put scrm_staff_rel_index/_doc/office_rel1?routing=staff1
{
"office_name":"湖南",
"office_code":"hunan",
"staff_office_relation":{
"name":"scrm_staff_office_rel",
"parent":"staff1"
}
}

put scrm_staff_rel_index/_doc/office_rel2?routing=staff2
{
  
"office_name":"衡阳金甲领",
"office_code":"hyjjl",
 "staff_office_relation":{
   
   "name":"scrm_staff_office_rel",
   "parent":"staff2"
 }
  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

  • 根据父文档id查询
GET scrm_staff_rel_index/_doc/staff1
  • 1

在这里插入图片描述

  • 根据子文档id查询(发现高版本es会自动指定routing,低版本可能不兼容)
GET scrm_staff_rel_index/_doc/office_rel1?routing=staff1
  • 1

在这里插入图片描述

  • 根据子文档查询父文档内容
GET scrm_staff_rel_index/_search
{
  "query": {
    "has_child": {
      "type": "scrm_staff_office_rel", 
      "query": {
        "match": {
          "office_code": "hunan"
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

  • 通过父文档查询子文档
GET scrm_staff_rel_index/_search
{
"query": {
"has_parent": {
"parent_type": "scrm_staff",
"query": {
"match": {
"staff_name": "windDrift"}}}}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

嵌套文档的新建与查询

  • 构建嵌套文档`
PUT scrm_staff
{
  "mappings":{
    "properties":{
      "scrm_office":{
        "type":"nested",
        "properties":{
          "office_name":{"type":"keyword"},
        "office_code":{"type":"keyword"}
        }                
      },
      "name":{"type":"text","fields": {"keyword":{"type":"keyword","ignore_above":256}}}
      
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

POST scrm_staff/_doc/1
{
  "name":"windDrift",
  "scrm_office":[
    {      
    "office_name":"湖南",
    "office_code":"hunan"      
    },{
      
      "office_name":"深圳",
      "office_code":"shenzhen"
    }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述
Nested嵌套文档查询

POST scrm_staff/_search
{
  "query":{
    "bool": {
      
      "must": [
        {"match": { "name": "windDrift"}},
        {
          
          "nested": {
            "path": "scrm_office",
            "query": {
              "bool": {
                "must": [
                  { "match": {"scrm_office.office_code": "hunan" }},
                  { "match": {"scrm_office.office_name": "湖南" }}
                ]
              }
            }
          }
        }
      ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Nested嵌套文档聚合分析

POST scrm_staff/_search
{
"aggs":{
    "nameAggs":{
      
      "nested": {
        "path": "scrm_office"
      },
      
      "aggs": {
        "nameAggs": {
          "terms": {
            "field": "scrm_office.office_code",
            "size": 10
          }
        }
      }
    }
    
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述
普通的agg是不生效的

POST scrm_staff/_search
{
   "aggs": {
    "nameAgg": {
      "terms": {"field": "scrm_office.office_name"}
      
    }
  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述
普通agg查询这里的聚合没有生效

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/456707
推荐阅读
相关标签
  

闽ICP备14008679号