SpringBoot接入ElasticSearch7.10.2详细教程

一、搭建项目

目前时间 2021年1月27日 我们将使用当前最新版本ElasticSearch,即7.10.2版本配合SpringBoot搭建,由于新版本只有ElasticSearch官方提供了开发工具,所以我们采用官方工具。

SpringBoot接入ElasticSearch7.10.2详细教程_第1张图片

我们因为不采用Spring为我们提供的接入方式,下一步填好信息后选择不需要选NoSQL里面任何选项,选一个Web直接一路Next等待完成

SpringBoot接入ElasticSearch7.10.2详细教程_第2张图片

 

二、导入Maven



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.2
         
    
    com.qiruipeng
    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
            7.10.2
        
        
            org.elasticsearch
            elasticsearch
            7.10.2
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            junit
            junit
            test
        

        
            org.projectlombok
            lombok
        
        
            com.alibaba
            fastjson
            1.2.75
        
    

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


 

三、建立Config

建立一个类,我们命名为 ElasticSearchConfig 把它放在了Config文件夹作为我们的配置文件

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

/**
 * @author ruipeng.qi
 **/
@Configuration
public class ElasticSearchConfig {

	public  static final RequestOptions COMMON_OPTIONS;
	static {
		RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
		COMMON_OPTIONS = builder.build();
	}

	@Bean
	public RestHighLevelClient client() {
		return new RestHighLevelClient(
				RestClient.builder(
						new HttpHost("127.0.0.1", 9200)));
	}
}

 

四、Test测试新增

我们尝试新增一个User对象。我们采用内部类形式定义对象,之后新建一个实例,让他的名字叫 zhangsan;性别是 男;年龄18

注意,下面代码中 @Data 注解作用是自动添加 GET 和 SET 方法,和项目本身没有太大关系

运行后尝试查询是否添加成功。

import com.alibaba.fastjson.JSON;
import com.qiruipeng.es.config.ElasticSearchConfig;
import lombok.Data;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
class EsApplicationTests {

	@Autowired
	private RestHighLevelClient client;

	/**
	 * 测试存储数据到es
	 * 更新也可以
	 */
	@Test
	void indexData() throws IOException {
		IndexRequest indexRequest = new IndexRequest("users");
		indexRequest.id("1");
		User user = new User("zhangsan", "男", 18);
		String jsonString = JSON.toJSONString(user);
		indexRequest.source(jsonString, XContentType.JSON);

		//执行操作
		IndexResponse index = client.index(indexRequest, ElasticSearchConfig.COMMON_OPTIONS);

		//提取有用的操作数据
		System.out.println(index);
	}

	@Data
	static class User{
		private String userName;
		private String gender;
		private Integer age;

		public User(String userName, String gender, Integer age) {
			this.userName = userName;
			this.gender = gender;
			this.age = age;
		}
	}

}

 

五、检测是否成功

我们通过Kinaba查询是否添加成功:

GET users/_search

返回结果如下,可见添加已经成功

{
  "took" : 50,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "age" : 18,
          "gender" : "男",
          "userName" : "zhangsan"
        }
      }
    ]
  }
}

 

你可能感兴趣的:(ElasticSearch)