成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

ElasticSearch Java API只需十招,輕松掌握變專家!

開發(fā) 前端
Java API是Elasticsearch提供的官方客戶端,它允許Java開發(fā)者輕松地與Elasticsearch服務(wù)器進(jìn)行交互。下面是一些關(guān)于如何使用Java API來調(diào)用Elasticsearch的常用方法。

環(huán)境:springboot2.4.12 + elasticsearch7.8.0

      Elasticsearch是一種開源的、分布式的、實時的搜索和分析引擎。它允許你存儲,搜索和分析大量數(shù)據(jù),通常用于為網(wǎng)站或應(yīng)用程序提供強大的搜索功能。

      Java API是Elasticsearch提供的官方客戶端,它允許Java開發(fā)者輕松地與Elasticsearch服務(wù)器進(jìn)行交互。下面是一些關(guān)于如何使用Java API來調(diào)用Elasticsearch的常用方法。

注意:這里為了方便使用springboot項目(避免還要單獨引用其它包)

相關(guān)依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>7.8.0</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-high-level-client</artifactId>
  <version>7.8.0</version><!--$NO-MVN-MAN-VER$-->
</dependency>

索引操作

高級別的Rest客戶端對象

private static RestHighLevelClient client = 
  new RestHighLevelClient(RestClient.builder(
    new HttpHost("localhost", 9200, "http"))) ;

1. 創(chuàng)建索引

public static void createIndex(String index) throws Exception {
  CreateIndexRequest request = new CreateIndexRequest(index) ;
  CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT) ;
  boolean ack = response.isAcknowledged() ;
  System.out.println("ack = " + ack) ;
}

2. 查看索引

public static void viewIndex(String index) throws Exception {
  GetIndexRequest request = new GetIndexRequest(index) ;
  GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT) ;
  System.out.println("aliases: " + response.getAliases() + "\n"
    + "mappings: " + response.getMappings() + "\n"
    + "settings: " + response.getSettings()) ;
}

3. 刪除索引

public static void deleteIndex(String index) throws Exception {
  DeleteIndexRequest request = new DeleteIndexRequest(index) ;
  AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT) ;
  System.out.println("ack: " + response.isAcknowledged()) ;
}

文檔操作

private static RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"))) ;

1. 創(chuàng)建文檔

public static void createDoc(String index, Users users) throws Exception {
  IndexRequest request = new IndexRequest() ;
  // 設(shè)置索引及唯一標(biāo)識
  request.index(index).id("1001") ;
  ObjectMapper objectMapper = new ObjectMapper() ;
  String jsonString = objectMapper.writeValueAsString(users) ;
  // 添加文檔數(shù)據(jù)及數(shù)據(jù)格式
  request.source(jsonString, XContentType.JSON) ;
  IndexResponse response = client.index(request, RequestOptions.DEFAULT) ;
  System.out.println("_index: " + response.getIndex() + "\n"
      + "_id: " + response.getId() + "\n"
      + "_result: " + response.getResult()) ;
}

2. 更新文檔

public static void updateDoc(String index, String id) throws Exception {
  UpdateRequest request = new UpdateRequest() ;
  // 配置修改參數(shù)
  request.index(index).id(id) ;
  Map<String, Object> source = new HashMap<>() ;
  source.put("sex", "女") ;
  request.doc(source, XContentType.JSON) ;
  UpdateResponse response = client.update(request, RequestOptions.DEFAULT) ;
  System.out.println("_index: " + response.getIndex() + "\n"
      + "_id: " + response.getId() + "\n"
      + "_result: " + response.getResult()) ;
}

3. 查詢文檔

public static void viewDoc(String index, String id) throws Exception {
  GetRequest request = new GetRequest().index(index).id(id) ;
  GetResponse response = client.get(request, RequestOptions.DEFAULT) ;
  System.out.println("_index: " + response.getIndex() + "\n"
      + "_type: " + response.getType() + "\n"
      + "_id: " + response.getId() + "\n"
      + "source: " + response.getSourceAsString()) ;
}

4. 刪除文檔

public static void deleteIndex(String index, String id) throws Exception {
  DeleteRequest request = new DeleteRequest().index(index).id(id) ;
  DeleteResponse response = client.delete(request, RequestOptions.DEFAULT) ;
  System.out.println(response.toString()) ;
}

5. 批量操作

public static void batchOperator(String index) throws Exception {
  BulkRequest request = new BulkRequest() ;
  request.add(new IndexRequest().index(index).id("1002").source(XContentType.JSON, "name","老六", "sex", "男", "age", 20)) ;
  request.add(new IndexRequest().index(index).id("1003").source(XContentType.JSON, "name","外網(wǎng)", "sex", "女", "age", 10)) ;
  request.add(new IndexRequest().index(index).id("1004").source(XContentType.JSON, "name","莉莉", "sex", "女", "age", 35)) ;
  BulkResponse response = client.bulk(request, RequestOptions.DEFAULT) ;
  System.out.println("took: " + response.getTook() + "\n"
      + "items: " + new ObjectMapper().writeValueAsString(response.getItems())) ;
}

6. 高級查詢

public static void highSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.matchAllQuery()) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
}

7. term精確查詢

public static void highTermSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.termQuery("age", "20")) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
}

8. 分頁查詢

public static void highPagingSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.matchAllQuery()) ;
  builder.from(1) ;
  builder.size(2) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
}

9. 分頁&排序查詢

public static void highPagingAndSortSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.matchAllQuery()) ;
  builder.from(0) ;
  builder.size(20) ;
  builder.sort("age", SortOrder.ASC) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
}

10. 分頁&排序&過濾字段查詢

public static void highPagingAndSortAndFilterFieldSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.matchAllQuery()) ;
  builder.from(0) ;
  builder.size(20) ;
  builder.sort("age", SortOrder.ASC) ;
  String[] includes = {"name"} ;
  String[] excludes = {} ;
  builder.fetchSource(includes, excludes) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
}

11. 范圍查詢

public static void highBoolSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.matchAllQuery()) ;
  builder.from(0) ;
  builder.size(20) ;
  builder.sort("age", SortOrder.ASC) ;
  RangeQueryBuilder rangeBuilder = QueryBuilders.rangeQuery("age");
  rangeBuilder.gte(15) ;
  rangeBuilder.lte(30) ;
  builder.query(rangeBuilder) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
}

12. 高亮查詢

public static void highHighLightSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.query(QueryBuilders.matchQuery("name", "莉莉")) ;
  HighlightBuilder highLightBuilder = new HighlightBuilder() ;
  highLightBuilder.preTags("<font color='red'>") ;
  highLightBuilder.postTags("</font>") ;
  highLightBuilder.field("name") ;
  builder.highlighter(highLightBuilder) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString() + "\n"
        + "highlight: " + hit.getHighlightFields()) ;
  }
}

13. 聚合查詢

public static void highAggsSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.aggregation(AggregationBuilders.avg("avg_age").field("age")) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString())  ;
  }
  System.out.println(((ParsedAvg)response.getAggregations().iterator().next()).getValue()) ;
}

14. 分組統(tǒng)計

public static void highGroupSearch(String index) throws Exception {
  SearchRequest request = new SearchRequest().indices(index) ;
  SearchSourceBuilder builder = new SearchSourceBuilder() ;
  builder.aggregation(AggregationBuilders.terms("age_groupby").field("age")) ;
  request.source(builder) ;
  SearchResponse response = client.search(request, RequestOptions.DEFAULT) ;
  SearchHits hits = response.getHits() ;
  System.out.println("took: " + response.getTook() + "\n"
      + "timeout: " + response.isTimedOut() + "\n"
      + "total: " + hits.getTotalHits() + "\n"
      + "MaxScore: " + hits.getMaxScore()) ;
  for (SearchHit hit : hits) {
    System.out.println(hit.getSourceAsString()) ;
  }
  System.out.println(response) ;
}

完畢!!!

責(zé)任編輯:武曉燕 來源: Spring全家桶實戰(zhàn)案例源碼
相關(guān)推薦

2023-10-04 17:31:21

項目部署軟件包

2019-11-26 09:47:50

代碼開發(fā)工具

2016-08-23 00:15:28

2010-06-12 13:49:16

學(xué)習(xí)UML

2019-02-15 09:00:00

機器學(xué)習(xí)API人工智能

2023-09-26 22:12:13

數(shù)據(jù)倉庫Doris

2010-01-06 17:51:26

Linux關(guān)機命令

2010-09-16 11:07:28

裁員

2011-07-05 13:08:22

虛擬化網(wǎng)絡(luò)虛擬化VEPA

2009-10-12 13:18:55

RHEL 4內(nèi)核

2014-10-30 16:12:55

編程技術(shù)算法

2012-07-17 10:54:49

AJAX

2023-09-13 08:00:00

MLOps數(shù)據(jù)科學(xué)

2024-06-12 00:00:01

Java函數(shù)式接口

2010-07-28 10:28:53

Facebook

2010-09-28 14:15:34

清除DLL木馬

2022-12-07 13:58:26

Git命令

2009-12-16 14:26:19

Linux VMwar

2010-01-04 17:35:32

Silverlight

2009-11-12 10:32:47

ADO.NET技術(shù)
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 色综合一区 | 91麻豆产精品久久久久久夏晴子 | 久久久久久高潮国产精品视 | 欧美一级在线观看 | 国产综合视频 | 成人在线免费观看视频 | 中文字幕av在线一二三区 | 国产免费一区二区 | 日韩高清一区二区 | 天天天操 | 亚洲一区综合 | av电影一区 | 亚洲精品自拍视频 | 欧美日韩在线视频一区 | 精品国产乱码久久久 | 91精品久久久久久久久中文字幕 | 国产中文字幕在线观看 | 亚洲精品久久国产高清情趣图文 | 亚洲欧美激情精品一区二区 | 成人av高清 | 欧美日韩亚洲一区 | 日本三级全黄三级a | 国产成在线观看免费视频 | 亚洲一区二区精品视频 | 国产成人午夜精品影院游乐网 | 日韩一区二区三区精品 | 91精品国产欧美一区二区 | 欧美a√ | 国内精品视频一区二区三区 | 日本午夜精品 | 在线午夜| 久久精品亚洲精品国产欧美 | 中文字幕一区二区三区精彩视频 | 成人欧美一区二区三区在线播放 | 成人做爰www免费看视频网站 | 午夜男人免费视频 | 91久久国产综合久久 | 中文字幕亚洲视频 | 久久久久久www | 久久人人网 | 中文字幕精品视频在线观看 |