Build your own search engine Part 4

I. Introduction

Part 3 of Building Your Own Search Engine  introduces the addition, deletion, modification and query of ES through HTTP RESTful. This is generally used when operating and maintaining ES manually. It is better to use Java API to operate ES in the program code, which will be easier to maintain, but the ES API is actually very messy. Many, this article introduces four APIs and their simple use.

Note: It is somewhat similar to Kong Yiji’s four ways of writing the word fennel.

2. TransportClient

TransportClient is no longer recommended for use in version 7. , spring-data-elasticsearch is also based on this Client.

3. RestClient

Using HTTP REST style interaction, the client sends a request to any node of ES, and the node will route the request to other nodes through the transport interface. After completing the data processing, it is summarized and returned to the client. The client load is low, and it has two sets of APIs.

1. Low-level REST client

The low-level REST client uses the Apache Http Async client internally to send http requests.

After adding dependencies, the httpasyncclient, httpcore-nio, httpclient, and httpcore packages will be automatically introduced.

Initialize RestClient configuration

Request and response , rightBuild your own search engine Part 3   to query the index data created

Note: This kind of query syntax is difficult to write correctly. There is a little trick. You can use the ElasticSearch-Head interface to operate and then view the PayLoad requested by the browser Network and copy it. Its additions, deletions and modifications are the same as using RestFul directly. The operation syntax is the same, except that RestClient wraps the processing of Http request response.

Asynchronous request  

2. Advanced REST client

The lower version of RestClient is difficult to use, so the Java High Level REST Client was launched after version 6.0, which provides a more convenient way to construct request parameters instead of directly writing a bunch of JSON strings.

Note: elasticsearch-rest-client and elasticsearch need to be introduced manually, otherwise there will be version conflicts.

Initialization , RestHighLevelClient relies on low-level clients to build.

Simple query example

In addition, the writing methods of JavaRestClient and Spring Data EleasticSearch will be introduced in the next article.

Guess you like

Origin blog.csdn.net/2301_76787421/article/details/133411965