依赖
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
/**
* 创建索引
* @throws IOException
*/
private static void createIdx() throws IOException {
String mapping =
{
"mappings": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
System.out.println(service.doPut("http://localhost:9200/users", mapping, headers,null));
}
/**
* 创建或者更新用户信息
* @throws IOException
*/
private static void createOrUpdateUser() throws IOException {
User user = new User(1L,"Lisa","HR", "人力资源管理");
System.out.println(user);
System.out.println(service.doPost("http://localhost:9200/users/_doc/" + user.getId(), new Gson().toJson(user), headers,null));
}
/**
* 删除用户信息
* @throws IOException
*/
private static void deleteUser() throws IOException {
System.out.println(service.doDelete("http://localhost:9200/users/_doc/1"));
}
/**
* 根据用户ID获取用户信息
* @throws IOException
*/
private static void getUserById() throws IOException {
System.out.println(service.doGet("http://localhost:9200/users/_doc/1", null,null));
}
/**
* 获取所有的用户信息
* @throws IOException
*/
private static void getAllUser() throws IOException {
System.out.println(service.doGet("http://localhost:9200/users/_search", null,null));
}
/**
* 条件获取用户信息
* @throws IOException
*/
private static void getConditionUser() throws IOException {
String params = "{\"query\" : { \"match\" : { \"desc\" : \"搬砖\" }}, \"from\": 0, \"size\":100}";
System.out.println(service.doPost("http://localhost:9200/users/_search", params, headers,null));
}
HTTP请求具体封装省略,自行封装
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有