Elasticsearch教程(五) elasticsearch Mapping的創(chuàng)建
- Elasticsearch教程(一),全程直播(小白級(jí)別)
- Elasticsearch教程(二),IK分詞器安裝
- Elasticsearch教程(三),IK分詞器安裝 (極速版)
- Elasticsearch安裝(四), elasticsearch head 插件安裝和使用。
- Elasticsearch教程(五) elasticsearch Mapping的創(chuàng)建
- Elasticsearch教程(六) elasticsearch Client創(chuàng)建
- Elasticsearch教程(七) elasticsearch Insert 插入數(shù)據(jù)(Java)
- Elasticsearch教程(八) elasticsearch delete 刪除數(shù)據(jù)(Java)
- Elasticsearch教程(九) elasticsearch 查詢數(shù)據(jù) | 分頁(yè)查詢
- Elasticsearch權(quán)威指南-中文.pdf
一、Mapping介紹
在 Elasticsearch 中, Mapping 是什么?
mapping 在 Elasticsearch 中的作用就是約束。
1.數(shù)據(jù)類型聲明
它類似于靜態(tài)語(yǔ)言中的數(shù)據(jù)類型聲明,比如聲明一個(gè)字段為String
, 以后這個(gè)變量都只能存儲(chǔ)String
類型的數(shù)據(jù)。同樣的, 一個(gè)number
類型的 mapping 字段只能存儲(chǔ)number
類型的數(shù)據(jù)。
2.Mapping它定義了 Type 的屬性。
"_ttl": {"enabled": false}
表示 ttl
關(guān)閉,其實(shí)ttl
默認(rèn)就是關(guān)閉。
3.指定分詞器。
"id": {
"index": "not_analyzed",
"type": "string"
}
指定字段 id
不分詞,并且類型為 string
。
4.…………
二、創(chuàng)建Mapping
1.下面介紹一下HTTP的創(chuàng)建方式。我一般用Java 創(chuàng)建方式。
PUT http://123.123.123.123:9200/index/type/
{
"settings": {
//設(shè)置10個(gè)分片,理解為類似數(shù)據(jù)庫(kù)中的表分區(qū)中一個(gè)個(gè)分區(qū)的概念,不知道是否妥當(dāng)
"number_of_shards": 10
},
"mappings": {
"trades": {
"_id": {
"path": "id"
},
"properties": {
"id": {
"type": "integer",
//id:自增數(shù)字
//要求:查詢
"store" : true
},
"name": { //名稱
"type": "string"
},
"brand": { //品牌: PG,P&G,寶潔集團(tuán),寶潔股份,聯(lián)想集團(tuán),聯(lián)想電腦等
"type": "string"
},
"orderNo": { //訂單號(hào) :如ATTS000928732
"type": "string",
"index": "not_analyzed"
},
"description": {
//描述: 2015款玫瑰香型強(qiáng)生嬰兒沐浴露,550ml,包郵
"type": "string",
"sort": true
},
"date": {
"type": "date"
},
"city": {
"type": "string"
},
"qty": {// index分詞無(wú)效
"type": "float"
},
"price": {
//價(jià)格: float index無(wú)效
"type": "float"
}
}
}
}
}
上面是從其他地方抄過(guò)來(lái)的。因?yàn)槲也挥眠@種方式。
2.Java方式創(chuàng)建。
構(gòu)建 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()
//開(kāi)啟倒計(jì)時(shí)功能
.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()
//關(guān)聯(lián)數(shù)據(jù)
.startObject("list").field("type","object").endObject()
.endObject()
.endObject();
} catch (IOException e) {
e.printStackTrace();
}
return mapping;
}
}
創(chuàng)建 Mapping
public static void createBangMapping(){
PutMappingRequest mapping = Requests.putMappingRequest(INDEX).type(TYPE).source(ZhidaoMapping.getMapping());
ESTools.client.admin().indices().putMapping(mapping).actionGet();
}
創(chuàng)建的時(shí)候,需要 index
已經(jīng)創(chuàng)建才行,要不然會(huì)報(bào)錯(cuò)。
//構(gòu)建一個(gè)Index(索引)
CreateIndexRequest request = new CreateIndexRequest(INDEX);
ESTools.client.admin().indices().create(request);
創(chuàng)建完畢在 Head 插件里查看或者Get
請(qǐng)求。
http://123.123.123.123:9200/index/type/_mapping
得到的結(jié)果:
{
"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"//這里出現(xiàn)了復(fù)合類型
},
"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插件查看
其實(shí) Mapping ,你接觸 Elasticsearch 久一點(diǎn)也就那么回事。我們雖然知道 Elasticsearch 有根據(jù)數(shù)據(jù)識(shí)別創(chuàng)建 Mapping ,但是最好是創(chuàng)建,并且指定分詞與否。這樣高效一點(diǎn)。
版權(quán)所屬:SO JSON在線解析
原文地址:http://zijieyoumin.cn/blog/86.html
轉(zhuǎn)載時(shí)必須以鏈接形式注明原始出處及本聲明。
如果本文對(duì)你有幫助,那么請(qǐng)你贊助我,讓我更有激情的寫下去,幫助更多的人。