為什么80%的碼農都做不了架構師?>>> ??
The best elasticsearch highlevel java rest api-----bboss
ElasticSearch客戶端框架bboss的ClientInterface 接口提供了創建/修改、獲取、刪除索引Indice和IndexTemplate的方法,本文舉例說明其使用方法。
1 準備工作
參考文檔在項目中導入Elasticsearch客戶端:集成Elasticsearch Restful API案例分享
本文除了介紹索引Indice和Index Template的創建修改方法,還可以看到如何在dsl中添加注釋的用法:
單行注釋
## 注釋內容
多行注釋
#*。。。。注釋內容。。。*#
更多bboss dsl配置和定義的內容,參考文檔:高性能elasticsearch ORM開發庫使用介紹?章節【5.3 配置es查詢dsl腳本語法】
2 定義創建Indice的dsl腳本
在配置文件-esmapper/demo.xml中定義一個名稱為createDemoIndice的dsl腳本:
<!--創建demo需要的索引表結構--><property name="createDemoIndice"><![CDATA[{"settings": {"number_of_shards": 6,"index.refresh_interval": "5s"},"mappings": {"demo": {"properties": {"demoId":{"type":"long"},"contentbody": {"type": "text" ##定義text類型的全文檢索字段},"agentStarttime": {"type": "date"## ,"format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"},"applicationName": {"type": "text",##定義text類型的全文檢索字段"fields": { ##定義精確查找的內部keyword字段"keyword": {"type": "keyword"}}},"name": {"type": "keyword"}}}}}]]></property>
3 創建indice/判斷indice是否存在/刪除indice
根據上面定義的dsl腳本文件初始化ClientInterface對象,并創建索引表demo:
public void testCreateIndice(){//創建加載配置文件的客戶端工具,單實例多線程安全ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");try {//判讀索引表demo是否存在,存在返回true,不存在返回falseboolean exist = clientUtil.existIndice("demo");//如果索引表demo已經存在先刪除mappingif(exist)clientUtil.dropIndice("demo");//創建索引表democlientUtil.createIndiceMapping("demo",//索引表名稱"createDemoIndice");//索引表mapping dsl腳本名稱,在esmapper/demo.xml中定義createDemoIndice//獲取修改后的索引mapping結構String mapping = clientUtil.getIndice("demo");System.out.println(mapping);} catch (ElasticSearchException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
4 定義索引Template dsl腳本
通過定義索引模板,定義表結構相同,但是索引表名稱不同的索引表的建表模板,通過index_patterns中對應的模式名稱來匹配索引模板適用的索引表:
<property name="demoTemplate"><![CDATA[{"index_patterns": "demo-*", ## 5.x版本中請使用語法:"template": "demo-*""settings": {"number_of_shards": 30, ##定義分片數"number_of_replicas" : 2, ##定義副本數"index.refresh_interval": "5s" ## 定義索引寫入刷新時間間隔},"mappings": {"demo": {"properties": {"contentbody": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}},"agentStarttime": {"type": "date","format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"},"applicationName": {"type": "text","fields": {"keyword": {"type": "keyword","ignore_above": 256}}}}}}}]]></property>
5 創建/獲取/刪除索引表模板
public void testCreateTempate() throws ParseException{ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");//創建模板String response = clientUtil.createTempate("demotemplate_1",//模板名稱"demoTemplate");//模板對應的腳本名稱,在esmapper/demo.xml中配置System.out.println("createTempate-------------------------");System.out.println(response);//創建結果//獲取模板/*** 指定模板* /_template/demoTemplate_1* /_template/demoTemplate** 所有模板 /_template**/String template = clientUtil.executeHttp("/_template/demotemplate_1",ClientUtil.HTTP_GET);System.out.println("HTTP_GET-------------------------");System.out.println(template);ElasticSearchHelper.getRestClientUtil().deleteTempate("demotemplate_1"); }
6 修改和獲取索引表結構
修改先前創建的demo表,為其中的type demo增加email關鍵字段
定義dsl結構-esmapper/demo.xml
<!--修改demo 索引表的結構,增加email字段https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html--><property name="updateDemoIndice"><![CDATA[{"properties": {"email": {"type": "keyword"}}}]]></property>
修改和獲取mapping結構的方法:
public void updateDemoIndice(){ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil(mappath);//修改索引表demo中type為demo的mapping結構,增加email字段,對應的dsl片段updateDemoIndice定義在esmapper/demo.xml文件中String response = clientUtil.executeHttp("demo/_mapping/demo","updateDemoIndice",ClientUtil.HTTP_PUT);System.out.println(response);//獲取修改后的索引mapping結構String mapping = clientUtil.getIndice("demo");System.out.println(mapping);}
7 案例源碼工程下載
https://github.com/bbossgroups/eshelloword-booter
https://gitee.com/bboss/eshelloword-booter
8 參考文檔
https://my.oschina.net/bboss/blog/1556866
9 開發交流
elasticsearch技術交流:166471282
elasticsearch: