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
| package com.flepeng.esspringboot;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
class SpringbootElasticsearchApplicationTests {
@Autowired public RestHighLevelClient restHighLevelClient; @Test public void testCreateIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("liuyou_index"); CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); System.out.println(response); restHighLevelClient.close(); } @Test public void testIndexIsExists() throws IOException { GetIndexRequest request = new GetIndexRequest("index"); boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); restHighLevelClient.close(); } @Test public void testDeleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("liuyou_index"); AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); restHighLevelClient.close(); } @Test public void testAddDocument() throws IOException { User liuyou = new User("liuyou", 18); IndexRequest request = new IndexRequest("liuyou_index"); request.id("1"); request.timeout(TimeValue.timeValueMillis(1000)); request.source(JSON.toJSONString(liuyou), XContentType.JSON); IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(response.status()); System.out.println(response); } @Test public void testGetDocument() throws IOException { GetRequest request = new GetRequest("liuyou_index","1"); GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT); System.out.println(response.getSourceAsString()); System.out.println(request); restHighLevelClient.close(); } @Test public void testDocumentIsExists() throws IOException { GetRequest request = new GetRequest("liuyou_index", "1"); request.fetchSourceContext(new FetchSourceContext(false)); request.storedFields("_none_"); boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT); System.out.println(exists); } @Test public void testUpdateDocument() throws IOException { UpdateRequest request = new UpdateRequest("liuyou_index", "1"); User user = new User("lmk",11); request.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT); System.out.println(response.status()); restHighLevelClient.close(); } @Test public void testDeleteDocument() throws IOException { DeleteRequest request = new DeleteRequest("liuyou_index", "1"); request.timeout("1s"); DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT); System.out.println(response.status()); } @Test public void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "liuyou"); searchSourceBuilder.highlighter(new HighlightBuilder()); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchSourceBuilder.query(termQueryBuilder); searchRequest.source(searchSourceBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(JSON.toJSONString(hits)); System.out.println("======================="); for (SearchHit documentFields : hits.getHits()) { System.out.println(documentFields.getSourceAsMap()); } } @Test public void test() throws IOException { IndexRequest request = new IndexRequest("bulk"); request.source(JSON.toJSONString(new User("liu",1)),XContentType.JSON); request.source(JSON.toJSONString(new User("min",2)),XContentType.JSON); request.source(JSON.toJSONString(new User("kai",3)),XContentType.JSON); IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(index.status()); } @Test public void testBulk() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList<User> users = new ArrayList<>(); users.add(new User("liuyou-1",1)); users.add(new User("liuyou-2",2)); users.add(new User("liuyou-3",3)); users.add(new User("liuyou-4",4)); users.add(new User("liuyou-5",5)); users.add(new User("liuyou-6",6)); for (int i = 0; i < users.size(); i++) { bulkRequest.add( new IndexRequest("bulk") .id(""+(i + 1)) .source(JSON.toJSONString(users.get(i)),XContentType.JSON) ); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.status()); }
|