




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Java API之索引管理ElasticSearch為了便于處理索引管理(Indices administration)請求,提供了org.elasticsearch.client.IndicesAdminClient接口。通過如下代碼從 Client 對象中獲得這個接口的實現(xiàn):IndicesAdminClient indicesAdminClient = client.admin().indices();IndicesAdminClient定義了好幾種prepareXXX()方法作為創(chuàng)建請求的入口點。1. 索引存在API索引存在API用于檢查集群中是否存在由prepareExists調(diào)用指定
2、的索引。 /* * 判斷索引是否存在 * param client * param index * return */ public static boolean isIndexExists(Client client, String index) if(Objects.equal(client, null) ("- IndexAPI isIndexExists 請求客戶端為null"); return false; if(StringUtils.isBlank(index) ("- IndexAPI isIndexEx
3、ists 索引名稱為空"); return false; IndicesAdminClient indicesAdminClient = client.admin().indices(); IndicesExistsResponse response = indicesAdminClient.prepareExists(index).get(); return response.isExists(); /* 另一種方式 IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(index); Indice
4、sExistsResponse response = client.admin().indices().exists(indicesExistsRequest).actionGet();*/ prepareExists()可以同時指定多個索引:IndicesExistsResponse response = indicesAdminClient.prepareExists(index1, index2 .).get();2. 類型存在API類型存在API和索引存在API類似,只是不是用來檢查索引是否存在,而是檢查指定索引下的指定類型是否存在。為了確保成功返回結(jié)果,請確保索引已經(jīng)存在,否則不會查
5、找到指定的類型。下面代碼演示查找索引下的指定類型: /* * 判斷類型是否存在 * param client * param index * param type * return */ public static boolean isTypeExists(Client client, String index, String type) if(!isIndexExists(client, index) ("- isTypeExists 索引 不存在",index); return false; IndicesAdminClient indicesAd
6、minClient = client.admin().indices(); TypesExistsResponse response = indicesAdminClient.prepareTypesExists(index).setTypes(type).get(); return response.isExists(); 3. 創(chuàng)建索引API創(chuàng)建索引API可以用來建立一個新索引。我們可以創(chuàng)建空索引或者給它設(shè)置它的映射(mapping)和設(shè)置信息(settings)。3.1 創(chuàng)建空索引下面代碼創(chuàng)建了一個空索引: /* * 創(chuàng)建空索引 默認(rèn)setting 無mapping * param cl
7、ient * param index * return */ public static boolean createSimpleIndex(Client client, String index) IndicesAdminClient indicesAdminClient = client.admin().indices(); CreateIndexResponse response = indicesAdminClient.prepareCreate(index).get(); return response.isAcknowledged(); 查看索引狀態(tài)信息: "state&
8、quot;: "open", "settings": "index": "creation_date": "1476078197394", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "rBATEkx_SBq_oUEIlW8ryQ", "version": "created
9、": "2030399" , "mappings": , "aliases": 3.2. 創(chuàng)建復(fù)雜索引下面代碼創(chuàng)建復(fù)雜索引,給它設(shè)置它的映射(mapping)和設(shè)置信息(settings),指定分片個數(shù)為3,副本個數(shù)為2,同時設(shè)置school字段不分詞。 /* * 創(chuàng)建索引 指定setting * param client * param index * return */ public static boolean createIndex(Client client, String index) / settings Se
10、ttings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2).build(); / mapping XContentBuilder mappingBuilder; try mappingBuilder = XContentFactory.jsonBuilder() .startObject() .startObject(index) .startObject("properties") .
11、startObject("name").field("type", "string").field("store", "yes").endObject() .startObject("sex").field("type", "string").field("store", "yes").endObject() .startObject("college").field(&quo
12、t;type", "string").field("store", "yes").endObject() .startObject("age").field("type", "integer").field("store", "yes").endObject() .startObject("school").field("type", "string").field(
13、"store", "yes").field("index", "not_analyzed").endObject() .endObject() .endObject() .endObject(); catch (Exception e) logger.error("- createIndex 創(chuàng)建 mapping 失?。?quot;,e); return false; IndicesAdminClient indicesAdminClient = client.admin().indices(); Cre
14、ateIndexResponse response = indicesAdminClient.prepareCreate(index) .setSettings(settings) .addMapping(index, mappingBuilder) .get(); return response.isAcknowledged(); 查看索引狀態(tài)信息: "state": "open", "settings": "index": "creation_date": "/p>
15、25", "number_of_shards": "3", "number_of_replicas": "2", "uuid": "ToakRDisSYyX7vjH30HR-g", "version": "created": "2030399" , "mappings": "simple-index": "properties": "co
16、llege": "store": true, "type": "string" , "school": "index": "not_analyzed", "store": true, "type": "string" , "sex": "store": true, "type": "string" , "name&qu
17、ot;: "store": true, "type": "string" , "age": "store": true, "type": "integer" , "aliases": 4. 刪除索引刪除索引API允許我們反向刪除一個或者多個索引。 /* * 刪除索引 * param client * param index */ public static boolean deleteIndex(Client client, Strin
18、g index) IndicesAdminClient indicesAdminClient = client.admin().indices(); DeleteIndexResponse response = indicesAdminClient.prepareDelete(index).execute().actionGet(); return response.isAcknowledged(); 5. 關(guān)閉索引關(guān)閉索引API允許我們關(guān)閉不使用的索引,進(jìn)而釋放節(jié)點和集群的資源,如cpu時鐘周期和內(nèi)存。 /* * 關(guān)閉索引 * param client * param index * ret
19、urn */ public static boolean closeIndex(Client client, String index) IndicesAdminClient indicesAdminClient = client.admin().indices(); CloseIndexResponse response = indicesAdminClient.prepareClose(index).get(); return response.isAcknowledged(); 測試: Test public void closeIndex() throws Exception Stri
20、ng index = "suggestion-index" if(!IndexAPI.isIndexExists(client, index) ("- closeIndex 索引 不存在", index); return; boolean result = IndexAPI.closeIndex(client, index); ("- closeIndex ",result); 關(guān)閉之前:關(guān)閉之后:6. 打開索引打開索引API允許我們打開我們之前使用關(guān)閉索引API /* * 關(guān)閉索引 * p
21、aram client * param index * return */ public static boolean openIndex(Client client, String index) IndicesAdminClient indicesAdminClient = client.admin().indices(); OpenIndexResponse response = indicesAdminClient.prepareOpen(index).get(); return response.isAcknowledged(); 7. 設(shè)置映射API設(shè)置映射API允許我們在指定索引上
22、一次性創(chuàng)建或修改一到多個索引的映射。如果設(shè)置映射必須確保指定的索引必須存在,否則會報錯。 /* * 設(shè)置映射 * param client * param index * param type * return */ public static boolean putIndexMapping(Client client, String index, String type) / mapping XContentBuilder mappingBuilder; try mappingBuilder = XContentFactory.jsonBuilder() .startObject() .st
23、artObject(type) .startObject("properties") .startObject("name").field("type", "string").field("store", "yes").endObject() .startObject("sex").field("type", "string").field("store", "yes").en
24、dObject() .startObject("college").field("type", "string").field("store", "yes").endObject() .startObject("age").field("type", "long").field("store", "yes").endObject() .startObject("school")
25、.field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject() .endObject() .endObject() .endObject(); catch (Exception e) logger.error("- createIndex 創(chuàng)建 mapping 失?。?quot;, e); return false; IndicesAdminClien
26、t indicesAdminClient = client.admin().indices(); PutMappingResponse response = indicesAdminClient.preparePutMapping(index).setType(type).setSource(mappingBuilder).get(); return response.isAcknowledged(); 先創(chuàng)建一個空索引,這樣該索引上不會有映射,再使用下面代碼添加映射: Test public void putIndexMapping() throws Exception String ind
27、ex = "simple-index" String type = "simple-type" if(!IndexAPI.isIndexExists(client, index) ("- putIndexMapping 索引 不存在", index); return; boolean result = IndexAPI.putIndexMapping(client, index, type); ("- putIndexMapping ",result); 添加映射之后的索
28、引信息: "state": "open", "settings": "index": "creation_date": "1476108496237", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "9SR5OQJ-QLSARFjmimvs1A", "version&quo
29、t;: "created": "2030399" , "mappings": "simple-type": "properties": "college": "store": true, "type": "string" , "school": "index": "not_analyzed", "store": true, "t
30、ype": "string" , "sex": "store": true, "type": "string" , "name": "store": true, "type": "string" , "age": "store": true, "type": "long" , "aliases": 8. 別名
31、API別名API允許我們可以為已經(jīng)存在的索引創(chuàng)建別名 /* * 為索引創(chuàng)建別名 * param client * param index * param alias * return */ public static boolean addAliasIndex(Client client, String index , String alias) IndicesAdminClient indicesAdminClient = client.admin().indices(); IndicesAliasesResponse response = indicesAdminClient.prepar
32、eAliases().addAlias(index, alias).get(); return response.isAcknowledged(); 測試:下面代碼為simple-index索引創(chuàng)建一個別名為simple: Test public void addAliasIndex() throws Exception String index = "simple-index" String aliasName = "simple" boolean result = IndexAPI.addAliasIndex(client, index, alias
33、Name); ("- addAliasIndex ", result); 結(jié)果圖:9. 別名存在API別名存在API允許我們檢查是否存在至少一個我們列舉出的的別名,注意是判斷的索引別名,不是索引。我們可以在別名中使用星號通配符。 /* * 判斷別名是否存在 * param client * param aliases * return */ public static boolean isAliasExist(Client client, String. aliases) IndicesAdminClient indicesAdminClient =
34、client.admin().indices(); AliasesExistResponse response = indicesAdminClient.prepareAliasesExist(aliases).get(); return response.isExists(); 測試,下面代碼檢查以sim開頭的索引別名和test索引別名是否存在,我們列舉的索引別名只要有一個存在就會返回true。 Test public void isAliasExist() throws Exception String aliasName = "simp*" String aliasN
35、ame2 = "test" boolean result = IndexAPI.isAliasExist(client, aliasName, aliasName2); ("- isAliasExist ", result); / true 10. 獲取別名API獲取別名API可以列舉出當(dāng)前已經(jīng)定義的的別名 /* * 獲取別名 * param client * param aliases */ public static void getAliasIndex(Client client, String. aliases) Indic
36、esAdminClient indicesAdminClient = client.admin().indices(); GetAliasesResponse response = indicesAdminClient.prepareGetAliases(aliases).get(); ImmutableOpenMap<String, List<AMetaData>> aliasesMap = response.getAliases(); UnmodifiableIterator<String> iterator = aliasesMap.keysIt();
37、 while(iterator.hasNext() String key = iterator.next(); List<AliasMetaData> aliasMetaDataList = aliasesMap.get(key); for(AliasMetaData aliasMetaData : aliasMetaDataList) ("- getAliasIndex ", aliasMetaData.getAlias(); 測試,下面代碼展示以sim開頭的別名和test別名: Test public void getAliasInde
38、x() throws Exception String aliasName = "simp*" String aliasName2 = "test" IndexAPI.getAliasIndex(client, aliasName, aliasName2); / simple test 11. 刪除別名API刪除別名API允許我們刪除指定索引的別名,如果索引沒有該別名,則會報錯 /* * 刪除別名 * param client * param index * param aliases * return */ public static boolean
39、deleteAliasIndex(Client client, String index, String. aliases) IndicesAdminClient indicesAdminClient = client.admin().indices(); IndicesAliasesResponse response = indicesAdminClient.prepareAliases().removeAlias(index, aliases).get(); return response.isAcknowledged(); 測試,下面代碼刪除test-index索引的別名test: Te
40、st public void deleteAliasIndex() throws Exception String index = "test-index" String aliasName = "test" boolean result = IndexAPI.deleteAliasIndex(client, index, aliasName); ("- deleteAliasIndex ", result); / true 12. 更新設(shè)置API更新設(shè)置API允許我們更新特定索引或全部索引的設(shè)置。 /* * 更
41、新設(shè)置 * param client * param index * param settings * return */ public static boolean updateSettingsIndex(Client client, String index, Settings settings) IndicesAdminClient indicesAdminClient = client.admin().indices(); UpdateSResponse response = indicesAdminClient.prepareUpdateSettings(index).setSettings(settings).get(); return response.isAcknowled
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 問題導(dǎo)向式教學(xué)在小學(xué)信息技術(shù)課教學(xué)中的應(yīng)用
- 代理工地合同范本
- 保溫砌塊采購合同范例
- 產(chǎn)品租借合同范例
- 出租司機聘用合同范本
- 農(nóng)莊水產(chǎn)養(yǎng)殖合同范例
- 低價出售水洗設(shè)備合同范例
- 泰州吊裝施工方案公司
- 創(chuàng)作靈感合同范例范例
- 碳內(nèi)部審計問題研究
- 專題16 生活用電(3大模塊知識清單+3個易混易錯+5種方法技巧+典例真題解析)
- 癡呆的影像鑒別診斷
- 基于義務(wù)教育質(zhì)量監(jiān)測結(jié)果的德育改進(jìn)對策研究
- 2024版質(zhì)量管理培訓(xùn)
- 開展我為同學(xué)辦實事活動
- 幼兒園大班健康《硬硬的殼香香的肉》課件
- 醫(yī)科大學(xué)2024年12月五官科護(hù)理學(xué)作業(yè)考核試題答卷
- GB/T 44569.1-2024土工合成材料內(nèi)部節(jié)點強度的測定第1部分:土工格室
- 《智能網(wǎng)聯(lián)汽車智能傳感器測試與裝調(diào)》電子教案
- 機動車維修經(jīng)營備案表
- 《公務(wù)員錄用體檢操作手冊(試行)》
評論
0/150
提交評論