elasticsearch之嵌套对象、父子文档

一,嵌套对象

es并非关系型数据库,它并不擅长关系型数据库的join操作。在存储数据时,用冗余数据替代查询时的关联。

如blog和comments,如果用关系型数据库,会将blog存一个表,comments存另一个表,然后将这两个表关联起来。

在es中可以这样存储:

PUT /nested_index/_doc/1
{
  "title": "Nest eggs",
  "body":  "Making your money work...",
  "tags":  [ "cash", "shares" ],
  "comments": [ 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    },
    {
      "name":    "Alice White",
      "comment": "More like this please",
      "age":     31,
      "stars":   5,
      "date":    "2014-10-22"
    }
  ]
}

查询时就很高效,一次把文档和关联的信息查出来。

不过,在用es查询时,可能出现一些意外情况:

GET nested_index/_search
{
  
  "query": {
         "bool": {
            "must": [
              {
                "match": {
                  "comments.name": "john"
                }
              },
              {
                "match": {
                  "comments.age": "31"
                }
              }
            ]
          }
      }
}

本来,根据john+31是不应该查出数据的,因为john是28岁,实际上却能查出来结果。

原因在于es存储object对象时会将对象扁平化处理:

{
   "blog":"aking your money work...",
   "comments.name":["john","alice"],
   "comments.age":[31,28]
}

这样子,就失去了边界,两个comments就区分不了了。

这种情况下,可以用到嵌套对象,嵌套对象的mapping不能动态创建,必须在索引文档之前创建。

PUT /nested_index2
{
  "mappings": {
    "properties": {
      "comments":{
        "type": "nested"
      }
    }
  }
}

再次查询

GET nested_index2/_search
{
  
  "query": {
    "nested": {
      "path": "comments",
      "query": {
         "bool": {
            "must": [
              {
                "match": {
                  "comments.name": "john"
                }
              },
              {
                "match": {
                  "comments.age": "31"
                }
              }
            ]
          }
      }
    }
  }
}

这时查询31岁的john就不会有结果了,把条件age改成28就可以了。

原理就是对于nested对象,es会将其抽取出来作为独立的文档,并保留文档之间的关联关系,在查询时,把相应文档关联起来。

二,父子文档

可以把结构相同但有父子关系的文档存储在同一个索引中,在形式上父子文档都是独立的文档,互相之间的修改、更新都是独立的,在逻辑上二者是有层次关系的。

1,设置mapping,设置一个field用于识别父子关系的key,这个field的类型时join,relations用来指明父子关系:answer是子文档,question是父文档。

PUT join_index
{
  "mappings": {
    "properties": {
      "my_join_field": { 
        "type": "join",
        "relations": {
          "question": "answer" 
        }
      }
    }
  }
}

2,插入父文档时要指明文档类型

PUT join_index/_doc/1?refresh
{
  "text": "This is a question",
  "my_join_field": {
    "name": "question" 
  }
}

PUT join_index/_doc/2?refresh
{
  "text": "This is another question",
  "my_join_field": {
    "name": "question"
  }
}

3,插入子文档时用routing关键字,指明文档类型和父文档id

PUT join_index/_doc/3?routing=1&refresh
{
  "text": "This is an answer",
  "my_join_field": {
    "name": "answer", 
    "parent": "1" 
  }
}

PUT join_index/_doc/4?routing=1&refresh
{
  "text": "This is another answer",
  "my_join_field": {
    "name": "answer",
    "parent": "1"
  }
}

4,根据父文档id查所有子文档

GET join_index/_search
{
  "query": {
    "parent_id":{
      "type":"answer",
      "id":"1"
    }
  }
}

5,根据父文档内容查询所有子文档

GET join_index/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question",
      "query": {
        "match": {
          "text": "question"
        }
      }
    }
  }
}

6,根据子文档内容查询所有父文档

GET join_index/_search
{
  "query": {
    "has_child": {
      "type": "answer",
      "query": {
        "match": {
          "text": "answer"
        }
      }
    }
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小手追梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值