SpringBoot整合Elasticsearch

项目结构

SpringBoot整合Elasticsearch_第1张图片
image.png

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    
    com.elasticsearch
    es
    0.0.1-SNAPSHOT
    es
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.properties

#spring.profiles.active=dev

# elasticsearch 配置
es.ip=192.168.41.139
es.port = 9200
es.scheme = http

ElasticConfig.java

package com.elasticsearch.es.config;


import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticConfig {

    @Value("${es.ip}")
    private String esIp;

    @Value("${es.port}")
    private int esPort;

    @Value("${es.scheme}")
    private String esScheme;

    @Bean
    public RestHighLevelClient getRestHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(esIp, esPort, esScheme))
        );
        return client;
    }
}

EsApplicationTests.java

package com.elasticsearch.es;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.TermVectorsRequest;
import org.elasticsearch.client.core.TermVectorsResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class EsApplicationTests {

    @Qualifier("getRestHighLevelClient")
    @Autowired
    private RestHighLevelClient esClient;

    /*
     * 不建议使用 ES 对索引和mapping进行CRUD操作
     *  一般只对文档进行CRUD操作
     * */
    @Test
    void createIndex() {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("test");
        try {
            CreateIndexResponse createIndexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void createDocument(){
        Map jsonMap = new HashMap<>();
        jsonMap.put("name", "aaa");
        jsonMap.put("age", 10);
        jsonMap.put("sex", true);
        jsonMap.put("money", 120.2);
        jsonMap.put("createDate", "2020-02-02");
//      IndexRequest indexRequest = new IndexRequest("index_test").id("111").source(jsonMap);
        IndexRequest indexRequest = new IndexRequest("index_test").source(jsonMap);
        try {
            IndexResponse indexResponse = esClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void getDocument(){
        GetRequest getRequest = new GetRequest("index_test", "111");
        try {
            GetResponse getResponse = esClient.get(getRequest, RequestOptions.DEFAULT);
            Map map = getResponse.getSource();
            for(String key : map.keySet()){
                System.out.println(key+":"+map.get(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void updateDocument(){
        Map jsonMap = new HashMap<>();
        jsonMap.put("name", "bbb");
        jsonMap.put("age", 10);
        jsonMap.put("sex", true);
        jsonMap.put("money", 120.2);
        jsonMap.put("createDate", "2020-02-02");
        UpdateRequest updateRequest = new UpdateRequest("index_test", "111").doc(jsonMap);
        try {
            UpdateResponse updateResponse = esClient.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    void deleteDocument(){
        DeleteRequest deleteRequest = new DeleteRequest("index_test", "111");
        try {
            DeleteResponse deleteResponse = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(SpringBoot整合Elasticsearch)