[Search series] ES basic project construction

I haven’t written ES-related blog posts before, and now I’m starting to make up lessons. It is expected that 5-6 blog posts will show the use of es to your friends; this article will be the first blog post of es combined with springboot, and the basic project environment will be built.

<!-- more -->

I. Project Construction

1. Project dependencies

This project is developed with the help of SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA

Open a web service for testing

<dependencies>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
    </dependency>
</dependencies>

2. Configuration Information

Configuration file application.yml, pay attention to the following configuration information, the following is the way to parse the configuration by ourselves

elasticsearch:
  host: localhost
  port: 9200
  user: elastic
  pwd: test123
  connTimeout: 3000
  socketTimeout: 5000
  connectionRequestTimeout: 500

Description

The above configuration describes a basic es document operation posture, which is more flexible to use than the better spring-boot-starter-data-elasticsearch

II. SpringBoot combined with ES

1. RestHighLevelClient initialization

Next, we operate es based on RestHighLevelClient . The first step is to initialize this instance.

@Getter
@Configuration
public class ElasticsearchConfiguration {

    @Value("${elasticsearch.host}")
    private String host;

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

    @Value("${elasticsearch.connTimeout}")
    private int connTimeout;

    @Value("${elasticsearch.socketTimeout}")
    private int socketTimeout;

    @Value("${elasticsearch.connectionRequestTimeout}")
    private int connectionRequestTimeout;

    @Value("${elasticsearch.user}")
    private String user;

    @Value("${elasticsearch.pwd}")
    private String pwd;

    @Bean(destroyMethod = "close", name = "client")
    public RestHighLevelClient initRestClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                        .setConnectTimeout(connTimeout)
                        .setSocketTimeout(socketTimeout)
                        .setConnectionRequestTimeout(connectionRequestTimeout));
        return new RestHighLevelClient(builder);
    }
}

Note that in the above implementation, the username + password is not used. When the username and password are set in es, authentication is performed based on the Basic Auth method in the request header for each request; it will be introduced later.

2. Basic use

We built an es on this machine for simulation testing. After the above configuration, we can directly interact with es

es installation can refer to:

The following is a simple posture to use

@Service
public class EsTest {
    @Autowired
    private RestHighLevelClient client;

    private static String auth;

    public EsTest(ElasticsearchConfiguration elasticsearchConfiguration) {
        auth = Base64Utils.encodeToString((elasticsearchConfiguration.getUser() + ":" + elasticsearchConfiguration.getPwd()).getBytes());
        auth = "Basic " + auth;
    }

    public void testGet() throws Exception {
        // 文档查询
        GetRequest getRequest = new GetRequest("first-index", "_doc", "gvarh3gBF9fSFsHNuO49");
        RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
        requestOptions.addHeader("Authorization", auth);
        GetResponse getResponse = client.get(getRequest, requestOptions.build());
        if (getResponse.isExists()) {
            String sourceAsString = getResponse.getSourceAsString();
            System.out.println(sourceAsString);
        } else {
            System.out.println("no string!");
        }
    }
}

Pay attention to the above implementation, there are the following important knowledge points

Authentication

The Basic Auth method is used for identity verification, which is simply to add a

  • key = Authorization
  • value = "Basic " + base64(user + ":" + pwd)

access pose

The above is an example of querying documents according to id , which can be easily understood as three steps

  • Created: XxRequest
  • Add request header: RequestOptions.Builder.addHeader
  • Execute: client.get(xxRequest, RequestOptions)

III. Source code and related knowledge points that cannot be missed

0. Project

1. WeChat public account: Yihuihui Blog

It is not as good as a letter. The above content is purely from the family. Due to limited personal ability, it is inevitable that there will be omissions and mistakes. If you find bugs or have better suggestions, you are welcome to criticize and correct them.

The following is a gray personal blog, recording all blog posts in study and work, welcome everyone to visit

一灰灰blog


小灰灰Blog
251 声望46 粉丝