码迷,mamicode.com
首页 > 移动开发 > 详细

ElasticSearch添加mapping

时间:2017-06-08 13:15:34      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:build   sts   update   .exe   arch   ati   res   let   name   

1.创建索引

/**
* 创建索引
*
* @param indexName
*/
public static void createIndex(String indexName) {
//插入前删除,以免报错
boolean flag = client.admin().indices().exists(new IndicesExistsRequest().indices(new String[]{indexName})).actionGet().isExists();
if (flag){
client.admin().indices().prepareDelete(indexName).execute().actionGet();
}
client.admin().indices().prepareCreate(indexName).get();
//client.admin().indices().prepareCreate(indexName)
// .setSettings(Settings.builder()
// .put("index.number_of_shards", 3)
// .put("index.number_of_replicas", 2)
// )
// .get();
}

 

2.创建mapping

    /**
     * 创建mapping,根据需求修改
     *
     * @param indexName
     * @param typeName
     */
    public static void createMapping(String indexName, String typeName) {
        XContentBuilder mappingType = null;
        try {
            mappingType = jsonBuilder()
                    .startObject()
                    .startObject(typeName)
                    .startObject("properties")
                    .startObject("id").field("type", "integer").field("store", "yes").endObject()
                    .startObject("Name").field("type", "string").field("store", "yes").field("analyzer", "ik_smart").field("search_analyzer", "ik_smart").endObject()
                    .startObject("updatedate").field("type", "date").field("store", "yes").endObject()
                    .endObject()
                    .endObject()
                    .endObject();
        } catch (IOException e) {
            logger.error("build company mapping failed", e);
        }
        PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(typeName).source(mappingType);
        client.admin().indices().putMapping(mapping).actionGet();
        client.admin().indices().prepareRefresh().get();
        //client.close();
    }

 

ElasticSearch添加mapping

标签:build   sts   update   .exe   arch   ati   res   let   name   

原文地址:http://www.cnblogs.com/meowmiao/p/6956241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!