package com.gulimall.gulimallsearch.search;
import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.SortOrder; import co.elastic.clients.elasticsearch.core.*; import co.elastic.clients.elasticsearch.core.bulk.BulkOperation; import co.elastic.clients.elasticsearch.core.search.Hit; import co.elastic.clients.elasticsearch.core.search.HitsMetadata; import co.elastic.clients.elasticsearch.indices.CreateIndexResponse; import co.elastic.clients.elasticsearch.indices.GetMappingResponse; import co.elastic.clients.transport.endpoints.BooleanResponse; import com.fasterxml.jackson.databind.ObjectMapper; import com.gulimall.gulmallsearch.entity.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException; import java.util.ArrayList; import java.util.List;
@SpringBootTest public class TestElasticsearch { @Autowired private ElasticsearchClient elasticsearchClient;
@Test public void testSearch() { System.out.println(elasticsearchClient); }
@Test void indexData() throws IOException {
CreateIndexResponse java_client_index = elasticsearchClient.indices().create(createIndexBuilder -> createIndexBuilder.index("java_client_index") ); Boolean acknowledged = java_client_index.acknowledged(); System.out.println("索引操作:" + acknowledged); }
@Test void searchData() throws IOException { GetMappingResponse mapping = elasticsearchClient.indices().getMapping(e -> e.index("java_client_index")); System.out.println(mapping); System.out.println(elasticsearchClient.indices().get(r -> r.index("java_client_index")).result().size()); System.out.println(elasticsearchClient.indices().getAlias(e -> e.index("java_client_index"))); System.out.println(elasticsearchClient.indices().getSettings(e -> e.index("java_client_index")));
}
@Test void deleteIndex() throws IOException { System.out.println(elasticsearchClient.indices().delete(r -> r.index("java_client_index")).acknowledged()); }
@Test void insertIndex() throws IOException { User user = new User("byz", "男", 25); ObjectMapper objectMapper = new ObjectMapper(); String userJson = objectMapper.writeValueAsString(user); System.out.println(userJson); IndexResponse indexResponse = elasticsearchClient.index(r -> r.index("user").id("1001").document(user)); System.out.println(indexResponse.result().jsonValue()); }
@Test void updateIndex() throws IOException { User user = new User(); user.setAge(15); UpdateResponse<User> updateUser = elasticsearchClient.update(r -> r.index("user").id("1001").doc(user), User.class); System.out.println(updateUser); }
@Test void existDocument() throws IOException { BooleanResponse userBoolean = elasticsearchClient.exists(r -> r.index("user").id("1001")); System.out.println(userBoolean.value()); }
@Test void getDocument() throws IOException { GetResponse<User> resp = elasticsearchClient.get(r -> r.index("user").id("1001"), User.class); User source = resp.source(); System.out.println(source); }
@Test void deleteDocument() throws IOException { DeleteResponse deleteResponse = elasticsearchClient.delete(r -> r.index("user").id("1001")); System.out.println(deleteResponse.result().jsonValue()); }
@Test void bulkList() throws IOException { List<User> userList = new ArrayList<>(); userList.add(new User("jon", "男", 18)); userList.add(new User("lucy", "男", 17)); userList.add(new User("jack", "女", 199)); userList.add(new User("person", "男", 28)); userList.add(new User("user", "女", 23)); List<BulkOperation> bulkOperations = new ArrayList<>(); for (User user : userList) { bulkOperations.add(BulkOperation.of(o -> o.index(r -> r.document(user)))); } BulkResponse bulkResponse = elasticsearchClient.bulk(b -> b.index("user").operations(bulkOperations)); System.out.println(bulkResponse.errors()); }
@Test void searchTest() throws IOException { SearchResponse<User> search = elasticsearchClient.search(s -> s .index("user") .query(q -> q .term(t -> t .field("sex") .value(v -> v.stringValue("男")) )) .from(0) .size(3) .sort(f -> f.field(o -> o.field("age").order(SortOrder.Desc))), User.class); HitsMetadata<User> hits = search.hits(); List<Hit<User>> hitsUser = hits.hits(); System.out.println(hits.total().value()); for (Hit<User> userHit : hitsUser) { System.out.println(userHit.source()); } } }
|