您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何實現(xiàn)ElasticSearch的JavaAPI”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“如何實現(xiàn)ElasticSearch的JavaAPI”吧!
有人說學(xué)習(xí)一項技術(shù)最好的資料是官方文檔,對大部分技術(shù)來說確實是這樣的。但是官方文檔不一定適合每個人去看,比如一個初學(xué)者,直接讓他看Spring的官方文檔,其實是不合適的。今天我會結(jié)合ElasticSearch的一個客戶端官方文檔介紹ES在Java中的API應(yīng)用。
官方文檔不一定好找,這里直接給出地址:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.6/index.html
你可以選擇自己對應(yīng)版本的文檔來參考,我這里選擇的是7.6版本,選用的是Java High Level REST Client。
首先需要創(chuàng)建一個項目,創(chuàng)建項目就不介紹了,引入ES核心依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
為了防止版本問題導(dǎo)致服務(wù)器客戶端沖突,盡量將ES的版本設(shè)置的和自己安裝的服務(wù)器端版本一致:
<properties> <java.version>1.8</java.version> <elasticsearch.version>7.6.1</elasticsearch.version> </properties>
后續(xù)還會用一些json和web以及測試的操作,引入這些依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency>
編寫個配置類注入restHighLevelClient對象:
@Configuration public class ElasticSearchConfig { @Bean public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient client=new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.78.128",9200,"http") ) ); return client; } }
后續(xù)會用到實體類,這里先提供了:
@Data @AllArgsConstructor public class User { private String name; private String address; }
接下來的操作都在SpringBootTest中進行,首先通過@Autowired注入
RestHighLevelClient 對象 @SpringBootTest class ElasticsearchdemoApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; }
還是按照學(xué)習(xí)ES語法的順序?qū)W習(xí)語法,找到索引API ,API中的操作很多,我主要選一些重要的講一下
創(chuàng)建索引的主要對象是CreateIndexRequest
@Test public void testCreateIndex() throws IOException { //創(chuàng)建索引 CreateIndexRequest request=new CreateIndexRequest("test_index"); CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse.isAcknowledged()); }
創(chuàng)建CreateIndexRequest對象,設(shè)置相關(guān)屬性,比如切片數(shù),副本數(shù),超時時間等等,然后通過restHighLevelClient創(chuàng)建索引,獲得一個結(jié)果響應(yīng)。
//設(shè)置分片和副本 request.settings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2) );
獲取一個索引,主要對象是GetIndexRequest
@Test public void testGetIndex() throws IOException { //獲取索引 GetIndexRequest request=new GetIndexRequest("test_index"); GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT); System.out.println(getIndexResponse); }
用的也是GetIndexRequest
@Test public void testExistsIndex() throws IOException { //獲取索引 GetIndexRequest request=new GetIndexRequest("test_index"); boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); }
刪除索引的請求對象是DeleteIndexRequest
@Test public void testDeleteIndex() throws IOException { DeleteIndexRequest request=new DeleteIndexRequest("test_index"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); }
對索引的操作可以通過圖形化界面來實現(xiàn),了解增刪改即可。
文檔的API分為單個文檔處理和批量文檔處理,我會介紹單個文檔的增刪改查和一個批量文檔API
在用語法創(chuàng)建文檔的時候,是這樣的:
PUT http://ip:port/索引名/類型名/文檔id { "name":"javayz", "address":"hz" }
使用代碼是這樣的:請求對象為IndexRequest
@Test public void testCreateDoc() throws IOException { // PUT http://ip:port/索引名/類型名/文檔id User user=new User("javayz","hz"); IndexRequest request=new IndexRequest("text_index"); request.id("1"); request.source(JSON.toJSONString(user), XContentType.JSON); IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(index.status()); }
其實兩者十分相似,這也是學(xué)技術(shù)要先學(xué)基礎(chǔ)的原因。有了基礎(chǔ)一看就懂。
獲取文檔和判斷是否存在這里放在一起寫,請求對象都是GetRequest :
@Test public void testGetDoc() throws IOException{ GetRequest request=new GetRequest("text_index"); request.id("1"); boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT); if (exists){ GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT); String sourceAsString = documentFields.getSourceAsString(); System.out.println(sourceAsString); }else{ System.out.println(exists); } }
更新文檔的請求對象是UpdateRequest
@Test public void testUpdateDoc() throws IOException{ UpdateRequest request=new UpdateRequest("text_index","1"); User user=new User("javayz2","hz"); request.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT); System.out.println(response.status()); }
刪除文檔的請求對象是DeleteRequest
@Test public void testDeleteDoc() throws IOException{ DeleteRequest request=new DeleteRequest("text_index","1"); DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT); System.out.println(deleteResponse.status()); }
批量創(chuàng)建文檔用到了BulkRequest ,具體的使用方式和單體很相似,只不過是把多個請求聚合到一個bulk中一起提交。
@Test public void testBulkRequest() throws IOException{ BulkRequest request=new BulkRequest(); request.timeout("10s"); ArrayList<User> list=new ArrayList<>(); list.add(new User("javayz1","hz")); list.add(new User("javayz2","hz")); //往BulkRequest對象中add文檔 list.stream().forEach(x->{ request.add(new IndexRequest("text_index").source(JSON.toJSONString(x),XContentType.JSON)); }); BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT); System.out.println(bulk.status()); }
查詢操作用最常用的就是match(模糊查詢)和term(精確查詢),介紹最常用的查詢方式:
@Test public void testSearch() throws IOException { SearchRequest request = new SearchRequest("text_index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //匹配條件 MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "javayz1"); searchSourceBuilder.query(matchQueryBuilder); request.source(searchSourceBuilder); SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT); System.out.println(search); }
到此,相信大家對“如何實現(xiàn)ElasticSearch的JavaAPI”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。