elasticsearch2.1.0索引的相关操作

来源:互联网 发布:windows图标缩小 编辑:程序博客网 时间:2024/06/05 01:51

这里记录一下elasticsearch2.1.0索引的相关的操作,代码都是自己经过测试可以直接使用的代码。

/** * es索引相关操作测试类 * @author yujie.wang */public class IndexOperator {private static String INDEX = "my_store";private static String TYPE = "products";/** * 判断一个索引是否存在 * @param client * @param index */public void isExists(IndicesAdminClient client,String index){IndicesExistsResponse response = client.prepareExists(index).get();System.out.println(response.isExists() ? "exists" : "no");// another mthod/*IndicesExistsRequest request = new IndicesExistsRequest(index);IndicesExistsResponse response2 = indexClient.exists(request).actionGet();System.out.println(response2.isExists() ? "exists": " no");*/}/** * 判断一个索引的类型是否存在 * @param client * @param index * @param type */public void isTypeExists(IndicesAdminClient client , String index, String type){TypesExistsResponse response = client.prepareTypesExists(index).setTypes(type).get();System.out.println(response.isExists() ? " type exists": " no");}/** * 创建一个索引 * @param client * @param index */public void createIndex(IndicesAdminClient client , String index){CreateIndexResponse response = client.prepareCreate(index).get();System.out.println(response.isAcknowledged() ? " create successfully": " create failed");}/*{"state": "open","settings": {"index": {"creation_date": "1513589566589","uuid": "d8C9DP04QdW9BUD2M6JBLg","number_of_replicas": "1","number_of_shards": "5","version": {"created": "2010099"}}},"mappings": { },"aliases": [ ]}*//** * 指定setting和mapping信息创建索引 * @param client * @param index * @param type */public void createIndexBySettingAndMapping(IndicesAdminClient client , String index,String type){Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2).build();XContentBuilder mapping = null;try {mapping = XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties") .startObject("name").field("type", "string").field("store", "yes").endObject()                         .startObject("sex").field("type", "string").field("store", "yes").endObject()                         .startObject("college").field("type", "string").field("store", "yes").endObject()                         .startObject("age").field("type", "integer").field("store", "yes").endObject()                         .startObject("school").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()                     .endObject()                 .endObject()             .endObject();} catch (Exception e) {// TODO: handle exceptionSystem.out.println("exception: "+e);}CreateIndexResponse response = client.prepareCreate(index).setSettings(settings).addMapping(type, mapping).get();System.out.println(response.isAcknowledged() ? " create successfully": " create failed");}/*{"state": "open","settings": {"index": {"creation_date": "1513592594874","uuid": "d0R_IzukQSC8pXT7JkPpxA","number_of_replicas": "2","number_of_shards": "3","version": {"created": "2010099"}}},"mappings": {"yujie_type": {"properties": {"sex": {"store": true,"type": "string"},"school": {"index": "not_analyzed","store": true,"type": "string"},"name": {"store": true,"type": "string"},"age": {"store": true,"type": "integer"},"college": {"store": true,"type": "string"}}}},"aliases": [ ]}*//** * 删除索引 * @param client * @param index */public void deleteIndex(IndicesAdminClient client , String index){DeleteIndexResponse response = client.prepareDelete(index).get();System.out.println(response.isAcknowledged() ? " delete successfully": " delete failed");}/** * 当索引不再使用时 可以关闭索引 * @param client * @param index */public void closeIndex(IndicesAdminClient client , String index){CloseIndexResponse response = client.prepareClose(index).get();System.out.println(response.isAcknowledged() ? " delete successfully": " delete failed");}/** * 打开一个关闭的索引 * @param client * @param index */public void openIndex(IndicesAdminClient client , String index){OpenIndexResponse response = client.prepareOpen(index).get();System.out.println(response.isAcknowledged() ? " delete successfully": " delete failed");}/** * 索引存在的情况下 为索引添加一个mapping * @param client * @param index * @param type */public void putMappingByIndex(IndicesAdminClient client , String index, String type){ XContentBuilder mappingBuilder = null;    try {        mappingBuilder = XContentFactory.jsonBuilder()                .startObject()                    .startObject(type)                        .startObject("properties")                            .startObject("name").field("type", "string").field("store", "yes").endObject()                            .startObject("sex").field("type", "string").field("store", "yes").endObject()                            .startObject("college").field("type", "string").field("store", "yes").endObject()                            .startObject("age").field("type", "long").field("store", "yes").endObject()                            .startObject("school").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject()                        .endObject()                    .endObject()                .endObject();    } catch (Exception e) {    System.out.println("exception: "+ e);    }    PutMappingResponse response = client.preparePutMapping(index).setType(type).setSource(mappingBuilder).get();    System.out.println(response.isAcknowledged() ? " delete successfully": " delete failed");}/** * 给一个索引设置别名 * @param client * @param index */public void addAlias(IndicesAdminClient client , String index, String alias){IndicesAliasesResponse response = client.prepareAliases().addAlias(index, alias).get();System.out.println(response.isAcknowledged() ? " add successfully": " add failed");}/** * 判断一个别名是否存在 * @param client * @param index * @param alias */public void isAliasesExist(IndicesAdminClient client , String index, String alias){AliasesExistResponse response = client.prepareAliasesExist(alias).get();System.out.println(response.exists() ? " alias exists": "  alias not exists");}/** * 更新索引的分片信息,只能更新分片副本信息,主分片一旦分配就不能改变 * @param client * @param index */public void updateIndexSetting(IndicesAdminClient client , String index){Settings settings = Settings.builder().put("index.number_of_replicas", 1).build();UpdateSettingsResponse response = client.prepareUpdateSettings(index).setSettings(settings).get();System.out.println(response.isAcknowledged() ? " update successfully": " update failed");}}