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
| @SpringBootTest class KuangEsApiApplicationTests {
@Autowired @Qualifier("restHighLevelClient") private RestHighLevelClient client;
@Test void testCreateIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("kuang_index"); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(createIndexResponse); }
@Test void testExistsIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("kuang_index"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); }
@Test void testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("kuang_index"); AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); }
@Test void testAddDocument() throws IOException { User user = new User("狂神说", 3); IndexRequest request = new IndexRequest("kuang_index"); request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); request.timeout("1s");
IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString()); System.out.println(indexResponse.status()); }
@Test void testIsExists() throws IOException { GetRequest getRequest = new GetRequest("kuang_index", "1"); getRequest.fetchSourceContext(new FetchSourceContext(false)); getRequest.storedFields("_none_"); boolean exists = client.exists(getRequest, RequestOptions.DEFAULT); System.out.println(exists); }
@Test void testGetDocument() throws IOException { GetRequest getRequest = new GetRequest("kuang_index", "1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString()); System.out.println(getResponse); }
@Test void testUpdateDocument() throws IOException { UpdateRequest updateRequest = new UpdateRequest("kuang_index", "1"); updateRequest.timeout("1s"); User user = new User("狂神说Java", 18); updateRequest.doc(JSON.toJSONString(user), XContentType.JSON); UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println(updateResponse.status()); }
@Test void testDeleteDocument() throws IOException { DeleteRequest deleteRequest = new DeleteRequest("kuang_index", "1"); deleteRequest.timeout("1s"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(deleteResponse); }
@Test void testBulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s");
List<User> userList = new ArrayList<>(); userList.add(new User("kuangshen1", 3)); userList.add(new User("kuangshen2", 3)); userList.add(new User("kuangshen3", 3)); userList.add(new User("qinjiang1", 3)); userList.add(new User("qinjiang2", 3)); userList.add(new User("qinjiang3", 3));
for (int i = 0; i < userList.size(); i++) { bulkRequest.add( new IndexRequest("kuang_index") .id("" + (i + 1)) .source(JSON.toJSONString(userList.get(i)), XContentType.JSON)); } BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulkResponse.hasFailures()); }
@Test void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(ESConst.ES_INDEX); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "qinjiang1"); sourceBuilder.query(termQueryBuilder); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); System.out.println("======================"); for (SearchHit documentFields : searchResponse.getHits().getHits()) { System.out.println(documentFields.getSourceAsMap()); } } }
|