elasticsearch scroll search 使用示例

本文展示了如何使用Java编写Elasticsearch代码,通过构建布尔查询筛选出年龄大于18且性别为男的文档,获取personId字段的值。
摘要由CSDN通过智能技术生成
//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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值