日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ElasticSearch创建、修改、获取、删除、索引Indice mapping和Index Template案例

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElasticSearch创建、修改、获取、删除、索引Indice mapping和Index Template案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么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:

轉載于:https://my.oschina.net/bboss/blog/1807473

總結

以上是生活随笔為你收集整理的ElasticSearch创建、修改、获取、删除、索引Indice mapping和Index Template案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。