您好,登錄后才能下訂單哦!
本篇內容主要講解“如何實現ElasticSearch的JavaAPI”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何實現ElasticSearch的JavaAPI”吧!
有人說學習一項技術最好的資料是官方文檔,對大部分技術來說確實是這樣的。但是官方文檔不一定適合每個人去看,比如一個初學者,直接讓他看Spring的官方文檔,其實是不合適的。今天我會結合ElasticSearch的一個客戶端官方文檔介紹ES在Java中的API應用。
官方文檔不一定好找,這里直接給出地址:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.6/index.html
你可以選擇自己對應版本的文檔來參考,我這里選擇的是7.6版本,選用的是Java High Level REST Client。
首先需要創建一個項目,創建項目就不介紹了,引入ES核心依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
為了防止版本問題導致服務器客戶端沖突,盡量將ES的版本設置的和自己安裝的服務器端版本一致:
<properties> <java.version>1.8</java.version> <elasticsearch.version>7.6.1</elasticsearch.version> </properties>
后續還會用一些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; } }
后續會用到實體類,這里先提供了:
@Data @AllArgsConstructor public class User { private String name; private String address; }
接下來的操作都在SpringBootTest中進行,首先通過@Autowired注入
RestHighLevelClient 對象 @SpringBootTest class ElasticsearchdemoApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; }
還是按照學習ES語法的順序學習語法,找到索引API ,API中的操作很多,我主要選一些重要的講一下
創建索引的主要對象是CreateIndexRequest
@Test public void testCreateIndex() throws IOException { //創建索引 CreateIndexRequest request=new CreateIndexRequest("test_index"); CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse.isAcknowledged()); }
創建CreateIndexRequest對象,設置相關屬性,比如切片數,副本數,超時時間等等,然后通過restHighLevelClient創建索引,獲得一個結果響應。
//設置分片和副本 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()); }
對索引的操作可以通過圖形化界面來實現,了解增刪改即可。
文檔的API分為單個文檔處理和批量文檔處理,我會介紹單個文檔的增刪改查和一個批量文檔API
在用語法創建文檔的時候,是這樣的:
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()); }
其實兩者十分相似,這也是學技術要先學基礎的原因。有了基礎一看就懂。
獲取文檔和判斷是否存在這里放在一起寫,請求對象都是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()); }
批量創建文檔用到了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); }
到此,相信大家對“如何實現ElasticSearch的JavaAPI”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。