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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| package com.wzh.elasticsearch.doc;
import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch.core.*; import co.elastic.clients.elasticsearch.core.bulk.BulkOperation; import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem; import co.elastic.clients.elasticsearch.core.search.Hit; import co.elastic.clients.json.jackson.JacksonJsonpMapper; import co.elastic.clients.transport.rest_client.RestClientTransport; import com.wzh.elasticsearch.model.User; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.junit.Test;
import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List;
public class DocTest {
@Test public void searchDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) );
GetRequest.Builder builder = new GetRequest.Builder(); builder.id("101"); builder.index("user"); GetResponse<User> getResponse = esClient.get(builder.build(), User.class);
System.out.println("found==" + getResponse.found()); System.out.println("id==" + getResponse.id()); System.out.println("index==" + getResponse.index()); System.out.println("primaryTerm==" + getResponse.primaryTerm()); System.out.println("seqNo==" + getResponse.seqNo()); System.out.println("source==" + getResponse.source()); System.out.println("version==" + getResponse.version()); System.out.println("toString==" + getResponse.toString());
restClient.close(); }
@Test public void searchDoc2() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) ); GetResponse<User> getResponse = esClient.get(s -> s.index("user").id("101"), User.class); System.out.println(getResponse.toString()); }
@Test public void searchAllDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) ); int size = 10; SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query( q -> q.matchAll( a -> a.queryName("search01") ) ).size(size), User.class); System.out.println(searchResponse.toString()); List<Hit<User>> hits = searchResponse.hits().hits(); for (Hit<User> hit : hits) { System.out.println(hit); }
restClient.close(); }
@Test public void addDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()));
User user = new User(); user.setUid("101"); user.setUname("张三"); user.setAge(23); user.setGender("男");
IndexRequest.Builder<User> userBuilder = new IndexRequest.Builder<>(); userBuilder.index("user"); userBuilder.id(user.getUid()); userBuilder.document(user);
IndexResponse indexResponse = esClient.index(userBuilder.build()); System.out.println("result==" + indexResponse.result()); System.out.println("version==" + indexResponse.version()); System.out.println("id==" + indexResponse.id()); System.out.println("index==" + indexResponse.index()); System.out.println("seqNo==" + indexResponse.seqNo()); System.out.println("type==" + indexResponse.type());
restClient.close(); }
@Test public void updateDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper())); UpdateRequest.Builder<Object, Object> builder = new UpdateRequest.Builder<>(); builder.id("101"); builder.index("user");
User user = new User(); user.setUid("101"); user.setUname("lisi"); user.setAge(24); user.setGender("女"); builder.doc(user);
UpdateResponse<Object> updateResponse = esClient.update(builder.build(), User.class); System.out.println(updateResponse.toString());
restClient.close(); }
@Test public void updateDoc2() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) ); HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("uname", "wangwu");
UpdateResponse<User> updateResponse = esClient.update( s -> s.index("user").id("101").doc(hashMap), User.class); System.out.println(updateResponse.toString()); }
@Test public void deleteDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) ); DeleteRequest.Builder builder = new DeleteRequest.Builder(); builder.index("user"); builder.id("101"); DeleteResponse deleteResponse = esClient.delete(builder.build()); System.out.println(deleteResponse.toString()); }
@Test public void bulkAddDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) ); ArrayList<User> users = new ArrayList<>(); users.add(new User("1001", "张三", 23, "1999-11-11")); users.add(new User("1002", "李四", 24, "1999-11-11")); users.add(new User("1003", "王五", 25, "1999-11-11"));
ArrayList<BulkOperation> bulkOperations = new ArrayList<>(); for (User user : users) { bulkOperations.add(BulkOperation.of( s -> s.index( v -> v.id(user.getUid()).document(user) ) )); } BulkRequest.Builder builder = new BulkRequest.Builder(); builder.index("user"); builder.operations(bulkOperations); BulkResponse bulkResponse = esClient.bulk(builder.build()); List<BulkResponseItem> items = bulkResponse.items(); for (BulkResponseItem item : items) { System.out.println(item.result()); } System.out.println(bulkResponse.errors());
restClient.close(); }
@Test public void bulkDeleteDoc() throws IOException { RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchClient esClient = new ElasticsearchClient( new RestClientTransport(restClient, new JacksonJsonpMapper()) ); List<String> ids = new ArrayList<>(); ids.add("1002"); ids.add("1003");
List<BulkOperation> bulkOperations = new ArrayList<>(); for (String id : ids) { bulkOperations.add(BulkOperation.of( s -> s.delete( v -> v.id(id) ) )); } BulkRequest.Builder br = new BulkRequest.Builder(); br.index("user"); br.operations(bulkOperations); BulkResponse bulkResponse = esClient.bulk(br.build());
List<BulkResponseItem> items = bulkResponse.items(); items.forEach(s -> System.out.println(s.result()));
restClient.close(); } }
|