網站首頁 編程語言 正文
一、ES客戶端
ES提供多種不同的客戶端:
1、TransportClient
ES提供的傳統客戶端,官方計劃8.0版本刪除此客戶端。
2、RestClient
RestClient是官方推薦使用的,它包括兩種:REST Low Level Client和 REST High Level Client。ES在6.0之后提供REST High Level Client, 兩種客戶端官方更推薦使用 REST High Level Client,不過當前它還處于完善中,有些功能還沒有。
二、搭建工程
2.1.pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<!-- 修改elasticsearch的版本 -->
<properties>
<elasticsearch.version>6.2.3</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2.2.application.yml
spring:
elasticsearch:
rest:
uris:
- http://192.168.113.135:9200
2.3.app
@SpringBootApplication
public class ElasticsearchApp {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchApp.class, args);
}
}
三、索引管理? ??
3.1 api
創建索引庫:
PUT /java06 { ?"settings":{ ? ? ? "number_of_shards" : 2, ? ? "number_of_replicas" : 0 } }
創建映射:
POST /java06/course/_mapping
{
?"_source": {
? ?"excludes":["description"]
},
"properties": {
? ? ?"name": {
? ? ? ? ?"type": "text",
? ? ? ? ?"analyzer":"ik_max_word",
? ? ? ? ?"search_analyzer":"ik_smart"
? ? },
? ? ?"description": {
? ? ? ? ?"type": "text",
? ? ? ? ?"analyzer":"ik_max_word",
? ? ? ? ?"search_analyzer":"ik_smart"
? ? ? },
? ? ? "studymodel": {
? ? ? ? ?"type": "keyword"
? ? ? },
? ? ? "price": {
? ? ? ? ?"type": "float"
? ? ? },
? ? ? "pic":{
? "type":"text",
? "index":false
? }
}
}
3.1.2.Java Client
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {ElasticsearchApp.class})
public class IndexWriterTest {
@Autowired
? ?private RestHighLevelClient restHighLevelClient;
?
? //創建索引庫
? ?@Test
? ?public void testCreateIndex() throws IOException {
? ? ? ?//創建“創建索引請求”對象,并設置索引名稱
? ? ? ?CreateIndexRequest createIndexRequest = new CreateIndexRequest("java06");
? ? ? ?//設置索引參數
? ? ? ?createIndexRequest.settings("{\n" +
? ? ? ? ? ? ? ?" ? ? ? \"number_of_shards\" : 2,\n" +
? ? ? ? ? ? ? ?" ? ? ? \"number_of_replicas\" : 0\n" +
? ? ? ? ? ? ? ?" }", XContentType.JSON);
? ? ? ?createIndexRequest.mapping("course", "{\r\n" +
? ? ? " \"_source\": {\r\n" +
? ? ? " ? \"excludes\":[\"description\"]\r\n" +
? ? ? " }, \r\n" +
? ? ? " \"properties\": {\r\n" +
? ? ? " ? ? ? ? ? \"name\": {\r\n" +
? ? ? " ? ? ? ? ? ? \"type\": \"text\",\r\n" +
? ? ? " ? ? ? ? ? ? \"analyzer\":\"ik_max_word\",\r\n" +
? ? ? " ? ? ? ? ? ? \"search_analyzer\":\"ik_smart\"\r\n" +
? ? ? " ? ? ? ? ? },\r\n" +
? ? ? " ? ? ? ? ? \"description\": {\r\n" +
? ? ? " ? ? ? ? ? ? \"type\": \"text\",\r\n" +
? ? ? " ? ? ? ? ? ? \"analyzer\":\"ik_max_word\",\r\n" +
? ? ? " ? ? ? ? ? ? \"search_analyzer\":\"ik_smart\"\r\n" +
? ? ? " ? ? ? ? ? },\r\n" +
? ? ? " ? ? ? ? ? \"studymodel\": {\r\n" +
? ? ? " ? ? ? ? ? ? \"type\": \"keyword\"\r\n" +
? ? ? " ? ? ? ? ? },\r\n" +
? ? ? " ? ? ? ? ? \"price\": {\r\n" +
? ? ? " ? ? ? ? ? ? \"type\": \"float\"\r\n" +
? ? ? " ? ? ? ? ? },\r\n" +
? ? ? " }\r\n" +
? ? ? "}", XContentType.JSON);
? ? ? ?//創建索引操作客戶端
? ? ? ?IndicesClient indices = restHighLevelClient.indices();
?
? ? ? ?//創建響應對象
? ? ? ?CreateIndexResponse createIndexResponse =
? ? ? ? ? ?indices.create(createIndexRequest);
? ? ? ?//得到響應結果
? ? ? ?boolean acknowledged = createIndexResponse.isAcknowledged();
? ? ? ?System.out.println(acknowledged);
? }
}
3.2.刪除索引庫
3.2.1.api
DELETE /java06
3.2.2.java client
//刪除索引庫
@Test
public void testDeleteIndex() throws IOException {
//創建“刪除索引請求”對象
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("java06");
//創建索引操作客戶端
IndicesClient indices = restHighLevelClient.indices();
//創建響應對象
DeleteIndexResponse deleteIndexResponse =
? ? ? ? ? ?indices.delete(deleteIndexRequest);
//得到響應結果
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
3.3.添加文檔
3.3.1.api
POST /java06/course/1
{
"name":"spring cloud實戰",
"description":"本課程主要從四個章節進行講解: 1.微服務架構入門 2.spring cloud 基礎入門 3.實戰Spring Boot 4.注冊中心eureka。",
"studymodel":"201001",
"price":5.6
}
3.3.2.java client
//添加文檔
@Test
public void testAddDocument() throws IOException {
//創建“索引請求”對象:索引當動詞
IndexRequest indexRequest = new IndexRequest("java06", "course", "1");
indexRequest.source("{\n" +
" \"name\":\"spring cloud實戰\",\n" +
" \"descriptio
? ? ? ? ? ? ? ? ? ? ? ? ? ?n\":\"本課程主要從四個章節進行講解: 1.微服務架構入門 " +
"2.spring cloud 基礎入門 3.實戰Spring Boot 4.注冊中心nacos。\",\n" +
" \"studymodel\":\"201001\",\n" +
" \"price\":5.6\n" +
"}", XContentType.JSON);
IndexResponse indexResponse =
? ? ? ? ? ?restHighLevelClient.index(indexRequest);
System.out.println(indexResponse.toString());
}
3.4.批量添加文檔
支持在一次API調用中,對不同的索引進行操作。支持四種類型的操作:index、create、update、delete。
-
語法:
3.4.1
POST /_bulk
{ action: { metadata }}
{ requestbody }\n
{ action: { metadata }}
{ requestbody }\n
...
.api
POST /_bulk
{"index":{"_index":"java06","_type":"course"}}
{"name":"php實戰","description":"php誰都不服","studymodel":"201001","price":"5.6"}
{"index":{"_index":"java06","_type":"course"}}
{"name":"net實戰","description":"net從入門到放棄","studymodel":"201001","price":"7.6"}
3.4.2.java client
@Test
public void testBulkAddDocument() throws IOException {
? ?BulkRequest bulkRequest = new BulkRequest();
? ?bulkRequest.add(new IndexRequest("java06", "course").source("{...}",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?XContentType.JSON));
? ?bulkRequest.add(new IndexRequest("java06", "course").source("{...}",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?XContentType.JSON));
? ?BulkResponse bulkResponse =
? ? ? ? ? ? ? ? ? restHighLevelClient.bulk(bulkRequest);
? ?System.out.println(bulkResponse.hasFailures());
}
3.5.修改文檔
3.5.1.api
PUT /java06/course/1
{
"price":66.6
}
3.5.2.java client
//更新文檔
@Test
public void testUpdateDocument() throws IOException {
? ?UpdateRequest updateRequest = new UpdateRequest("java06", "course", "1");
? ?updateRequest.doc("{\n" +
? ? ? ? ? ?" \"price\":7.6\n" +
? ? ? ? ? ?"}", XContentType.JSON);
? ?UpdateResponse updateResponse =
? ? ? ? ? ? ? ? ? restHighLevelClient.update(updateRequest);
? ?System.out.println(updateResponse.getResult());
}
3.6.刪除文檔
3.6.1.api
DELETE /java06/coures/1
3.6.2.java client
?//根據id刪除文檔
? ?@Test
? ?public void testDelDocument() throws IOException {
? ? ? ?//刪除請求對象
? ? ? ?DeleteRequest deleteRequest = new DeleteRequest("java06","course","1");
? ? ? ?//響應對象
? ? ? ?DeleteResponse deleteResponse =
? ? ? ? ? ?restHighLevelClient.delete(deleteRequest);
? ? ? ?System.out.println(deleteResponse.getResult());
? }
四.文檔搜索
原文鏈接:https://blog.csdn.net/RongYu__/article/details/126555441
相關推薦
- 2022-11-22 CentOS?7.9?安裝?docker20.10.12的過程解析_docker
- 2022-12-05 一文深入了解Python中的繼承知識點_python
- 2022-07-26 在Pycharm set ops_config=local之后,直接echo %ops_config
- 2022-12-15 C++?Boost?Lockfree超詳細講解使用方法_C 語言
- 2022-06-14 web項目中golang性能監控解析_Golang
- 2022-09-20 linux?shell字符串截取的詳細總結(實用!)_linux shell
- 2022-02-02 es 同步索引報錯:ElasticSearch ClusterBlockException[bloc
- 2022-05-16 .Net?MVC將Controller數據傳遞到View_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支