elasticsearch2.1.0索引的相關操作
阿新 • • 發佈:2019-01-05
這裡記錄一下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 exception System.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"); } }