//elasticsearch的一个索引里面有一个peronId的字段,现在需要遍历这个库拿到这个id列表集合,同时age要大于18,性别是男,java代码实现如下:
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class OptimizedElasticsearchScrollQuery {
public static List<String> getPersonIdsWithFilter() {
List<String> personIds = new ArrayList<>();
try (RestHighLevelClient client = new RestHighLevelClient.Builder().build()) {
SearchRequest searchRequest = new SearchRequest("your_index_name"); // 替换成你的索引名称
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gt(18));
boolQueryBuilder.must(QueryBuilders.matchQuery("gender", "male"));
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.size(100); // 每次滚动获取的文档数量
searchSourceBuilder.fetchSource(new String[]{"age", "gender", "personId"}, null); // 指定需要返回的字段
searchRequest.source(searchSourceBuilder);
// 设置scroll参数
searchRequest.scroll(TimeValue.timeValueMinutes(1));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
while (true) {
for (SearchHit hit : searchResponse.getHits().getHits()) {
String personId = hit.getSourceAsMap().get("personId").toString();
personIds.add(personId);
}
// 不需要在每次循环中更新scrollId
searchResponse = client.scroll(new SearchScrollRequest(scrollId)
.scroll(TimeValue.timeValueMinutes(1)), RequestOptions.DEFAULT);
if (searchResponse.getHits().getHits().length == 0) {
break; // 没有更多结果,退出循环
}
}
} catch (IOException e) {
e.printStackTrace();
}
return personIds;
}
public static void main(String[] args) {
List<String> personIds = getPersonIdsWithFilter();
System.out.println("PersonIds with age > 18 and gender = male:");
for (String personId : personIds) {
System.out.println(personId);
}
}
}
elasticsearch scroll search 使用示例
于 2024-01-28 10:58:18 首次发布
本文展示了如何使用Java编写Elasticsearch代码,通过构建布尔查询筛选出年龄大于18且性别为男的文档,获取personId字段的值。
摘要由CSDN通过智能技术生成