org.elasticsearch.client
elasticsearch-rest-high-level-client
7.15.0
org.elasticsearch
elasticsearch
7.15.0
org.apache.logging.log4j
log4j-core
2.11.0
com.alibaba
fastjson
1.2.83
public static void main(String[] args) throws Exception {
createIndex();
writeDoc();
batchWriteDoc();
searchFormSize();
searchAfter();
searchScroll();
}
/**
* 获取ES连接客户端
*
* @return RestHighLevelClient
*/
private static RestHighLevelClient getRestHighLevelClient() {
HttpHost host = new HttpHost("172.21.xx.xxx", 9200, HttpHost.DEFAULT_SCHEME_NAME);
RestClientBuilder restClientBuilder = RestClient.builder(host);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));
restClientBuilder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
return restHighLevelClient;
}
/**
* 创建索引
*
* @throws Exception
* 异常
*/
public static void createIndex() throws Exception {
// 获取连接
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("mappings");
{
builder.startObject("properties");
{
builder.startObject("id");
{
builder.field("type", "long");
}
builder.endObject();
builder.startObject("username");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("age");
{
builder.field("type", "integer");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
CreateIndexRequest createIndexRequest = new CreateIndexRequest("test_limit_index");
createIndexRequest.source(builder);
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
restHighLevelClient.close();
}
/**
* 写入doc
*
* @throws Exception
* 异常
*/
public static void writeDoc() throws Exception {
// 获取连接
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
for (int i = 0; i < 20000; i++) {
IndexRequest indexRequest = new IndexRequest();
indexRequest.index("test_limit_index").id(String.valueOf(i));
UserInfo userInfo = new UserInfo();
userInfo.setId(i);
userInfo.setUsername("黄xx" + i);
userInfo.setAge(18);
// 再将我们的json数据放进request中作为数据源
indexRequest.source(JSON.toJSONString(userInfo), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
// 查看结果
DocWriteResponse.Result result = response.getResult();
if (Result.CREATED.equals(result)) {
System.out.println("创建文档" + i + "成功");
}
}
// 关闭客户端
restHighLevelClient.close();
}
/**
* 批量写入
*
* @throws Exception
*/
public static void batchWriteDoc() throws Exception {
// 获取连接
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
// 创建批量请求
BulkRequest request = new BulkRequest();
// 添加20000个文档到批量请求中
for (int i = 10001; i <= 20000; i++) {
IndexRequest indexRequest = new IndexRequest("test_limit_index");
indexRequest.id(String.valueOf(i));
UserInfo userInfo = new UserInfo();
userInfo.setId(i);
userInfo.setUsername("黄xx" + i);
userInfo.setAge(18);
indexRequest.source(JSON.toJSONString(userInfo), XContentType.JSON);
request.add(indexRequest);
}
// 执行批量请求
BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
// 处理批量响应
for (BulkItemResponse bulkItemResponse : response) {
if (bulkItemResponse.isFailed()) {
BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
System.out.println(bulkItemResponse.getId() + ", Reason: " + failure.getMessage());
}
}
// 关闭客户端
restHighLevelClient.close();
}
/**
* 删除文档
*
* @throws Exception
* 异常
*/
public static void deleteDoc() throws Exception {
// 获取连接
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
String indexName = "test_limit_index";
String documentId = "3";
DeleteRequest request = new DeleteRequest(indexName).id(documentId);
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response);
restHighLevelClient.close();
}
只能查询前10000条,如果要解除限制,需要设置index.max_result_window的值
/**
* 使用form+size查询
*
* @throws Exception
*/
public static void searchFormSize() throws Exception {
// 获取连接
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
// 创建searchRequest
SearchRequest searchRequest = new SearchRequest("test_limit_index");
// 指定查询条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 必须加上track_total_hits,不然就只显示10000
sourceBuilder.trackTotalHits(true);
// 起始行
sourceBuilder.from(10000);
// 每页多少条数据
sourceBuilder.size(10);
// 设置唯一排序值定位
sourceBuilder.sort("id", SortOrder.DESC);
// 将sourceBuilder对象添加到搜索请求中
searchRequest.source(sourceBuilder);
// 发送请求
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
List
可以查询所有记录数,默认是系统的uuid,不重复,但是需要指定唯一id,再标记从这个id查询后面的数据,如果有重复的自定义主键,则数量可能会变少。
/**
* 继续查询 ,searchAfter方式
*
* @throws Exception
* 异常
*/
public static void searchAfter() throws Exception {
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
SearchRequest searchRequest = new SearchRequest("test_limit_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
// 设置每页查询的数据个数
sourceBuilder.size(1000);
// 设置唯一排序值定位
sourceBuilder.sort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
// 将sourceBuilder对象添加到搜索请求中
searchRequest.source(sourceBuilder);
// 发送请求
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = searchResponse.getHits().getHits();
List
每次获取scrollId往下查询
/**
* 滚动查询
*
* @throws Exception
* 异常
*/
public static void searchScroll() throws Exception {
RestHighLevelClient restHighLevelClient = getRestHighLevelClient();
SearchRequest searchRequest = new SearchRequest("test_limit_index");
searchRequest.scroll(TimeValue.timeValueMinutes(1L));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(1000);
searchSourceBuilder.sort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
searchRequest.source(searchSourceBuilder);
// 获取返回结果scrollId,source
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
List