白话Elasticsearch58-数据建模实战_基于nested object实现博客与评论嵌套关系
生活随笔
收集整理的這篇文章主要介紹了
白话Elasticsearch58-数据建模实战_基于nested object实现博客与评论嵌套关系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- 官網
- 示例
- Object datatype
- 讓ES自動創建索引,插入一條數據
- 查看mapping
- 需求: 被年齡是28歲的黃藥師評論過的博客
- 查詢結果不對原因分析
- Nested datatype
- 解決object查詢不對的問題
- 修改mapping,將comments的類型從object設置為nested
- 寫入數據
- 查看mapping
- nested object 存儲形式
- 再次查詢
- score_mode
- limits on nested mappings and objects
概述
繼續跟中華石杉老師學習ES,第58篇
課程地址: https://www.roncoo.com/view/55
官網
Nested datatype:戳這里
Object datatype:戳這里
示例
Object datatype
讓ES自動創建索引,插入一條數據
#讓ES自動創建索引,插入一條數據 PUT /website/blogs/1 {"title": "花無缺發表的一篇帖子","content": "我是花無缺,大家要不要考慮一下投資房產和買股票的事情啊。。。","tags": ["投資","理財"],"comments": [{"name": "小魚兒","comment": "什么股票啊?推薦一下唄","age": 28,"stars": 4,"date": "2016-09-01"},{"name": "黃藥師","comment": "我喜歡投資房產,風,險大收益也大","age": 31,"stars": 5,"date": "2016-10-22"}] }查看mapping
GET /website/_mapping/blogs/返回:
{"website": {"mappings": {"blogs": {"properties": {"comments": {"properties": {"age": {"type": "long"},"comment": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"date": {"type": "date"},"name": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"stars": {"type": "long"}}},"content": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"tags": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"title": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}}} }需求: 被年齡是28歲的黃藥師評論過的博客
#被年齡是28歲的黃藥師評論過的博客 GET /website/blogs/_search {"query": {"bool": {"must": [{"match": {"comments.name": "黃藥師"}},{"match": {"comments.age": 28}}]}} }返回:
查詢結果不對原因分析
官方文檔: 戳這里
歸根到底 還是object類型數據結構的底層存儲導致的查詢不正確
{"title": [ "花無缺", "發表", "一篇", "帖子" ],"content": [ "我", "是", "花無缺", "大家", "要不要", "考慮", "一下", "投資", "房產", "買", "股票", "事情" ],"tags": [ "投資", "理財" ],"comments.name": [ "小魚兒", "黃藥師" ],"comments.comment": [ "什么", "股票", "推薦", "我", "喜歡", "投資", "房產", "風險", "收益", "大" ],"comments.age": [ 28, 31 ],"comments.stars": [ 4, 5 ],"comments.date": [ 2016-09-01, 2016-10-22 ] }object類型底層數據結構,會將一個json數組中的數據,進行扁平化
所以,直接命中了這個document,name=黃藥師,age=28,在范圍之內,正好符合,所以被查詢出來了。
Nested datatype
解決object查詢不對的問題
引入nested object類型,來解決object類型底層數據結構導致的問題
修改mapping,將comments的類型從object設置為nested
修改mapping,將comments的類型從object設置為nested
DELETE websitePUT /website {"mappings": {"blogs": {"properties": {"comments": {"type": "nested", "properties": {"name": { "type": "text" },"comment": { "type": "text" },"age": { "type": "short" },"stars": { "type": "short" },"date": { "type": "date" }}}}}} }寫入數據
PUT /website/blogs/1 {"title": "花無缺發表的一篇帖子","content": "我是花無缺,大家要不要考慮一下投資房產和買股票的事情啊。。。","tags": [ "投資", "理財" ],"comments": [ {"name": "小魚兒","comment": "什么股票啊?推薦一下唄","age": 28,"stars": 4,"date": "2016-09-01"},{"name": "黃藥師","comment": "我喜歡投資房產,風,險大收益也大","age": 31,"stars": 5,"date": "2016-10-22"}] }查看mapping
#查看mapping GET /website/_mapping/blogs/返回:
{"website": {"mappings": {"blogs": {"properties": {"comments": {"type": "nested","properties": {"age": {"type": "short"},"comment": {"type": "text"},"date": {"type": "date"},"name": {"type": "text"},"stars": {"type": "short"}}},"content": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"tags": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"title": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}}} }nested object 存儲形式
這樣的話,nested object 存儲如下:
{ "comments.name": [ "小魚兒" ],"comments.comment": [ "什么", "股票", "推薦" ],"comments.age": [ 28 ],"comments.stars": [ 4 ],"comments.date": [ 2014-09-01 ] } { "comments.name": [ "黃藥師" ],"comments.comment": [ "我", "喜歡", "投資", "房產", "風險", "收益", "大" ],"comments.age": [ 31 ],"comments.stars": [ 5 ],"comments.date": [ 2014-10-22 ] } { "title": [ "花無缺", "發表", "一篇", "帖子" ],"body": [ "我", "是", "花無缺", "大家", "要不要", "考慮", "一下", "投資", "房產", "買", "股票", "事情" ],"tags": [ "投資", "理財" ] }再次查詢
GET /website/blogs/_search {"query": {"bool": {"must": [{"match": {"title": "花無缺"}},{"nested": {"path": "comments","query": {"bool": {"must": [{"match": {"comments.name": "黃藥師"}},{"match": {"comments.age": 28}}]}}}}]}} }
改成31的呢
score_mode
score_mode:max,min,avg,none,默認是avg
如果搜索命中了多個nested document,如何把多個nested document的分數合并為一個分數
limits on nested mappings and objects
總結
以上是生活随笔為你收集整理的白话Elasticsearch58-数据建模实战_基于nested object实现博客与评论嵌套关系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch57-数据
- 下一篇: 白话Elasticsearch60-数据