web页面修改以及java代码实现elasticsearch中max_result_window设置

elasticsearch默认size10000以上就会报错导致(’Result window is too large, from + size must be less than or equal to:[10000] but was [10100]. See the scroll api for a more efficient way to requestlarge data sets. This limit can be set by changing the[index.max_result_window] index level parameter.’)

一下分享两种方法:

1.是在web页面进行

1)现将我们要修改的索引进行关闭操作
web页面修改以及java代码实现elasticsearch中max_result_window设置_第1张图片
2)在复合查询中设置max_result_window的最大索引值,以put方式提交
索引名称/_settings?preserve_existing=true
{
“max_result_window” : “2000000000”
}
web页面修改以及java代码实现elasticsearch中max_result_window设置_第2张图片
3)点击提交,在概览中将索引打开即可!

2.java代码中实现


public SearchResponse search(String logIndex, String logType, QueryBuilder query, 
                 List<AggregationBuilder> agg, int page, int size) {
        page = page > 0 ? page - 1 : page;
        TransportClient client = getClient();
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(logIndex.split(","))
                .setTypes(logType.split(","))
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .addSort("createTime", SortOrder.DESC);

        if (agg != null && !agg.isEmpty()) {
            for (int i = 0; i < agg.size(); i++) {
                searchRequestBuilder.addAggregation(agg.get(i));
            }
        }
        updateIndexs(client, logIndex, page, size);

        SearchResponse searchResponse = searchRequestBuilder
                .setQuery(query)
                .setFrom(page * size)
                .setSize(size)
                .get();
        return searchResponse;
    }

    //更新索引的max_result_window参数
    private boolean updateIndexs(TransportClient client, String indices, int from, int size) {
        int records = from * size + size;
        if (records <= 10000) return true;
        UpdateSettingsResponse indexResponse = client.admin().indices()
                .prepareUpdateSettings(indices)
                .setSettings(Settings.builder()
                        .put("index.max_result_window", records)
                        .build()
                ).get();
        return indexResponse.isAcknowledged();
    }

:当然这种设置max_result_window的方式不是非常推荐,每次查询超过10000万条记录的时候,都会去更新一次index。会导致原本就偏慢的from+size查询更加雪上加霜了。还是推荐使用游标的方式来实现

你可能感兴趣的:(es)