创建indexorg.elasticsearch.client elasticsearch-rest-high-level-client 7.15.2 commons-io commons-io 2.5
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修改数据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 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测试
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();
}
}
运行效果



