基础知识请搜索其他博客,本文只讲实际操作。
场景:当需要存储关系对象时 比如 一个博客下有多个评论
-
- 主要解决对象关系存储
- 需要手动指定索引各字段的类型 并指定关系对象为nested类型
- 注意只能在新建索引时指定好,否则后续不能修改,需要通过删除索引 重建。
{ "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" } } } } } } } |
-
- 查询
{ "query": { "bool": { "must": [ { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.name": "john" } }, { "match": { "comments.age": 38 } } ] } } } } ] } } } |