一、Mapping介紹
在 Elasticsearch? 中, Mapping? 是什么?
mapping? 在 Elasticsearch? 中的作用就是約束。
1.數據類型聲明
它類似于靜態語言中的數據類型聲明,比如聲明一個字段為String, 以后這個變量都只能存儲String類型的數據。同樣的, 一個number類型的 mapping? 字段只能存儲number類型的數據。
2.Mapping它定義了 Type 的屬性。
"_ttl": {"enabled": false}
表示 ttl關閉,其實ttl默認就是關閉。
3.指定分詞器。
"id": {"index": "not_analyzed","type": "string"}
指定字段 id不分詞,并且類型為 string。
二、創建Mapping
?
1.下面介紹一下HTTP的創建方式。我一般用Java 創建方式。
PUT http://123.123.123.123:9200/index/type/{"settings": {//設置10個分片,理解為類似數據庫中的表分區中一個個分區的概念,不知道是否妥當"number_of_shards": 10}, "mappings": {"trades": {"_id": {"path": "id"},"properties": {"id": {"type": "integer",//id:自增數字//要求:查詢"store" : true},"name": { //名稱"type": "string"},"brand": { //品牌: PG,P&G,寶潔集團,寶潔股份,聯想集團,聯想電腦等 "type": "string"},"orderNo": { //訂單號 :如ATTS000928732"type": "string","index": "not_analyzed"},"description": {//描述: 2015款玫瑰香型強生嬰兒沐浴露,550ml,包郵"type": "string", "sort": true},"date": {"type": "date"},"city": {"type": "string"},"qty": {// index分詞無效"type": "float"},"price": {//價格: float index無效"type": "float"}}}}}
上面是從其他地方抄過來的。因為我不用這種方式。
2.Java方式創建。
構建 Mapping?
package com.sojson.core.elasticsearch.mapping;import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;import java.io.IOException;?import org.elasticsearch.common.xcontent.XContentBuilder;?public class ZhidaoMapping {?public static XContentBuilder getMapping(){XContentBuilder mapping = null;try {mapping = jsonBuilder().startObject()//開啟倒計時功能.startObject("_ttl").field("enabled",false).endObject().startObject("properties").startObject("title").field("type","string").endObject().startObject("question").field("type","string").field("index","not_analyzed").endObject().startObject("answer").field("type","string").field("index","not_analyzed").endObject().startObject("category").field("type","string").field("index","not_analyzed").endObject().startObject("author").field("type","string").field("index","not_analyzed").endObject().startObject("date").field("type","string").field("index","not_analyzed").endObject().startObject("answer_author").field("type","string").field("index","not_analyzed").endObject().startObject("answer_date").field("type","string").field("index","not_analyzed").endObject().startObject("description").field("type","string").field("index","not_analyzed").endObject().startObject("keywords").field("type","string").field("index","not_analyzed").endObject().startObject("read_count").field("type","integer").field("index","not_analyzed").endObject()//關聯數據.startObject("list").field("type","object").endObject().endObject().endObject();} catch (IOException e) {e.printStackTrace();}?return mapping;}}
創建 Mapping?
public static void createBangMapping(){PutMappingRequest mapping = Requests.putMappingRequest(INDEX).type(TYPE).source(ZhidaoMapping.getMapping());ESTools.client.admin().indices().putMapping(mapping).actionGet();?}
創建的時候,需要 index已經創建才行,要不然會報錯。
//構建一個Index(索引) CreateIndexRequest request = new CreateIndexRequest(INDEX);ESTools.client.admin().indices().create(request);
創建完畢在 Head? 插件里查看或者Get請求。
http://123.123.123.123:9200/index/type/_mapping
得到的結果:
{"zhidao_index": {"mappings": {"zhidao_type": {"_ttl": {"enabled": false},"properties": {"answer": {"type": "string","index": "not_analyzed"},"answerAuthor": {"type": "string"},"answerDate": {"type": "date","format": "strict_date_optional_time||epoch_millis"//這里出現了復合類型},"answer_author": {"type": "string","index": "not_analyzed"},"answer_date": {"type": "string","index": "not_analyzed"},"author": {"type": "string","index": "not_analyzed"},"category": {"type": "string","index": "not_analyzed"},"date": {"type": "string","index": "not_analyzed"},"description": {"type": "string","index": "not_analyzed"},"id": {"type": "string","index": "not_analyzed"},"keywords": {"type": "string","index": "not_analyzed"},"list": {"type": "object"},"question": {"type": "string","index": "not_analyzed"},"readCount": {"type": "long"},"read_count": {"type": "integer"},"title": {"type": "string"}}}}}}
Head插件查看
其實 Mapping? ,你接觸 Elasticsearch? 久一點也就那么回事。我們雖然知道 Elasticsearch? 有根據數據識別創建 Mapping? ,但是最好是創建,并且指定分詞與否。這樣高效一點。
總結
以上是生活随笔為你收集整理的Elasticsearch教程 elasticsearch Mapping的创建的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。