一、Java 客户端连接和索引创建
/**
* @author hzq
* @date 2022年07月14日 14:52
* JAVA API 操作--ES 连接客户端
*/
@Slf4j
public class EsClinet {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
esClinent.close();
}
二、索引的CRUD 操作
/**
* @author hzq
* @date 2022年07月14日 19:47
*
* 创建索引
*/
public class EsCreateIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
// 创建索引
CreateIndexRequest request = new CreateIndexRequest("user");
CreateIndexResponse response = esClinent.indices().create(request, RequestOptions.DEFAULT);
// 响应状态
boolean responseAck = response.isAcknowledged();
System.out.println("索引操作响应状态为:" + responseAck);
esClinent.close();
}
/**
* @author hzq
* @date 2022年07月14日 19:49
*
* 查询索引
*/
public class EsSearchIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
//查询索引
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = esClinent.indices().get(request, RequestOptions.DEFAULT);
//响应状态码
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
esClinent.close();
}
}
/**
* @author hzq
* @date 2022年07月14日 19:48
*
* 删除索引
*/
public class EsDeleteIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
DeleteIndexRequest request = new DeleteIndexRequest("user");
request.timeout("1s");
AcknowledgedResponse delete= esClinent.indices().delete(request,RequestOptions.DEFAULT);
System.out.println("删除索引返回的状态码{}"+delete.isAcknowledged());
}
}
二、文档型的CRUD 操作
/**
* @author hzq
* @date 2022年07月14日 22:32
* <p>
* 添加 文档型
*
* 更新都一样 只需要更改对应的方法即可。updateRequest
*/
@Slf4j
public class EsInsertDoc {
public static void main(String[] args){
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
IndexRequest request = null;
try {
// 插入数据
request = new IndexRequest();
request.index("user").id("1");
BookEntity bookEntity = new BookEntity();
bookEntity.setName("ES");
bookEntity.setMessage("开源搜索框架");
bookEntity.setType("java");
String userJson = JSON.toJSONString(bookEntity);
request.source(userJson, XContentType.JSON);
log.info("用户喜欢的书籍为:" + userJson);
IndexResponse response = esClinent.index(request, RequestOptions.DEFAULT);
response.getResult();
} catch (Exception e) {
if (request.id("1")==null){
log.info("插入用户喜欢的书籍异常",e);
}
log.error(e.getMessage());
} finally {
if (esClinent !=null){
try {
esClinent.close();
} catch (IOException e) {
log.error("关闭流异常"+e.getMessage());
}
}
}
}
}
/**
* @author hzq
* @date 2022年07月15日 13:12
*
* 查询 文档型
*
*
*/
public class EsQueryDoc {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
// 查询数据
GetRequest request = new GetRequest();
request.index("user").id("1");
GetResponse response = esClinent.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
esClinent.close();
}
}
/**
* @author hzq
* @date 2022年07月15日 13:21
*/
public class EsDeleteDoc {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClinent = new RestHighLevelClient(
RestClient.builder(new HttpHost("124.223.187.185", 9200, "http"))
);
// 删除数据
DeleteRequest request = new DeleteRequest();
request.index("user").id("1");
DeleteResponse response = esClinent.delete(request, RequestOptions.DEFAULT);
System.out.println("删除用户文档型数据:"+response.toString());
esClinent.close();
}
}
四、其他案例参考
https://blog.51cto.com/u_15069450/4529440