你知道ES的Nested类型吗?

基础知识请搜索其他博客,本文只讲实际操作。

场景:当需要存储关系对象时 比如 一个博客下有多个评论

    1. 主要解决对象关系存储
    2. 需要手动指定索引各字段的类型 并指定关系对象为nested类型
    3. 注意只能在新建索引时指定好否则后续不能修改,需要通过删除索引 重建

{

  "mappings": {

    "blog": {

      "properties": {

        "title": {

          "type": "text"

        },

        "body": {

          "type": "text"

        },

        "tags": {

          "type": "keyword"

        },

        "published_on": {

          "type": "keyword"

        },

        "comments": {

          "type": "nested",

          "properties": {

            "name": {

              "type": "text"

            },

            "comment": {

              "type": "text"

            },

            "age": {

              "type": "short"

            },

            "rating": {

              "type": "short"

            },

            "commented_on": {

              "type": "text"

            }

          }

        }

      }

    }

  }

}

    1. 查询

{

  "query": {

    "bool": {

      "must": [

        {

          "nested": {

            "path": "comments",

            "query": {

              "bool": {

                "must": [

                  {

                    "match": {

                      "comments.name": "john"

                    }

                  },

                  {

                    "match": {

                      "comments.age": 38

                    }

                  }

                ]

              }

            }

          }

        }

      ]

    }

  }

}

 

猜你喜欢

转载自blog.csdn.net/qq_29857681/article/details/88011313