ElasticSearch关于映射mapping介绍
#首先我們還是先增加幾個文檔
PUT /myindex/article/1 {"post_date": "2020-03-14","title": "Java","content": "java is the best language","author_id": 119 }PUT /myindex/article/2 {"post_date": "2020-03-14" ,"title": "html","content": "I like html","author_id": 120 } PUT /myindex/article/3 {"post_date": "2020-03-14" ,"title": "es","content": "Es is distributed document store" ,"author_id": 110 }?
#查看es的文檔映射mapping的數據結構 GET /myindex/article/_mappingmapping數據結構如下:?
{"myindex" : {"mappings" : {"article" : {"properties" : {"author_ id" : {"type" : "long"},"content" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"post_ _date" : {"type" : "date"},"post_ date" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"title" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}}} }這里我們并沒有為索引文檔的數據結構做定義,但是我們發現es自動創建了?index?type?以及type對應的mapping(dynamic mapping)?說明ElasticSearch能根據我們給的數據自動檢測并且給定字段的數據類型
如果給定的? true? 、false------>boolean
如果給定的字符串 "字符串"------>string(注意我這里用的是6.8.6版本?string已經被text或者keyword代替)
如果給定的數字? ?1、2------>long
如果給定的小數? 12.34------>double
如果給定的時間? ?2020-03-14------->date
什么是映射mapping?
mapping定義了type中的每個字段的數據類型以及這些字段的如何分詞的等相關屬性
我們現在帶查詢一下添加的文檔內容 GET /myindex/article/_search
{"took" : 29,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 3,"max_score" : 1.0,"hits" : [{"_index" : "myindex","_type" : "article","_id" : "2","_score" : 1.0,"_source" : {"post_ date" : "2020-03-14","title" : "html","content" : "I like html","author_ id" : 120}},{"_index" : "myindex","_type" : "article","_id" : "1","_score" : 1.0,"_source" : {"post_ date" : "2020-03-14","title" : "Java","content" : "java is the best language","author_ id" : 119}},{"_index" : "myindex","_type" : "article","_id" : "3","_score" : 1.0,"_source" : {"post_ _date" : "2020-03-14","title" : "es","content" : "Es is distributed document store","author_ id" : 110}}]} }?
帶條件的查詢
#查不出來
GET /myindex/article/_search?q=post_date:2020
?
#可以查出來
GET /myindex/article/_search?q=post_date:2020-03-14
?
#可以查出來
GET /myindex/article/_search?q=content:html
?
?
注意?
這是因為ElasticSearch映射mapping進行指定的,post_date是日期類型,而content是字符串類型所以字符串就查詢出來了。像日期date類型和數字long類型要進行精確查詢才可以查詢,而說明日期和數字類型沒有進行分詞,而字符串進行了分詞。字符串text類型默認進行了分詞的
?
到這里我們知道了mapping有兩個作用,一是規定自定的類型,二是規定相關字段的屬性(比如是不是進行分詞)
?
?
?
?
總結
以上是生活随笔為你收集整理的ElasticSearch关于映射mapping介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 去中心化钱包CoinU下载教程(如何下载
- 下一篇: 去中心化钱包CoinU基本介绍,你想知道