es8.2 + springboot 整合
1、 引入 maven 依赖
<!-- https://mvnrepository.com/artifact/co.elastic.clients/elasticsearch-java --> <dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.2.0</version> </dependency> <dependency> <groupId>jakarta.json</groupId> <artifactId>jakarta.json-api</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.1</version> </dependency>
2、 配置 客户端 bean
import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.transport.ElasticsearchTransport; import co.elastic.clients.transport.rest_client.RestClientTransport; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ElasticSearchConfig { @Bean public ElasticsearchClient elasticsearchClient() { // 创建低级客户端 RestClient restClient = RestClient.builder(new HttpHost("192.168.1.103", 9200)).build(); //使用Jackson 映射器创建传输层 ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); //创建 API 客户端 ElasticsearchClient client = new ElasticsearchClient(transport); return client; } }
3、 简单索引操作
@Autowired ElasticsearchClient client; /** * 创建 索引 * @throws IOException */ @Test public void createTest() throws IOException { client.indices().create(c -> c.index("products")); } /** * 查询 索引 * @throws IOException */ @Test public void queryTest() throws IOException { GetIndexResponse getIndexResponse = client.indices().get(i -> i.index("products")); System.out.println(getIndexResponse.get("products")); } /** * 判断 index 是否存在 * @throws IOException */ @Test public void existsTest() throws IOException { BooleanResponse booleanResponse = client.indices().exists(e -> e.index("products")); System.out.println(booleanResponse.value()); } /** * 删除 索引 */ @Test public void deleteTest() throws IOException { client.indices().delete(d -> d.index("products")); }
4、 简单数据操作
/** * 插入数据 * @throws IOException */ @Test public void addDocTest() throws IOException { User user = new User("zhangsan", 18); client.index( i -> i.index("products") //索引 .id("1") //ID .document(user)); //值对象 } /** * 更新数据 * @throws IOException */ @Test public void updateDocTest() throws IOException { client.update(u -> u.index("products") .id("1") .doc(new User("douger", 23)) , User.class); } /** * 查询 数据是否存在 * @throws IOException */ @Test public void existDocTest() throws IOException { BooleanResponse booleanResponse = client.exists(e -> e.index("products").id("1")); System.out.println(booleanResponse.value()); } /** * 查询数据 * @throws IOException */ @Test public void getDocTest() throws IOException { GetResponse<User> getResponse = client.get(g -> g.index("products") .id("1") , User.class); System.out.println(getResponse.source()); } /** * 删除 数据 * @throws IOException */ @Test public void deleteDocTest() throws IOException { DeleteResponse products = client.delete(d -> d.index("products").id("1")); System.out.println(products.id()); } /** * 批量插入数据 * @throws IOException */ @Test public void bulkTest() throws IOException { List<User> userList = new ArrayList<>(); userList.add(new User("hello world", 10)); userList.add(new User("hello java", 11)); userList.add(new User("hello girl", 12)); userList.add(new User("hello spring", 13)); userList.add(new User("hello es", 14)); userList.add(new User("user6", 15)); List<BulkOperation> bulkOperations = new ArrayList<>(); for(User user : userList) { bulkOperations.add(BulkOperation.of(o -> o.index(i -> i.document(user)))); } client.bulk(b -> b.index("products").operations(bulkOperations)); } /** * 查询 * @throws IOException */ @Test public void searchTest() throws IOException { SearchResponse<User> search = client.search(s -> s.index("products") // 查询 name 字段包含 hello 的 document(不使用分词器精确查找) .query(q -> q.term(t -> t.field("name") .value(v -> v.stringValue("hello")) )) // 分页查询, 查询前3条数据 .from(0) .size(3) // 按 age 降序排序 .sort(f -> f.field(o -> o.field("age").order(SortOrder.Desc))), User.class); for(Hit<User> hit : search.hits().hits()) { System.out.println(hit.source()); } }