1. Introduction

Elasticsearch is one of the most popular search engines for Elasticsearch in the world. Elasticsearch is one of the most popular search engines for Elasticsearch in the world

2. Elasticsearch is introduced

Elasticsearch is a distributed search engine that stores, analyzes, and searches data.

3. Elasticsearch installation

3.1 Installing Elasticsearch in Docker Mode

3.1.1 Using Docker to Search for Images

➜  ~ docker search elasticsearch

Copy the code

3.1.2 Pulling a Mirror from a Remote Location

➜ ~ Docker Pull ElasticSearch :7.4.0

Copy the code

3.1.3 Starting a Mirror

➜ ~ docker run -d --name myes -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" ElasticSearch :7.4.0

Copy the code

3.1.4 access Elasticsearch

http://localhost:9200/

{

"name" : "0222e17f062d".

"cluster_name" : "docker-cluster".

"cluster_uuid" : "OtL-k7FpR4ylPVsiKcZIkA".

"version" : {

"number" : "7.4.0".

"build_flavor" : "default".

"build_type" : "docker".

"build_hash" : "22e1767283e61a198cb4db791ea66e3f11ab9910".

"build_date" : "The 2019-09-27 T08:36:48. 569419 z".

"build_snapshot" : false.

"lucene_version" : "8.2.0".

"minimum_wire_compatibility_version" : "6.8.0".

"minimum_index_compatibility_version" : "6.0.0 - beta1"

},

"tagline" : "You Know, for Search"

}

Copy the code

When you see the data returned above, congratulations on the successful es installation.

3.2 Elasticsearch Is Properly Installed

3.2.1 Downloading the installation package

Linux:

➜ ~ curl - L - O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.1-linux-x86_64.tar.gz

Copy the code

MacOS:

➜ ~ wget HTTP: / / https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.1-darwin-x86_64.tar.gz

Copy the code

3.2.2 Decompressing the installation package

Linux:

➜ ~ tar-xvf ElasticSearch-7.4.1-linux-x86_64.tar.gz

Copy the code

macOS:

➜ ~ tar-xvf ElasticSearch-7.4.1 - Darwin x86_64.tar.gz

Copy the code

3.2.3 run

Linux and macOS:

➜ ~ CD elasticsearch - 7.4.1 / bin

➜ ~. / elasticsearch

Copy the code

3.3 access Elasticsearch

http://localhost:9200/

4. Add, delete, change, and check battles for Elasticsearch

Elasticsearch (SpringBoot) Elasticsearch (Elasticsearch

4.1 Creating a Project

Create boot-example-elasticSearch module

4.2 Adding a Dependency

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>



<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</dependency>



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

</dependency>



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-elasticsearch</artifactId>

</dependency>

</dependencies>

Copy the code

4.3 Configuring the Elasticsearch Address

spring:

elasticsearch:

rest:

uris: http://localhost:9200 # specify es address

Copy the code

4.4 Creating a Document Entity Class

@Data

@Builder

@NoArgsConstructor

@AllArgsConstructor

@ToString

@Document(indexName = "goods", type = "_doc")

public class Goods implements Serializable {



@Id

private Long id;



@Field

private String name;



@Field

private String title;



@Field

private BigDecimal price;



@Field

private String publishDate;

}

Copy the code

You need to use the @document annotation to specify the index and Document type so that you don’t need to specify the index and type when manipulating Document data. Use the @ID annotation to indicate the Id of the document, and the @field annotation to indicate other attributes of the document

4.5 injection ElasticsearchRestTemplate

As usual with other middleware such as Redis and rabbitMQ, you need to inject the appropriate xxxxTemplate

@Autowired

private ElasticsearchRestTemplate elasticsearchRestTemplate;

Copy the code

4.6 Adding Document Data

@Test

public void insert(a) {

Goods goods = Goods.builder()

.id(10006L)

.name("Test goods")

.title("I am a test goods, shot not delivery, please be careful!")

.price(new BigDecimal(9999999))

.publishDate("2019-11-06")

.build();

/ * *

* index is used to add or modify the entire document

* The document does not exist, perform the new operation

* The document exists, perform the modification operation

* Here you only need to set the index object, the underlying code from the index object@DocumentGet the index, type, and document ID from the annotation

* /


elasticsearchRestTemplate.index(new IndexQueryBuilder().withObject(goods).build());

log.info("index document finish");

}

Copy the code

4.7 Adding Document Data in Batches

@Test

public void batchInsert(a) {

List<Goods> goodsList = buildGoodsList();

List<IndexQuery> indexQueryList = new ArrayList<>();

for (int i = 0, len = goodsList.size(); i < len; i++) {

indexQueryList.add(new IndexQueryBuilder()

.withObject(goodsList.get(i)).build());

}

// Use the bulk method to bulk index documents

elasticsearchRestTemplate.bulkIndex(indexQueryList);

log.info("batch index document finish");

}

Copy the code

4.8 Modifying Document Data

@Test

public void update(a) {

Goods goods = Goods.builder()

.id(10001L)

.name("AppleiPhone 11 update")

.title(Apple iPhone 11 (A2223) 64GB Black China Unicom 4G Mobile Phone Dual Sim Card Dual Waiting update)

.price(new BigDecimal(6666))

.publishDate("2019-12-12")

.build();

IndexQuery indexQuery = new IndexQueryBuilder()

.withObject(goods)

.build();

/ * *

* Use the index method to update the entire document

* If a field is null and updated to ES, it will not exist in the ES document

* {

* "_class" : "com.boot.example.Goods",

* "id" : 10006,

* "name" : "AppleiPhone 11 update",

* "Title" : "Apple iPhone 11 (A2223) 64GB Black Mobile Unicom 4G Mobile Dual sim dual waiting update"

*}

* /


elasticsearchRestTemplate.index(indexQuery);

log.info("update document");

}

Copy the code

4.9 Modifying Some Data in the Document

@Test

public void partialUpdate(a) {

// Build the fields to be updated

Map<String, Object> map = new HashMap<>();

map.put("price".new BigDecimal(6666));

map.put("publishDate"."2019-09-08");

UpdateQuery updateQuery = new UpdateQueryBuilder()

.withId("10001")

.withClass(Goods.class)

.withUpdateRequest(new UpdateRequest().doc(map))

.build();

// Update part of the document

elasticsearchRestTemplate.update(updateQuery);

log.info("partial update document");

}

Copy the code

4.10 Querying Data of a Single Document

@Test

public void getById(a) {

GetQuery getQuery = new GetQuery();

getQuery.setId("10001");

Goods goods = elasticsearchRestTemplate.queryForObject(getQuery, Goods.class);

log.info("Get document by ID result: {}", goods);

}

Copy the code

4.11 Querying Document List Data

@Test

public void list(a) {

/ * *

* Use match to search

* GET /goods/_search

* {

* "query": {

* "match": {

* "title": "apple"

*}

*}

*}

* /


NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()

.withQuery(new MatchQueryBuilder("title"."apple"))

.withSort(new FieldSortBuilder("publishDate").order(SortOrder.DESC))

.build();

List<Goods> goodsList = elasticsearchRestTemplate.queryForList(nativeSearchQuery, Goods.class);

log.info("List document size: {}, result: {}", goodsList.size(), goodsList);

}

Copy the code

4.12 Querying Document List Data using Multiple Criteria

@Test

public void listByMultiCondition(a) {

NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()

.withQuery(new BoolQueryBuilder()

.must(new MatchQueryBuilder("title"."apple"))

.must(new RangeQueryBuilder("price").from(BigDecimal.ZERO).to(new BigDecimal(12800)))

)

.build();

List<Goods> goodsList = elasticsearchRestTemplate.queryForList(nativeSearchQuery, Goods.class);

log.info("List document size: {}, result: {}", goodsList.size(), goodsList);

}

Copy the code

4.13 Querying Document Data in Pages

@Test

public void page(a) {

QueryBuilder queryBuilder = new MatchQueryBuilder("title"."apple");

NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()

.withQuery(queryBuilder)

.withPageable(PageRequest.of(0.10))

.build();

/ * *

* To view detailed request response logs, set the following parameters in the configuration file:

* logging:

* level:

* tracer: trace

*

[/O dispatcher 1] Dispatcher: [/O dispatcher 2] dispatcher: curl -iX POST 'http://localhost:9200/goods/_doc/_search? rest_total_hits_as_int=true&typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_ throttled=true&search_type=dfs_query_then_fetch&batched_reduce_size=512' -d '{"from":0,"size":10,"query":{"match":{"title":{"query":"apple","operator":"OR","prefix_length":0,"max_expansions":50,"f uzzy_transpositions":true,"lenient":false,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"boost":1 .0}}},"version":true}'

* # HTTP/1.1 200 OK

* # Warning: Elasticsearch 7.4.0-299-22 e1767283e61a198cb4db791ea66e3f11ab9910 "[types removal] Specifying the types in the search requests is deprecated."

* # content-type: application/json; charset=UTF-8

* # content-length: 730

* #

* # {"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0. 76022595, "hits" : [{" _index ":" goods ", "_type" : "_doc", "_id" : "10001", "_version" : 2, "_score" : 0.76022595, "_source" : {" _class ":" co M.boot.example.Goods"," ID ":10001,"name":"AppleiPhone 11"," Title ":"AppleiPhone 11 (A2223) 64GB Black Mobile Unicom 4G mobile phone Double card double stay ", "price" : 6666, "publishDate" : "2019-09-08"}}, {" _index ":" goods ", "_type" : "_doc", "_id" : "10002", "_version" : 1, "_score" : 0.7 3438257,"_source":{"_class":"com.boot.example.Goods","id":10002,"name":"AppleMNYH2CH/A","title":"Apple MacBook 12 | Core MNYH2CH/A","price":12800.0,"publishDate":"2018-09-11"}}]}

* /


Page<Goods> page = elasticsearchRestTemplate.queryForPage(nativeSearchQuery, Goods.class);

List<Goods> goodsList = page.getContent();

log.info("Document size: {}, result: {}", goodsList.size(), goodsList);

}

Copy the code

4.14 Deleting a Document

@Test

public void delete(a) {

// Delete the document

elasticsearchRestTemplate.delete(Goods.class, "10001");

}

Copy the code

5.Elasticsearch aggregation combat

5.1 Collecting Documents

@Test

public void count(a) {

SearchQuery searchQuery = new NativeSearchQueryBuilder().build();

Long count = elasticsearchRestTemplate.count(searchQuery, Goods.class);

log.info("Goods count: {}", count);

}

Copy the code

5.2 Average Value

@Test

public void avgPrice(a) {

SearchQuery searchQuery = new NativeSearchQueryBuilder()

.addAggregation(AggregationBuilders.avg("avg_price").field("price"))

.withIndices("goods")

.build();

double avgPrice = elasticsearchRestTemplate.query(searchQuery, response -> {

Avg avg = response.getAggregations().get("avg_price");

return avg.getValue();

});

log.info("Avg_price: {}", avgPrice);

}

Copy the code

5.3 Finding the Maximum value

@Test

public void maxPrice(a) {

SearchQuery searchQuery = new NativeSearchQueryBuilder()

.addAggregation(AggregationBuilders.max("max_price").field("price"))

.withIndices("goods")

.build();

double maxPrice = elasticsearchRestTemplate.query(searchQuery, response -> {

Max max = response.getAggregations().get("max_price");

return max.getValue();

});

log.info("Max_price: {}", maxPrice);

}

Copy the code

6. ElasticsearchRestTemplate API

The serial number function The method name The query object
1 new index IndexQuery
2 A batch of new bulkIndex IndexQuery
3 Modify the index IndexQuery
Part of the modified update UpdateQuery
5 The query object queryForObject GetQuery
6 Query list queryForList NativeSearchQuery
7 Paging query queryForPage NativeSearchQuery
8 delete delete DeleteQuery
9 statistical count NativeSearchQuery
10 The average uery NativeSearchQuery
11 The maximum query NativeSearchQuery

Elasticsearch with SpringBoot

7.1 ElasticsearchDataConfiguration


Statement of our most important ElasticsearchRestTemplate ElasticsearchRestTemplate need RestHighLevelClient

7.2 RestClientConfigurations


Declare RestHighLevelClient, RestHighLevelClient requires RestClientBuilder


Declare the RestClientBuilder. The RestClientBuilder is built by RestClient. The hosts used by the build are obtained from the URIS property of RestClientProperties, which is specified in the configuration file

spring:

elasticsearch:

rest:

uris: http://localhost:9200 # specify es address

Copy the code

Through this article you not only learn how to integrate SpringBoot and Elasticsearch, also can be used to ElasticsearchRestTemplate document to add, delete, change, check operation, finally you also know the Elasticsearch principle of automatic configuration, I hope you found this knowledge helpful.

8. Project address

boot-example