赞
踩
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
private static boolean createIndexAndMapping(RestHighLevelClient highLevelClient, String indexName) throws Exception { GetIndexRequest getIndexRequest = new GetIndexRequest(indexName); // 判断索引是否存在 if(!highLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT)) { File file = ResourceUtils.getFile("classpath:json/mapping.json"); // 读取mapping配置 String jsonStr = IOUtils.toString(file.toURI(), StandardCharsets.UTF_8); CreateIndexRequest request = new CreateIndexRequest(indexName); XContentBuilder builder = XContentFactory.jsonBuilder().map(JSONObject.parseObject(jsonStr)); request.mapping(builder); highLevelClient.indices().create(request, RequestOptions.DEFAULT); } return true; }
json文件
{
"properties": {
"userName": {
"type": "keyword"
},
"pwd": {
"type": "keyword"
}
}
}
private static int addUser(RestHighLevelClient highLevelClient, String indexName, Map<String, Object> data) throws IOException {
IndexRequest request = new IndexRequest(indexName);
request.source(data, XContentType.JSON);
IndexResponse indexResponse = highLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.status());
return indexResponse.status().getStatus();
}
private static long updatePwd(RestHighLevelClient highLevelClient, String indexName, String userName, String pwd) throws IOException { UpdateByQueryRequest request = new UpdateByQueryRequest(indexName); request.setRefresh(true); request.setQuery(QueryBuilders.termQuery("userName", userName)); Map<String, Object> params = new HashMap<>(); params.put("pwd", pwd); request.setScript(new Script(ScriptType.INLINE, "painless", "ctx._source.pwd=params.pwd", params)); BulkByScrollResponse response = highLevelClient.updateByQuery(request, RequestOptions.DEFAULT); return response.getStatus().getUpdated(); }
private static long deleteUser(RestHighLevelClient highLevelClient, String indexName, String userName) throws IOException {
DeleteByQueryRequest request = new DeleteByQueryRequest(indexName);
request.setRefresh(true);
request.setQuery(new TermQueryBuilder("userName", userName));
BulkByScrollResponse response = highLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
return response.getStatus().getDeleted();
}
private static List<Map<String, Object>> search(RestHighLevelClient highLevelClient, String indexName, String userName) throws IOException { SearchRequest searchRequest = new SearchRequest(indexName); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder .query(QueryBuilders.wildcardQuery("userName", userName)) .fetchSource(new String[] {"userName", "pwd"}, null); searchRequest.source(searchSourceBuilder); SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); return Stream.of(hits).map(hit -> hit.getSourceAsMap()).collect(Collectors.toList()); }
public static void main(String[] args) { RestHighLevelClient highLevelClient = new RestHighLevelClient( RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))); try { // 创建索引 createIndexAndMapping(highLevelClient, "test_user_index"); // 添加数据 addUser(highLevelClient, "test_user_index", Maps.mutable.of("userName", "张先生", "pwd", "123456")); addUser(highLevelClient, "test_user_index", Maps.mutable.of("userName", "张女士", "pwd", "123456")); Thread.sleep(1000); // 输出添加后的数据 search(highLevelClient, "test_user_index", "张*").stream() .forEach(System.out::println); // 更新密码 System.out.println(updatePwd(highLevelClient, "test_user_index", "张女士", "123")); // 查看更新后的数据 search(highLevelClient, "test_user_index", "张*").stream() .forEach(System.out::println); // 删除用户 System.out.println(deleteUser(highLevelClient, "test_user_index", "张先生")); // 查看删除后的数据 search(highLevelClient, "test_user_index", "张*").stream() .forEach(System.out::println); highLevelClient.close(); } catch (Exception e) { e.printStackTrace(); } }
运行效果
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。