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
|
@Test public void addDocTest() throws IOException { String str = "{\"account_number\":1,\"balance\":39225,\"firstname\":\"Amber\",\"lastname\":\"Duke\",\"age\":32,\"gender\":\"M\",\"address\":\"880 Holmes Lane\",\"employer\":\"Pyrami\",\"email\":\"amberduke@pyrami.com\",\"city\":\"Brogan\",\"state\":\"IL\"}\n"; Account account = JsonUtils.fromJSON(str, Account.class); IndexRequest indexRequest = new IndexRequest() .index(Constants.INDEX_NAME) .type(Constants.TYPE) .id(String.valueOf(account.getAccount_number())) .source(BeanMapUtil.objectToStringMap(account)); IndexResponse indexResponse = highLevelClient.index(indexRequest, RequestOptions.DEFAULT); String index = indexResponse.getIndex(); String type = indexResponse.getType(); String id = indexResponse.getId(); long version = indexResponse.getVersion(); String result = indexResponse.getResult().getLowercase(); ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); log.info("新增文档|index={}, type={}, id={}, version={}, result={}, shardInfo={}", index, type, id, version, result, shardInfo); }
@Test public void getDocTest() throws IOException { GetRequest getRequest = new GetRequest() .index(Constants.INDEX_NAME)
.type(Constants.TYPE) .id("1"); GetResponse response = highLevelClient.get(getRequest, RequestOptions.DEFAULT); log.info("获取文档|{}", response.getSource()); }
@Test public void updateDocTest() throws IOException { String str = "{\"account_number\":1,\"balance\":38000,\"firstname\":\"Amber\",\"lastname\":\"Duke\",\"age\":32,\"gender\":\"M\",\"address\":\"880 Holmes Lane\",\"employer\":\"Pyrami\",\"email\":\"amberduke@pyrami.com\",\"city\":\"Brogan\",\"state\":\"IL\"}\n"; Account account = JsonUtils.fromJSON(str, Account.class); UpdateRequest updateRequest = new UpdateRequest() .index(Constants.INDEX_NAME) .type(Constants.TYPE) .id("1") .doc(BeanMapUtil.objectToStringMap(account)); UpdateResponse response = highLevelClient.update(updateRequest, RequestOptions.DEFAULT); log.info("更新文档|{}", response); }
@Test public void bulkTest() throws IOException { String str = "{\"account_number\":1,\"balance\":38000,\"firstname\":\"Amber\",\"lastname\":\"Duke\",\"age\":32,\"gender\":\"M\",\"address\":\"880 Holmes Lane\",\"employer\":\"Pyrami\",\"email\":\"amberduke@pyrami.com\",\"city\":\"Brogan\",\"state\":\"IL\"}\n"; UpdateRequest updateRequest = new UpdateRequest() .index(Constants.INDEX_NAME) .type(Constants.TYPE) .id("1") .doc(BeanMapUtil.objectToStringMap(JsonUtils.fromJSON(str, Account.class)));
String str2 = "{\"account_number\":6,\"balance\":5686,\"firstname\":\"Hattie\",\"lastname\":\"Bond\",\"age\":36,\"gender\":\"M\",\"address\":\"671 Bristol Street\",\"employer\":\"Netagy\",\"email\":\"hattiebond@netagy.com\",\"city\":\"Dante\",\"state\":\"TN\"}\n"; Account account = JsonUtils.fromJSON(str2, Account.class); IndexRequest indexRequest = new IndexRequest() .index(Constants.INDEX_NAME) .type(Constants.TYPE) .id(String.valueOf(account.getAccount_number())) .source(BeanMapUtil.objectToStringMap(account));
BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(updateRequest) .add(indexRequest); BulkResponse response = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); for (BulkItemResponse bulkItemResponse : response) { DocWriteResponse itemResponse = bulkItemResponse.getResponse(); if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) { IndexResponse indexResponse = (IndexResponse) itemResponse;
} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) { UpdateResponse updateResponse = (UpdateResponse) itemResponse;
} else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) { DeleteResponse deleteResponse = (DeleteResponse) itemResponse; } } }
@Test public void searchTest() { AccountRequest request = new AccountRequest(); request.setFrom(0); request.setSize(10); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
String gender = request.getGender(); if (gender != null) { boolQueryBuilder.must(QueryBuilders.termQuery("gender", gender)); }
List<Integer> ages = request.getAges(); if (!CollectionUtils.isEmpty(ages)) { boolQueryBuilder.must(QueryBuilders.termsQuery("age", ages)); }
Long startBalance = request.getStartBalance(); if (startBalance != null) { boolQueryBuilder.must(QueryBuilders.rangeQuery("balance").gte(startBalance)); } Long endBalance = request.getEndBalance(); if (endBalance != null) { boolQueryBuilder.must(QueryBuilders.rangeQuery("balance").lte(endBalance)); }
String addressKey = request.getAddressKey(); if (addressKey != null) { boolQueryBuilder.must(QueryBuilders.matchQuery("address", addressKey)); }
String preAddress = request.getPreAddress(); if (preAddress != null) { boolQueryBuilder.must(QueryBuilders.prefixQuery("address", preAddress)); }
String nameKey = request.getNameKey(); if (nameKey != null) { BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery() .should(QueryBuilders.termQuery("firstname", nameKey)) .should(QueryBuilders.termQuery("lastname", nameKey)); boolQueryBuilder.must(shouldQuery); }
String notState = request.getNotState(); if (notState != null) { boolQueryBuilder.mustNot(QueryBuilders.termsQuery("state", notState)); }
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.from(request.getFrom()); searchSourceBuilder.size(request.getSize());
SearchRequest searchRequest = new SearchRequest() .indices(Constants.INDEX_NAME) .source(searchSourceBuilder); try { SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); RestStatus restStatus = searchResponse.status(); if (restStatus != RestStatus.OK) { log.error("查询结果异常|" + restStatus); return; }
SearchHits searchHits = searchResponse.getHits(); long totalHits = searchHits.totalHits; log.info("搜索结果|totalHits={}", totalHits); if (totalHits > 0) { for (SearchHit hit : searchHits.getHits()) { String source = hit.getSourceAsString(); log.info("搜索结果|{}", source); } } } catch (IOException e) { e.printStackTrace(); }
}
|