您的位置:首页 > 移动开发

mapping创建 添加记录到es

2017-06-05 17:10 381 查看
/**
* 索引的mapping
* <p>
* 预定义一个索引的mapping,使用mapping的好处是可以个性的设置某个字段等的属性
* Es_Setting.INDEX_DEMO_01类似于数据库 mapping 类似于预设某个表的字段类型
* <p>
* Mapping,就是对索引库中索引的字段名及其数据类型进行定义,类似于关系数据库中表建立时要定义字段名及其数据类型那样, 不过es的
* mapping比数据库灵活很多,它可以动态添加字段。 一般不需要要指定mapping都可以,因为es会自动根据数据格式定义它的类型,
* 如果你需要对某 些字段添加特殊属性(如:定义使用其它分词器、是否分词、是否存储等),就必须手动添加mapping。
* 有两种添加mapping的方法,一种是定义在配 置文件中,一种是运行时手动提交mapping,两种选一种就行了。 *
* 创建mapping(feid("indexAnalyzer","ik")该字段分词IK索引
* ;feid("searchAnalyzer","ik")该字段分词ik查询;具体分词插件请看IK分词插件说明)
*
* @param indices
*            索引名称;
* @param mappingType
*            索引类型
* @throws Exception
*             Exception
*/
protected static void buildMappingdatafile_commcust_info(String indices, String mappingType) throws Exception {
EsUtils.client.admin().indices().prepareCreate(indices).execute().actionGet();
//2.create a mapping
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject()
.startObject(mappingType)
.startObject("_all")
.field("analyzer", "whitespace")
.field("search_analyzer", "whitespace")
.field("term_vector","no")
.field("store","false")
.endObject()
.startObject("properties")
.startObject("id").field("type","text").field("index","false").field("include_in_all","false").endObject()//主键,不做索引
.startObject("emptycol").field("type","text").field("index","false").field("include_in_all","false").endObject()//未命名列,不做索引
.startObject("comm_no").field("type","keyword").endObject()//通讯号码,不做分词
.startObject("cust_name").field("type","keyword").endObject()//客户名称,不做分词
.startObject("cust_addr").field("type","text").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").endObject()//客户地址
.startObject("cust_id_type").field("type","keyword").endObject()//证件类型,
.startObject("cust_id_no").field("type","keyword").endObject()//证件号码
.startObject("create_time").field("type","date").endObject()//创建时间,基本列
.startObject("data_src").field("type","text").field("index","false").field("include_in_all","false").endObject()//数据来源,基本列
.startObject("creator").field("type","text").field("index","false").field("include_in_all","false").endObject()//创建人,基本列
.endObject() // end 'properties object'
.endObject() // end 'mappingType object'
.endObject(); // end ' object'

PutMappingRequest mappingRequest = Requests.putMappingRequest(indices).type(mappingType).source(mapping);
EsUtils.client.admin().indices().putMapping(mappingRequest).actionGet();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: