概 述
Elasticsearch 在企業里落地的場景越來越多了,但是大家在項目里使用 Elasticsearch的姿勢也是千奇百怪,這次正好自己需要使用,所以干脆就封裝一個 elasticsearch-spring-boot-starter以供復用好了。如果不知道 spring-boot-starter該如何制作,可以參考文章《如何自制一個Spring Boot Starter并推送到遠端公服》,下面就來簡述一下自制的 elasticsearch-spring-boot-starter該如何使用。
依賴引入
<dependency><groupId>com.github.hansonwang99</groupId><artifactId>elasticsearch-spring-boot-starter</artifactId><version>0.0.8</version>
</dependency><repositories><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository>
</repositories>
配置文件
如果你還沒有一個屬于自己的 Elasticsearch集群,可以參考文章 《CentOS7 上搭建多節點 Elasticsearch集群》來一步步搭建之,本文實驗所用的集群即來源于此。 elasticsearch:host: 192.168.31.75httpPort: 9200tcpPort: 9300clusterName: codesheepdocFields: title,filecontentauth:enable: false
各個字段解釋如下:
- host:Elasticsearch 節點地址
- httpPort: Elasticsearch REST端口
- tcpPort:Elasticsearch TCP端口
- clusterName:集群名
- docFields:文檔字段,以英文逗號間隔,比如我這里的業務場景是文檔包含 標題(title)和 內容(filecontent)字段
- auth:是否需要權限認證
由于我這里安裝的實驗集群并無 x-pack權限認證的加持,因此無需權限認證,實際使用的集群或者阿里云上的 Elasticsearch集群均有完善的 x-pack權限認證,此時可以加上用戶名/密碼的配置:
elasticsearch:host: 192.168.199.75httpPort: 9200tcpPort: 9300clusterName: codesheepdocFields: title,filecontentauth:enable: trueusername: elasticsearchpassword: xxxxxx
用法例析
@Autowired
private ISearchService iSearchService;@Autowired
private DocModel docModel;
這些都是在 elasticsearch-spring-boot-starter中定義的
public String createIndex() throws IOException {IndexModel indexModel = new IndexModel();indexModel.setIndexName("testindex2"); // 注意索引名字必須小寫,否則ES拋異常indexModel.setTypeName("testtype2");indexModel.setReplicaNumber( 2 ); // 兩個節點,因此兩個副本indexModel.setShardNumber( 3 );XContentBuilder builder = null;builder = XContentFactory.jsonBuilder();builder.startObject();{builder.startObject("properties");{builder.startObject("title");{builder.field("type", "text");builder.field("analyzer", "ik_max_word");}builder.endObject();builder.startObject("filecontent");{builder.field("type", "text");builder.field("analyzer", "ik_max_word");builder.field("term_vector", "with_positions_offsets");}builder.endObject();}builder.endObject();}builder.endObject();indexModel.setBuilder( builder );Boolean res = iSearchService.createIndex(indexModel);if( true==res )return "創建索引成功";elsereturn "創建索引失敗";
}
public String deleteIndex() {return (iSearchService.deleteIndex("testindex2")==true) ? "刪除索引成功":"刪除索引失敗";
}
if ( existIndex(indexName) ) {...
} else {...
}
public String insertSingleDoc( ) {SingleDoc singleDoc = new SingleDoc();singleDoc.setIndexName("testindex2");singleDoc.setTypeName("testtype2");Map<String,Object> doc = new HashMap<>();doc.put("title","人工智能標題1");doc.put("filecontent","人工智能內容1");singleDoc.setDocMap(doc);return ( true== iSearchService.insertDoc( singleDoc ) ) ? "插入單個文檔成功" : "插入單個文檔失敗";
}
public String insertDocBatch() {BatchDoc batchDoc = new BatchDoc();batchDoc.setIndexName("testindex2");batchDoc.setTypeName("testtype2");Map<String,Object> doc1 = new HashMap<>();doc1.put("title","人工智能標題1");doc1.put("filecontent","人工智能內容1");Map<String,Object> doc2 = new HashMap<>();doc2.put("title","人工智能標題2");doc2.put("filecontent","人工智能內容2");Map<String,Object> doc3 = new HashMap<>();doc3.put("title","人工智能標題3");doc3.put("filecontent","人工智能內容3");Map<String,Object> doc4 = new HashMap<>();doc4.put("title","人工智能標題4");doc4.put("filecontent","人工智能內容4");List<Map<String,Object>> docList = new ArrayList<>();docList.add( doc1 );docList.add( doc2 );docList.add( doc3 );docList.add( doc4 );batchDoc.setBatchDocMap( docList );return ( true== iSearchService.insertDocBatch( batchDoc ) ) ? "批量插入文檔成功" : "批量插入文檔失敗";
}
public List<Map<String,Object>> searchDoc() {SearchModel searchModel = new SearchModel();searchModel.setIndexName( "testindex2" );List<String> fields = new ArrayList<>();fields.add("title");fields.add("filecontent");fields.add("id");searchModel.setFields( fields );searchModel.setKeyword( "人工" );searchModel.setPageNum( 1 );searchModel.setPageSize( 5 );return iSearchService.queryDocs( searchModel );
}
public String deleteDoc() {SingleDoc singleDoc = new SingleDoc();singleDoc.setIndexName("testindex2");singleDoc.setTypeName("testtype2");singleDoc.setId("vPHMY2cBcGZ3je_1EgIM");return (true== iSearchService.deleteDoc(singleDoc)) ? "刪除文檔成功" : "刪除文檔失敗";
}
public String deleteDocBatch() {BatchDoc batchDoc = new BatchDoc();batchDoc.setIndexName("testindex2");batchDoc.setTypeName("testtype2");List<String> ids = new ArrayList<>();ids.add("vfHMY2cBcGZ3je_1EgIM");ids.add("vvHMY2cBcGZ3je_1EgIM");batchDoc.setDocIds( ids );return ( true== iSearchService.deleteDocBatch(batchDoc) ) ? "批量刪除文檔成功" : "批量刪除文檔失敗";
}
public String updateDoc( @RequestBody SingleDoc singleDoc ) {SingleDoc singleDoc = new SingleDoc();singleDoc.setId("wPH6Y2cBcGZ3je_1OwI7");singleDoc.setIndexName("testindex2");singleDoc.setTypeName("testtype2");Map<String,Object> doc = new HashMap<>();doc.put("title","人工智能標題(更新后)");doc.put("filecontent","人工智能內容(更新后)");singleDoc.setUpdateDocMap(doc);return (true== iSearchService.updateDoc(singleDoc)) ? "更新文檔成功" : "更新文檔失敗";
}
后 記
由于能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!
- My Personal Blog:CodeSheep 程序羊
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的自制一个 elasticsearch-spring-boot-starter的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。