软件环境:docker、es7.6.1、kibana7.6.1、SpringBoot 2.3.2.RELEASE
1、项目结构
1639661783(1).png2、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.geovis</groupId>
<artifactId>elasticsearch-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elasticsearch-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、application.properties
server.port=8080
server.servlet.context-path=/es
4、ElasticSearchClientConfig.java
package com.geovis.elasticsearchapi.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
return client;
}
}
5、ElasticsearchApiApplicationTests.java
package com.geovis.elasticsearchapi;
import com.alibaba.fastjson.JSON;
import com.geovis.elasticsearchapi.pojo.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class ElasticsearchApiApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
// 创建索引
@Test
public void testCreateIndex() throws IOException {
// 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("test_index");
// 设置索引的settings
request.settings(
Settings.builder()
.put("index.number_of_shards", 5)
.put("index.number_of_replicas", 1)
.put("analysis.analyzer.default.tokenizer", "ik_smart")
);
request.mapping(
"{\n" +
"\"properties\":{\n" +
"\"name\":{\n" +
"\"type\": \"keyword\",\n"+
"\"store\": true\n"+
"},\n"+
"\"age\":{\n" +
"\"type\": \"long\",\n"+
"\"store\": true\n"+
"}\n"+
"}\n" +
"}\n",
XContentType.JSON
);
// 执行创建请求,请求后获取响应
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
// 获取索引,判断是否存在
@Test
public void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("test_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
// 删除索引
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test_index");
AcknowledgedResponse acknowledgedResponse = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(acknowledgedResponse.isAcknowledged());
}
// 创建文档
@Test
public void testCreateDocument() throws IOException {
User user = User.builder().name("aaa").age(10).build();
IndexRequest request = new IndexRequest("test_index");
// 规则 put /test_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.source(JSON.toJSONString(user), XContentType.JSON);
// 客户端发起请求,获取响应结果
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.status());
}
// 获取文档,判断是否存在
@Test
public void testExistDocument() throws IOException {
GetRequest request = new GetRequest("test_index", "1");
// 不返回 _source的上下文了
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
// 获取文档信息
@Test
public void testGetDocument() throws IOException {
GetRequest request = new GetRequest("test_index", "1");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
Map<String, Object> map = response.getSourceAsMap();
System.out.println(map);
}
// 更新文档信息
@Test
public void testUpdateDocument() throws IOException {
User user = User.builder().name("bbb").age(100).build();
UpdateRequest request = new UpdateRequest("test_index", "1");
request.timeout("1s");
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
// 删除文档
@Test
public void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("test_index", "1");
request.timeout("1s");
DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
System.out.println(delete.status());
}
// 批量插入
@Test
public void testBulkCreateDocument() throws IOException {
ArrayList<User> list = new ArrayList<>();
list.add(new User("111", 1));
list.add(new User("222", 2));
list.add(new User("333", 3));
BulkRequest request = new BulkRequest();
request.timeout("10s");
for (int i = 0; i < list.size(); i++) {
IndexRequest request1 = new IndexRequest("test_index")
.id(""+i)
.source(JSON.toJSONString(list.get(i)), XContentType.JSON);
request.add(request1);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.hasFailures());
}
// 查询
@Test
public void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest("test_index");
// 构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询条件,我们可以使用 QueryBuilders 工具实现
// QueryBuilders.termQuery 精确匹配
// QueryBuilders.matchAllQuery() 匹配所有
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "111");
sourceBuilder.query(termQueryBuilder);
// sourceBuilder.from();
// sourceBuilder.size();
// new HighlightBuilder();
// sourceBuilder.highlighter();
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("===============================");
for (SearchHit hit: searchResponse.getHits().getHits()){
System.out.println(hit.getSourceAsMap());
}
}
// 添加测试数据
@Test
void addTestData() throws IOException {
ArrayList<User> list = new ArrayList<>();
for (int i = 0; i <20; i++) {
list.add(new User("java"+i, i));
}
ArrayList<User> list2 = new ArrayList<>();
for (int i = 0; i <20; i++) {
list2.add(new User("vue"+i, i));
}
BulkRequest request = new BulkRequest();
request.timeout("10s");
for (int i = 0; i < list.size(); i++) {
IndexRequest request1 = new IndexRequest("test_index")
.source(JSON.toJSONString(list.get(i)), XContentType.JSON);
request.add(request1);
}
for (int i = 0; i < list2.size(); i++) {
IndexRequest request2 = new IndexRequest("test_index")
.source(JSON.toJSONString(list2.get(i)), XContentType.JSON);
request.add(request2);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.hasFailures());
}
}
6、TestController.java
package com.geovis.elasticsearchapi.controller;
import com.geovis.elasticsearchapi.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/searchPage")
public List<Map<String, Object>> searchPage(@RequestParam String keyword, @RequestParam int pageNo, @RequestParam int pageSize) throws IOException {
return testService.searchPage(keyword, pageNo, pageSize);
}
}
7、TestService.java
package com.geovis.elasticsearchapi.service;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class TestService {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
// ElasticSearch 5.0以后,String字段被拆分成两种新的数据类型: text用于全文搜索,会分词,而keyword用于关键词搜索,不进行分词
// term:精确查询, 这种查询适合keyword、numeric、date等明确值的
public List<Map<String, Object>> searchPage(String keyword, int pageNo, int pageSize) throws IOException {
// 创建搜索
SearchRequest searchRequest = new SearchRequest("test_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 模糊匹配
BoolQueryBuilder booleanQueryBuilder = new BoolQueryBuilder();
// TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "*"+keyword+"*");
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("name", "*"+keyword+"*");
booleanQueryBuilder.must(wildcardQueryBuilder);
sourceBuilder.query(booleanQueryBuilder);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 分页
sourceBuilder.from(pageNo);
sourceBuilder.size(pageSize);
// 执行搜索
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 封装结果
ArrayList<Map<String, Object>> result = new ArrayList<>();
SearchHit[] hits = searchResponse.getHits().getHits();
for (int i = 0; i < hits.length; i++) {
Map<String, Object> map = hits[i].getSourceAsMap();
result.add(map);
}
return result;
}
}
8、User.java
package com.geovis.elasticsearchapi.pojo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
1639662223(1).png
1639662303(1).png
网友评论