elaticsearch(3)

整合springboot

1.整合依賴

注意依賴版本和安裝的版本一致

<properties>
? ? <java.version>1.8</java.version>
? ? <!-- 統一版本 -->
? ? <elasticsearch.version>7.6.1</elasticsearch.version>
</properties>

導入elasticsearch

<dependency>
? ? <groupId>org.springframework.boot</groupId>
? ? <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

提前導入fastjson、lombok

<dependency>
? ? <groupId>com.alibaba</groupId>
? ? <artifactId>fastjson</artifactId>
? ? <version>1.2.70</version>
</dependency>
<!-- lombok需要安裝插件 -->
<dependency>
? ? <groupId>org.projectlombok</groupId>
? ? <artifactId>lombok</artifactId>
? ? <optional>true</optional>
</dependency>

2.編寫配置類

@Configuration
public class ElasticSearchConfig {
? ? // 注冊 rest高級客戶端?
? ? @Bean
? ? public RestHighLevelClient restHighLevelClient(){
? ? ? ? RestHighLevelClient client = new RestHighLevelClient(
? ? ? ? ? ? ? ? RestClient.builder(
? ? ? ? ? ? ? ? ? ? ? ? new HttpHost("127.0.0.1",9200,"http")
? ? ? ? ? ? ? ? )
? ? ? ? );
? ? ? ? return client;
? ? }
}

3.創建實體

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
? ? private static final long serialVersionUID = -3843548915035470817L;
? ? private String name;
? ? private Integer age;
}

4.測試

1.注入RestHighLevelClient

@Autowired
public RestHighLevelClient restHighLevelClient;

4.2.測試創建索引

// 測試索引的創建, Request PUT liuyou_index
@Test
public void testCreateIndex() throws IOException {
? ? CreateIndexRequest request = new CreateIndexRequest("liuyou_index");
? ? CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
? ? System.out.println(response.isAcknowledged());// 查看是否創建成功
? ? System.out.println(response);// 查看返回對象
? ? restHighLevelClient.close();
}

3.獲得索引

// 測試獲取索引,并判斷其是否存在
@Test
public void testIndexIsExists() throws IOException {
? ? GetIndexRequest request = new GetIndexRequest("index");
? ? boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
? ? System.out.println(exists);// 索引是否存在
? ? restHighLevelClient.close();
}

4.刪除索引

// 測試索引刪除
@Test
public void testDeleteIndex() throws IOException {
? ? DeleteIndexRequest request = new DeleteIndexRequest("liuyou_index");
? ? AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
? ? System.out.println(response.isAcknowledged());// 是否刪除成功
? ? restHighLevelClient.close();
}

4.5測試創建文檔

// 測試添加文檔(先創建一個User實體類,添加fastjson依賴)
@Test
public void testAddDocument() throws IOException {
? ? // 創建一個User對象
? ? User liuyou = new User("liuyou", 18);
? ? // 創建請求
? ? IndexRequest request = new IndexRequest("liuyou_index");
? ? // 制定規則 PUT /liuyou_index/_doc/1
? ? request.id("1");// 設置文檔ID
? ? request.timeout(TimeValue.timeValueMillis(1000));// request.timeout("1s")
? ? // 將我們的數據放入請求中
? ? request.source(JSON.toJSONString(liuyou), XContentType.JSON);
? ? // 客戶端發送請求,獲取響應的結果
? ? IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
? ? System.out.println(response.status());// 獲取建立索引的狀態信息 CREATED
? ? System.out.println(response);// 查看返回內容 IndexResponse[index=liuyou_index,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
}

6.獲取文檔信息

// 測試獲得文檔信息
@Test
public void testGetDocument() throws IOException {
? ? GetRequest request = new GetRequest("liuyou_index","1");
? ? GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
? ? System.out.println(response.getSourceAsString());// 打印文檔內容
? ? System.out.println(request);// 返回的全部內容和命令是一樣的
? ? restHighLevelClient.close();
}

7.獲取文檔

// 獲取文檔,判斷是否存在 get /liuyou_index/_doc/1
@Test
public void testDocumentIsExists() throws IOException {
? ? GetRequest request = new GetRequest("liuyou_index", "1");
? ? // 不獲取返回的 _source的上下文了
? ? request.fetchSourceContext(new FetchSourceContext(false));
? ? request.storedFields("_none_");
? ? boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
? ? System.out.println(exists);
}

8.文檔更新

// 測試更新文檔內容
@Test
public void testUpdateDocument() throws IOException {
? ? UpdateRequest request = new UpdateRequest("liuyou_index", "1");
? ? User user = new User("lmk",11);
? ? request.doc(JSON.toJSONString(user),XContentType.JSON);
? ? UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
? ? System.out.println(response.status()); // OK
? ? restHighLevelClient.close();
}

9.文檔刪除

// 測試刪除文檔
@Test
public void testDeleteDocument() throws IOException {
? ? DeleteRequest request = new DeleteRequest("liuyou_index", "1");
? ? request.timeout("1s");
? ? DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
? ? System.out.println(response.status());// OK
}

10.文檔查詢

// 查詢
// SearchRequest 搜索請求
// SearchSourceBuilder 條件構造
// HighlightBuilder 高亮
// TermQueryBuilder 精確查詢
// MatchAllQueryBuilder
// xxxQueryBuilder ...
@Test
public void testSearch() throws IOException {
? ? // 1.創建查詢請求對象
? ? SearchRequest searchRequest = new SearchRequest();
? ? // 2.構建搜索條件
? ? SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
? ? // (1)查詢條件 使用QueryBuilders工具類創建
? ? // 精確查詢
? ? TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "liuyou");
? ? // ? ? ? ?// 匹配查詢
? ? // ? ? ? ?MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
? ? // (2)其他<可有可無>:(可以參考 SearchSourceBuilder 的字段部分)
? ? // 設置高亮
? ? searchSourceBuilder.highlighter(new HighlightBuilder());
? ? // ? ? ? ?// 分頁
? ? // ? ? ? ?searchSourceBuilder.from();
? ? // ? ? ? ?searchSourceBuilder.size();
? ? searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
? ? // (3)條件投入
? ? searchSourceBuilder.query(termQueryBuilder);
? ? // 3.添加條件到請求
? ? searchRequest.source(searchSourceBuilder);
? ? // 4.客戶端查詢請求
? ? SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
? ? // 5.查看返回結果
? ? SearchHits hits = search.getHits();
? ? System.out.println(JSON.toJSONString(hits));
? ? System.out.println("=======================");
? ? for (SearchHit documentFields : hits.getHits()) {
? ? ? ? System.out.println(documentFields.getSourceAsMap());
? ? }
}

11.批量添加文檔

// 特殊的,真的項目一般會 批量插入數據
@Test
public void testBulk() throws IOException {
? ? BulkRequest bulkRequest = new BulkRequest();
? ? bulkRequest.timeout("10s");
? ? ArrayList<User> users = new ArrayList<>();
? ? users.add(new User("liuyou-1",1));
? ? users.add(new User("liuyou-2",2));
? ? users.add(new User("liuyou-3",3));
? ? users.add(new User("liuyou-4",4));
? ? users.add(new User("liuyou-5",5));
? ? users.add(new User("liuyou-6",6));
? ? // 批量請求處理
? ? for (int i = 0; i < users.size(); i++) {
? ? ? ? bulkRequest.add(
? ? ? ? ? ? ? ? // 這里是數據信息
? ? ? ? ? ? ? ? new IndexRequest("bulk")
? ? ? ? ? ? ? ? ? ? ? ? .id(""+(i + 1)) // 沒有設置id 會自定生成一個隨機id
? ? ? ? ? ? ? ? ? ? ? ? .source(JSON.toJSONString(users.get(i)),XContentType.JSON)
? ? ? ? );
? ? }
? ? BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
? ? System.out.println(bulk.status());// ok
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/43412.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/43412.shtml
英文地址,請注明出處:http://en.pswp.cn/news/43412.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據結構算法--1 順序查找二分查找

順序查找時間復雜度為O(n) 我們可以借助Python中的函數enumerate,通過enumerate遍歷列表返回其索引和值 def linnear_search(li, val):for ind, v in enumerate(li):if v val:return indelse:return None 也可以通過列表長度依次遍歷: def linear_search(li, val): # …

瀏覽器渲染原理 - 輸入url 回車后發生了什么

目錄 渲染時間點渲染流水線1&#xff0c;解析&#xff08;parse&#xff09;HTML1.1&#xff0c;DOM樹1.2&#xff0c;CSSOM樹1.3&#xff0c;解析時遇到 css 是怎么做的1.4&#xff0c;解析時遇到 js 是怎么做的 2&#xff0c;樣式計算 Recalculate style3&#xff0c;布局 la…

創建react native項目的筆記

創建react native項目的筆記 重新下載 ruby安裝 watchman安裝 cocoapods安裝 react native 項目創建好項目后安裝 ios 依賴清除設備緩存安裝 android 依賴鏈接 網易 mumu 模擬器安裝路由 Navigation頁面之間的跳轉邏輯自定義頭部樣式判斷不同設備平臺代碼示例安裝 Axios安裝本地…

java mysql druid mybatis-plus里使用多表刪除出錯的一種處理方式

今天在出來多表刪除的時候在mapper.xml用了下面的多個delete語句 <?xml version"1.0" encoding"UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"…

Spring Cloud 系列之OpenFeign:(7)鏈路追蹤zipkin

傳送門 Spring Cloud Alibaba系列之nacos&#xff1a;(1)安裝 Spring Cloud Alibaba系列之nacos&#xff1a;(2)單機模式支持mysql Spring Cloud Alibaba系列之nacos&#xff1a;(3)服務注冊發現 Spring Cloud 系列之OpenFeign&#xff1a;(4)集成OpenFeign Spring Cloud …

PHP酒店點菜管理系統mysql數據庫web結構apache計算機軟件工程網頁wamp

一、源碼特點 PHP 酒店點菜管理系統是一套完善的web設計系統&#xff0c;對理解php編程開發語言有幫助&#xff0c;系統具有完整的源代碼和數據庫&#xff0c;系統主要采用B/S模式開發。 代碼下載 https://download.csdn.net/download/qq_41221322/88232051 論文 https://…

前端技術Vue學習筆記--005

Vue學習筆記 一、非父子通信-event bus 事件總線 作用&#xff1a;非父子組件之間&#xff0c;進行簡易消息傳遞。&#xff08;復雜場景用----Vuex&#xff09; 使用步驟&#xff1a; 創建一個都能訪問的事件總線 &#xff08;空Vue實例&#xff09;-----utils/EventBus.js /…

兩個數組的交集-C語言/Java

描述 給定兩個數組 nums1 和 nums2 &#xff0c;返回 它們的交集 。輸出結果中的每個元素一定是 唯一 的。我們可以 不考慮輸出結果的順序。&#xff08;1 < nums1.length, nums2.length < 1000&#xff0c;0 < nums1[i], nums2[i] < 1000&#xff09; 示例1 輸入…

【golang】通道(channel)的基本原理(一)

通道類型的值本身就是并發安全的&#xff0c;這也是Go語言自帶的、唯一一個可以滿足并發安全性的類型。 聲明一個通道類型變量的時候&#xff0c;我們首先要確定該通道類型的元素類型&#xff0c;決定了我們可以通過這個通道傳遞什么類型的數據。 在初始化通道的時候&#xf…

一鍵批量修改文件夾名稱,中文瞬間變日語,輕松搞定重命名

大家好&#xff01;現在為了更好地適應全球化發展&#xff0c;許多人都有了海外交流、旅行、學習的需求。但是難免遇到一個問題&#xff1a;在電腦中的中文文件夾名稱如何快速翻譯成日語&#xff1f; 首先&#xff0c;第一步&#xff0c;我們需要打開文件批量改名&#xff0c;…

【Unity】編輯器下查找制定文件下的所有特定資源

需求上很簡單&#xff0c;就是在編輯器下&#xff0c;找到某個制定文件下的所有特定資源&#xff08;UnityEngine.Object&#xff09;。Unity 沒有提供專門的 API&#xff0c;我一開始想在網上搜索代碼&#xff0c;發現沒有現成可以直接用的。 功能實現本身并不復雜&#xff0c…

AWS EKS 集群自動擴容 Cluster Autoscaler

文章目錄 一&#xff0c;需求工作需求說明 二&#xff0c;部署精簡命令執行1&#xff0c;要求2&#xff0c;查看EC2 Auto Scaling groups Tag3&#xff0c;創建Serviceaccount需要的Policy&#xff0c;Role4&#xff0c;部署Cluster Autoscaler5&#xff0c;驗證6&#xff0c;常…

zotero在不同系統的安裝(win/linux)

1 window系統安裝 zotero 官網&#xff1a; https://www.zotero.org/ 官方文檔 &#xff1a;https://www.zotero.org/support/ (官方)推薦常用的插件: https://www.zotero.org/support/plugins 入門視頻推薦&#xff1a; Zotero 文獻管理與知識整理最佳實踐 點擊 exe文件自…

【環境配置】Windows 10 安裝 PyTorch 開發環境,以及驗證 YOLOv8

Windows 10 安裝 PyTorch 開發環境&#xff0c;以及驗證 YOLOv8 最近搞了一臺Windows機器&#xff0c;準備在上面安裝深度學習的開發環境&#xff0c;并搭建部署YOLOv8做訓練和測試使用&#xff1b; 環境&#xff1a; OS&#xff1a; Windows 10 顯卡&#xff1a; RTX 3090 安…

Bug日記-webstorm運行yarn 命令報錯

在windows中輸入yarn -v正確輸出&#xff0c;在webstrom終端中運行yarn命令輸出錯誤 問題&#xff1a;可能是由于 WebStorm 配置問題導致的。 解決方案&#xff1a; 檢查 WebStorm 的終端配置&#xff1a;在 WebStorm 中&#xff0c;點擊菜單欄的 “File”&#xff08;文件&am…

DeepSort:基于檢測的目標跟蹤的經典

本文來自公眾號“AI大道理” DeepSORT在SORT的基礎上引入了深度學習的特征表示和更強大的目標關聯方式&#xff0c;有效地減少了身份切換的數量&#xff0c;緩解了重識別問題。 ? 1、DeepSORT簡介 DeepSORT的主要思想是將目標檢測和目標跟蹤兩個任務相結合。 首先使用目標檢…

排序算法分析——什么時候 用 什么排序

排序算法 & 分析 排序算法歷史排序算法分析很快的排序較快的排序中等的排序很慢的排序 分析的結果0.沒有要求1.對速度有要求2.邊排序邊操作3.條件1&條件24.在有序數中操作5.條件1&條件4 了解各種排序&#xff0c;詳見排序專欄 排序算法歷史 縱觀排序算法的歷史&a…

硬件產品經理:從入門到精通(新書發布)

目錄 簡介 新書 框架內容 相關課程 簡介 在完成多款硬件產品從設計到推向市場的過程后。 筆者于2020年開始在產品領域平臺輸出硬件相關的內容。 在這個過程中經常會收到很多讀者的留言&#xff0c;希望能推薦一些硬件相關的書籍或資料。 其實&#xff0c;筆者剛開始做硬…

10. 實現業務功能--退出登錄

目錄 1. 實現 Controller 2. 單體測試 3. 實現前端界面 退出的具體實現邏輯如下&#xff1a; 1. 用戶訪問退出接口 2. 服務器注銷 Session( 在 Controller 中可以直接進行處理 &#xff09; 3. 返回成功或失敗 4. 如果返回成功瀏覽器跳轉到相應頁面 5. 結束 一般來說&#…