日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Spring Boot整合ElasticSearch

作者:RongYu__ 更新時間: 2022-08-28 編程語言

一、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

欄目分類
最近更新