一、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。
4.…………
二、创建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中Mapping的作用与创建方法,包括数据类型声明、字段分词配置等内容,并通过Java示例展示了如何自定义Mapping。
5100

被折叠的 条评论
为什么被折叠?



