Elasticsearch教程(五) elasticsearch Mapping的创建

JSON 2016-08-22 18:49:20 150054

Elasticsearch  目录

一、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  ,但是最好是创建,并且指定分词与否。这样高效一点。


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/86.html

转载时必须以链接形式注明原始出处及本声明。


如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
Elasticsearch教程Elasticsearch Java API创建Mapping,指定分词器
Elasticsearch教程(六) elasticsearch Client创建
Elasticsearch教程Elasticsearch count 查询,Elasticsearch 查询是否存在
Elasticsearch教程Elasticsearch配置文件 — elasticsearch.yml
Elasticsearch教程(八) elasticsearch delete 删除数据(Java)
Elasticsearch 教程Elasticsearch部署阿里云集群,支持外网请求方式
Elasticsearch 教程Elasticsearch 日期查询详解,Elasticsearch Date 查询Java API
Elasticsearch教程(九) elasticsearch 查询数据 | 分页查询
Elasticsearch教程(四) elasticsearch head 插件安装和使用
Elasticsearch教程Elasticsearch安全篇,通过Nginx http basic 限制访问
最新文章
PHP变量剖析 4
SQL全外连接剖析 119
SQL自然连接剖析 147
springboot启动原理 245
SQL右连接【RIGHT JOIN】详解及图解 450
SQL左链接【LEFT JOIN】详解及图解 357
SQL非等值连接剖析 262
SQL等链接剖析 291
SQL内连接详解及图解 385
python之numpy常用的100种数值相关方法及代码示例 231
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 679222
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674561
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 599008
免费天气API,天气JSON API,不限次数获取十五天的天气预报 565182
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 551699
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509186
Elasticsearch教程(四) elasticsearch head 插件安装和使用 479635
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 262798
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244092
Elasticsearch教程(一),全程直播(小白级别) 225115
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...