elasticsearch6-RestClient operation documentation

Please add image description
Business card:

Blogger: Alcoholic?.
Personal profile: Indulge in wine, and use the energy of wine to fight for a future.
This article is inspirational: When three people are together, there must be one who is my teacher.

Please add image description
This project is based on Bilibili’sDark Horse Programmer Java’s “SpringCloud Microservice Technology Stack”, SpringCloud + RabbitMQ + Docker + Redis + Search + Distribution

[SpringCloud + RabbitMQ + Docker + Redis + Search + Distributed, System Detailed Springcloud Microservice Technology Stack Course | Dark Horse Programmer Java Microservices] Click to watch

Directory

  • 5. RestClient operation documentation
    • 1. Add new documents
    • 2. Query documents
    • 3. Delete documents
    • 4. Modify the document
    • 5. Import documents in batches

5. RestClient operation documentation

RestClient in Elasticsearch is a non-Java API for interacting with an Elasticsearch cluster. RestClient provides a simple, intuitive way to manipulate and manage documents in Elasticsearch.
RestClient uses HTTP protocol to communicate with the Elasticsearch cluster. It supports multiple request types, such as GET, POST, PUT, DELETE, etc., and can send requests to specified indexes, types, and IDs. RestClient is one of the officially recommended APIs of Elasticsearch. It has the following advantages:

  • Simple and easy to use RestClient API is designed to be very simple and easy to learn and use. Developers can easily create, query, update, and delete documents in Elasticsearch with simple HTTP requests.

  • Comprehensive functional support RestClient supports most functions in Elasticsearch, including full-text search, structured search, aggregate query, etc. It also provides the function of monitoring and managing Elasticsearch clusters, which can help developers understand the status and operation of the cluster and discover and solve problems in a timely manner.

  • Cross-platform support RestClient is an API based on the HTTP protocol, which can be used on any platform that supports the HTTP protocol. Whether it’s Java, Python, Ruby or other programming languages, you can use RestClient
    API to interact with Elasticsearch cluster.

  • Flexible customization RestClient API is a non-intrusive API in Elasticsearch. It can easily customize and extend Elasticsearch without modifying the original code.

The main functions of the RestClient operation document are as follows:

  • CRUD operations on documents RestClient can be used to create, read, update, and delete documents in Elasticsearch. These operations can be achieved through simple HTTP requests, and developers only need to provide a JSON representation of the document and the corresponding request method.

  • Structured search RestClient provides a structured search function that can help developers perform search and filter operations based on specified fields. These operations can be implemented through simple query strings or JSON formatted query requests.

  • Full-text search RestClient provides a full-text search function, which can help developers perform search and matching operations based on text content. These operations can be implemented through simple query strings or JSON formatted query requests.

  • Aggregation query RestClient provides the function of aggregation query, which can help developers group, summarize and make statistics on large amounts of data. These operations can be implemented through simple query strings or JSON formatted query requests.

  • Data analysis and monitoring RestClient provides data analysis and monitoring functions, which can help developers understand the status and operation of the Elasticsearch cluster and discover and solve problems in a timely manner. These operations can be achieved through the monitoring API.

Case: Using JavaRestClient to implement CRUD of documents
Initialization:

@SpringBootTest
public class HotelDocumentTest {<!-- -->
    @Autowired
    private IHotelService hotelService;

    @Autowired
    private RestHighLevelClient client;

    @BeforeEach
    void setup() {<!-- -->
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.179.128:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {<!-- -->
        this.client.close();
    }
}

1. Add new document

@Test
void testIndexDocument() throws IOException {<!-- -->
    Hotel hotel = hotelService.getById(36934L);
    HotelDoc hotelDoc = new HotelDoc(hotel);
    IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
    request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
    client.index(request, RequestOptions.DEFAULT);
}

2. Query documents

@Test
void testGetDocumentById() throws IOException {<!-- -->
    GetRequest request = new GetRequest("hotel", "36934");
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    String json = response.getSourceAsString();
    HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
    System.out.println(hotelDoc);
}

3. Delete document

@Test
void testDeleteDocument() throws IOException {<!-- -->
    DeleteRequest request = new DeleteRequest("hotel", "36934");
    client.delete(request, RequestOptions.DEFAULT);
}

4. Modify document

  1. Full modification = insert data
  2. Incremental modification Modify the field if it exists. If it does not exist, add the field automatically.
@Test
void testUpdateDocument() throws IOException {<!-- -->
     UpdateRequest request = new UpdateRequest("hotel", "36934");
     request.doc(
             "price", "954",
             "startName", "Four Diamonds"
     );
     client.update(request, RequestOptions.DEFAULT);
 }

5. Batch import documents

@Test
void testBulk() throws IOException {<!-- -->
List<Hotel> list = hotelService.list();
\t
BulkRequest request = new BulkRequest();
for (Hotel hotel : list) {<!-- -->
HotelDoc hotelDoc = new HotelDoc(hotel);
request.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
client.bulk(request, RequestOptions.DEFAULT);
}

Summary:

Basic steps for document operations:
Initialize RestHighLevelClient
Create XxxRequest. XXX is Index, Get, Update, Delete, Bulk
Prepare parameters (required for Index, Update, and Bulk)
send request. Call RestHighLevelClient#.xxx() method, xxx is index, get, update, delete, bulk
Parse results (required when Get)