【笔记】SpringBoot项目整合Elasticsearch

前言

SpringBoot项目整合Elasticsearch

Elasticsearch学习笔记传送门

引入依赖

  • 在引入Elasticsearch客户端的依赖时要与Elasticsearch服务端的版本保持一致
  • 因为SpringBoot中会定义Elasticsearch客户端的版本,所以为了与Elasticsearch服务端的版本保持一致,需要手动修改版本
pom.xml
1
2
3
4
5
6
7
8
9
10
<properties>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

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

创建RestHighLevelClient对象

  • 通过HttpHost.create("127.0.0.1:9200")指定Elasticsearch服务端的地址。如果是Elasticsearch服务端集群,可以指定多个地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {
/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
// HttpHost.create("")
));

/* 使用RestHighLevelClient对象 */
System.out.println(restHighLevelClient);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}

}

对索引库的操作

  • 通过从indices()方法中调用的方法,对索引库操作

新增索引库

  • 通过indices().create()方法创建索引库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {

/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
CreateIndexRequest request = new CreateIndexRequest("索引库名");
// 2. 准备DLS语句
String dls = "{\"mappings\": {\"properties\": {\"字段名\": {\"type\": \"字段类型\"},\"id\": {\"type\": \"keyword\",\"index\": false},\"name\": {\"type\": \"text\",\"analyzer\": \"ik_smart\"},\"engilshname\": {\"type\": \"text\",\"copy_to\": \"name\"},\"father\": {\"properties\": {\"son\": {\"type\": \"keyword\"}}}}}}\n";
request.source(dls, XContentType.JSON);
// 3. 发起请求
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

删除索引库

  • 通过indices().delete()方法删除索引库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("索引库名");
// 2. 发起请求
restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

判断索引库是否存在

  • 通过indices().exists()方法判断索引库是否存在
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
GetIndexRequest request = new GetIndexRequest("索引库名");
// 2. 发起请求
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

对文档操作

新增文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
IndexRequest request = new IndexRequest("索引库名").id("1");
// 2. 准备JSON文档
String dls = "{\"key\": \"value\"}";
request.source(dls, XContentType.JSON);
// 3. 发起请求
restHighLevelClient.index(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

批量新增文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
BulkRequest request = new BulkRequest();
// 2. 准备多个JSON文档
request.add(new IndexRequest("索引库名").id("1").source("{\"key\": \"value\"}", XContentType.JSON));
request.add(new IndexRequest("索引库名").id("2").source("{\"key\": \"value\"}", XContentType.JSON));
// 3. 发起请求
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

查询文档

  • 如果没有查询到文档,会返回null
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
GetRequest request = new GetRequest("索引库名", "1");
// 2. 发起请求得到响应
GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT);
// 3. 解析响应数据
System.out.println(documentFields.getSourceAsString());

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

修改文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
UpdateRequest request = new UpdateRequest("索引库名", "1");
// 2. 准备修改的文档内容
request.doc("key1", "value1", "key2", "value2");
// 3. 发起请求
restHighLevelClient.update(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

删除文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
DeleteRequest request = new DeleteRequest("索引库名", "1");
// 2. 发起请求
restHighLevelClient.delete(request, RequestOptions.DEFAULT);

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

搜索文档的操作

  • 通过QueryBuilders对象提供的方法进行搜索文档的操作

简单查询

查询所有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.matchAllQuery());
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

全文检索

根据指定的单个字段全文检索
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.matchQuery("字段名", "关键字"));
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}
根据指定的多个字段全文检索
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.multiMatchQuery("关键字", "字段名1", "字段名2"));
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

精确查询

完全匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.termQuery("字段名", "关键字"));
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}
匹配数值范围
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.rangeQuery("字段名").gte(最小值).lte(最大值));
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

复合查询

布尔查询

  • 通过boolQueryBuilder对象添加查询条件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("字段名", "关键字"));
boolQueryBuilder.filter(QueryBuilders.rangeQuery("字段名").gte(100).lte(100));
request.source().query(boolQueryBuilder);
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

搜索结果处理

  • 通过source()方法中的方法进行搜索结果的处理

排序

SortOrder.ASC:升序
SortOrder.DESC:降序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {

/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.matchAllQuery());
request.source().sort("字段名", SortOrder.ASC);
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

分页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {

// 指定页数
int page = 1;
// 指定每页文档个数
int size = 10;

/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.matchAllQuery());
request.source().from((page - 1) * size).size(size);
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

高亮

  • 通过Spring的工具类实现数组的非空判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.Map;

public class Tests {

@Test
public void test() throws IOException {

/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().query(QueryBuilders.matchAllQuery());
request.source().highlighter(new HighlightBuilder().field("字段名").requireFieldMatch(false).preTags("<em>").postTags("</em>"));
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
SearchHits searchHits = response.getHits();
// 查询文档总数
long total = searchHits.getTotalHits().value;
// 查询所有数据内容
SearchHit[] hits = searchHits.getHits();
// 遍历数据内容
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
System.out.println(json);

/* 解析高亮数据 */
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
// 非空判断
if (!CollectionUtils.isEmpty(highlightFields)) {
HighlightField highlightField = highlightFields.get("字段名");
String highlightString = highlightField.getFragments()[0].string();
System.out.println(highlightString);
}
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

聚合搜索

桶聚合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

public class Tests {

@Test
public void test() throws IOException {

/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("文档名");
// 2. 准备DSL参数
request.source().size(0);
request.source().aggregation(
AggregationBuilders.terms("聚合名").field("字段名").size(10)
);
// 3. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
Aggregations aggregations = response.getAggregations();
// 根据聚合名获取聚合结果
Terms terms = aggregations.get("聚合名");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
// 遍历聚合结果
for (Terms.Bucket bucket : buckets) {
// 解析key
String key = bucket.getKeyAsString();
System.out.println(key);
// 解析doc_count
long docCount = bucket.getDocCount();
System.out.println(docCount);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

自动补全搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class Tests {

@Test
public void test() throws IOException {


/* 创建RestHighLevelClient对象 */
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("127.0.0.1:9200")
));

/* 使用RestHighLevelClient对象 */
// 1. 创建Request对象
SearchRequest request = new SearchRequest("索引库名");
request.source().suggest(new SuggestBuilder().addSuggestion("查询名称", SuggestBuilders.completionSuggestion("字段名").prefix("前缀关键字").skipDuplicates(true).size(10)));
// 2. 发起请求得到响应
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
// 4. 响应数据
System.out.println(response);

/* 解析结果 */
Suggest suggest = response.getSuggest();
CompletionSuggestion suggestion = suggest.getSuggestion("查询名称");
for (CompletionSuggestion.Entry.Option option : suggestion.getOptions()) {
// 获取自动补全的值
String value = option.getText().toString();
System.out.println(value);
}

/* 销毁RestHighLevelClient对象 */
restHighLevelClient.close();
}
}

完成

参考文献

哔哩哔哩——黑马程序员