This is the 18th day of my participation in the August Challenge

Author: Tom Brother wechat official account: Micro technology

Introduction to ElasticSearch

Elasticsearch is a Lucene based search engine developed in Java. It provides a distributed multi – user – capable full – text search engine based on RESTful Web interface. Elasticsearch can store, search, and analyze large amounts of data quickly and efficiently, and is especially useful when dealing with semi-structured data (i.e., natural language).

Elasticsearch can be integrated in 4 ways:

  • REST Client

  • Jest

  • Spring Data

  • Spring Data Elasticsearch Repositories

This article introduces how to use Spring Data Elasticsearch Repositories. This method is highly integrated with Spring Boot, which is convenient for daily development and can be used out of the box only after simple configuration.

2. Run Elasticsearch

For testing purposes, we quickly deployed a single instance of Elasticsearch using a Docker image. The container started with port 9200 and port 9300 bound to the host

Pull mirror:

Docker pull elasticsearch: 7.4.2Copy the code

View mirror:

docker images

Copy the code

Create a host mount directory:

mkdir -p /mydata/elasticsearch/config/ mkdir -p /mydata/elasticsearch/data/ echo "http.host: 0.0.0.0 "> > / mydata/elasticsearch/config/elasticsearch ymlCopy the code

Run container:

docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \ -e ES_JAVA_OPS="-Xms256m -Xmx256m" \ -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \ -v / mydata/elasticsearch/plugins: / usr/share/elasticsearch/plugins \ - d elasticsearch: 7.4.2Copy the code

Parameter Description:

-p 9200:9200 Maps port 9200 of a container to port 9200 of a host.

Name ElasticSearch Specifies the name of the currently started container elasticSearch

– v/mydata/elasticsearch/data: / usr/share/elasticsearch/data folder is mounted on the data to the host; – v/mydata/elasticsearch/config/elasticsearch yml: / usr/share/elasticsearch/config/elasticsearch. Yml will mount to the host configuration file;

-d Running in back-end mode (daemon)

-es_JAVA_OPS =” -xMS256m -XMx256m “Specifies a small memory size during the test

View container processes:

docker ps -a

Copy the code

3. Project integration

Following Spring Boot convention, we don’t have to provide any beans in the context to enable Elasticsearch support. We just need to add the following dependencies to pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Copy the code

Because the version number specified by spring-boot-starter-parent is 2.2.1.RELEASE, the two starter components introduced above are forced to have the same version, facilitating unified management. The spring-data-ElasticSearch introduced at the bottom is 3.2.1.RELEASE

Select * from localhost to connect to Elasticsearch; select * from localhost to connect to Elasticsearch;

Spring: Application: name: spring-boot-Bulking-ElasticSearch ElasticSearch: rest: uris: 127.0.0.1:9200 Read-timeout: 5sCopy the code

Spring Boot operates on ES data in three ways:

  • Implement ElasticsearchRepository interface

  • The introduction of ElasticsearchRestTemplate

  • The introduction of ElasticsearchOperations

Elasticsearch (Repositories) : Code for Elasticsearch by Spring Data Elasticsearch Repositories

@Document(indexName = "order", type = "biz1", shards = 2)
public class OrderModel {

    @Id
    private Long orderId;
    private Double amount;
    private Long buyerUid;
    private String shippingAddress;
}

Copy the code

Common notes:

Document: Represents the domain object mapped to the Elasticsearch Document

@id: indicates the Id of a document, which can be considered as the concept of a table row in mysql

Filed: Type of fields in a document, whether to set up an inverted index, and whether to store them

OrderModel represents the index model of the order, and an OrderModel object represents an ES index record. If you use a relational database as a reference, Index is a table and Document is a record

Then you need to define a business interface, OrderRepository, and inherit the extension interface ElasticsearchRepository

public interface OrderRepository extends ElasticsearchRepository<OrderModel, Long> {
}

Copy the code

ElasticsearchRepository is an extension interface reserved by Spring Boot Elasticsearch framework.

The ElasticsearchRepository interface provides common ES operation methods, such as adding, modifying, deleting, querying various dimension conditions and paging, as follows:

The save() method creates the index, and if the index document already exists, the subsequent save method overwrites the previous data. This means that both additions and modifications can be implemented through the save method.

Finally, verify method functionality by writing unit test classes

@test public void test1() {OrderModel OrderModel = orderModel.builder ().orderId(1L).amount(25.5).buyeruid (13201L) .shippingAddress(" Shanghai ").build(); orderModel = orderRepository.save(orderModel); System.out.println(orderModel); } @Test public void test2() { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder() .withQuery(boolQueryBuilder); List<OrderModel> orderDocumentList = orderRepository.search(searchQueryBuilder.build()).getContent(); System.out.println(JSON.toJSONString(orderDocumentList)); }Copy the code

Use this OrderRepository to manipulate OrderModel data in ES. We did not create the index for OrderModel manually, which is generated by elasticSearch by default.

Kibana Visual console

Install Kibana, relatively simple, here is not too tired to describe. Download the Kibana installation package and unzip it

Run the startup script./kibana in the # bin directoryCopy the code

Browser visit: http://localhost:5601/app/kibana#/dev\_tools/console? \_g=()

You can see the index record you created when you just performed the unit test.

5, the project source address

Github.com/aalansehaiy…

Module: spring – the boot – bulking – elasticsearch

Author introduction: Tom brother, computer graduate student, the school recruited ali, P7 technical expert, has a patent, CSDN blog expert. Responsible for e-commerce transactions, community fresh, flow marketing, Internet finance and other businesses, many years of first-line team management experience