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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| 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()); } } }
|