導(dǎo)入依賴
? <dependency>
? ? ? ? <groupId>com.fasterxml.jackson.core</groupId>
? ? ? ? <artifactId>jackson-databind</artifactId>
? ? ? ? <version>2.13.2</version>
? </dependency>
?
? <dependency>
? ? ? ? <groupId>org.glassfish</groupId>
? ? ? ? <artifactId>jakarta.json</artifactId>
? ? ? ? <version>2.0.1</version>
? </dependency>
? ? ? ?
? <dependency>
? ? ? ? <groupId>co.elastic.clients</groupId>
? ? ? ? <artifactId>elasticsearch-java</artifactId>
? ? ? ? <version>8.1.0</version>
? </dependency>
配置
@Configuration
public class ElasticSearchConfig {
? ? @Bean
? ? public ElasticsearchClient elasticsearchClient(){
? ? ? ? RestClient client = RestClient.builder(new HttpHost("localhost", 9200,"http")).build();
? ? ? ? ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
? ? ? ? return new ElasticsearchClient(transport);
? ? }
}
增加索引文章來源:http://www.zghlxwxcb.cn/news/detail-419879.html
? @Autowired
? private ElasticsearchClient client;
??
? @Test
? ? public void createTest() throws IOException {
? ? ? ??
? ? ? ? //寫法比RestHighLevelClient更加簡潔
? ? ? ? CreateIndexResponse indexResponse = client.indices().create(c -> c.index("user"));
? ? }文章來源地址http://www.zghlxwxcb.cn/news/detail-419879.html
增加index
? @Autowired
? private ElasticsearchClient client;
??
? @Test
? ? public void createTest() throws IOException {
? ? ? ??
? ? ? ? CreateIndexResponse indexResponse = client.indices().create(c -> c.index("user"));
? ? }
查詢Index
? ? @Test
? ? public void queryTest() throws IOException {
? ? ? ? GetIndexResponse getIndexResponse = client.indices().get(i -> i.index("user"));
? ? }
?判斷index是否存在
? ? @Test
? ? public void existsTest() throws IOException {
? ? ? ? BooleanResponse booleanResponse = client.indices().exists(e -> e.index("user"));
? ? ? ? System.out.println(booleanResponse.value());
? ? }
刪除index
@Test
? ? public void deleteTest() throws IOException {
? ? ? ? DeleteIndexResponse deleteIndexResponse = client.indices().delete(d -> d.index("user"));
? ? ? ? System.out.println(deleteIndexResponse.acknowledged());
? ? }
Document CRUD
插入document?
? ? @Test ? ? public void addDocumentTest() throws IOException { ? ? ? ? ? User user = new User("user1", 10); ? ? ? ? IndexResponse indexResponse = client.index(i -> i ? ? ? ? ? ? ? ? .index("user") ? ? ? ? ? ? ? ? ? //設(shè)置id ? ? ? ? ? ? ? ? .id("1") ? ? ? ? ? ? ? ? ? //傳入user對象 ? ? ? ? ? ? ? ? .document(user)); ? ? ? }
?更新Document
? ? @Test
? ? public void updateDocumentTest() throws IOException {
? ? ? ? UpdateResponse<User> updateResponse = client.update(u -> u
? ? ? ? ? ? ? ? ? ? ? ? .index("user")
? ? ? ? ? ? ? ? ? ? ? ? .id("1")
? ? ? ? ? ? ? ? ? ? ? ? .doc(new User("user2", 13))
? ? ? ? ? ? ? ? , User.class);
? ? }
判斷Document是否存在
? ? @Test
? ? public void existDocumentTest() throws IOException {
? ? ? ? BooleanResponse indexResponse = client.exists(e -> e.index("user").id("1"));
? ? ? ? System.out.println(indexResponse.value());
? ? }
查詢Document
? ? @Test
? ? public void getDocumentTest() throws IOException {
? ? ? ? GetResponse<User> getResponse = client.get(g -> g
? ? ? ? ? ? ? ? ? ? ? ? .index("user")
? ? ? ? ? ? ? ? ? ? ? ? .id("1")
? ? ? ? ? ? ? ? , User.class
? ? ? ? );
? ? ? ? System.out.println(getResponse.source());
? ? }
刪除Document
? ? @Test
? ? public void deleteDocumentTest() throws IOException {
? ? ? ? DeleteResponse deleteResponse = client.delete(d -> d
? ? ? ? ? ? ? ? .index("user")
? ? ? ? ? ? ? ? .id("1")
? ? ? ? );
? ? ? ? System.out.println(deleteResponse.id());
? ? }
批量插入Document
? ? @Test
? ? public void bulkTest() throws IOException {
? ? ? ? List<User> userList = new ArrayList<>();
? ? ? ? userList.add(new User("user1", 11));
? ? ? ? userList.add(new User("user2", 12));
? ? ? ? userList.add(new User("user3", 13));
? ? ? ? userList.add(new User("user4", 14));
? ? ? ? userList.add(new User("user5", 15));
? ? ? ? List<BulkOperation> bulkOperationArrayList = new ArrayList<>();
? ? ? ? //遍歷添加到bulk中
? ? ? ? for(User user : userList){
? ? ? ? ? ? bulkOperationArrayList.add(BulkOperation.of(o->o.index(i->i.document(user))));
? ? ? ? }
? ? ?
? ? ? ? BulkResponse bulkResponse = client.bulk(b -> b.index("user")
? ? ? ? ? ? ? ? .operations(bulkOperationArrayList));
?
? ? }
?查詢
? ? @Test
? ? public void searchTest() throws IOException {
? ? ? ? SearchResponse<User> search = client.search(s -> s
? ? ? ? ? ? ? ? .index("user")
? ? ? ? ? ? ? ? //查詢name字段包含hello的document(不使用分詞器精確查找)
? ? ? ? ? ? ? ? .query(q -> q
? ? ? ? ? ? ? ? ? ? ? ? .term(t -> t
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .field("name")
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .value(v -> v.stringValue("hello"))
? ? ? ? ? ? ? ? ? ? ? ? ))
? ? ? ? ? ? ? ? //分頁查詢,從第0頁開始查詢3個document
? ? ? ? ? ? ? ? .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());
? ? ? ? }
? ? }
到了這里,關(guān)于springBoot整合ElasticSearch8.x版本的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!