这是因为es默认的查询最大值是10000,查询的条数超出这个值就会报错,通过查资料得知,影响查询的是这个配置 max_result_window,因此我们需要修改这个默认值。
这里我采用的是HttpPut请求的方式修改es的配置,代码如下
/**
* 原生字符串发送put请求
*
* @param url
* @param jsonStr
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String doPut(String url, String jsonStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpPut.setConfig(requestConfig);
httpPut.setHeader("Content-type", "application/json");
httpPut.setHeader("DataEncoding", "UTF-8");
CloseableHttpResponse httpResponse = null;
try {
httpPut.setEntity(new StringEntity(jsonStr));
httpResponse = httpClient.execute(httpPut);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
return result;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void main(String[] args) throws Exception{
net.sf.json.JSONObject jsonObject = new JSONObject();
jsonObject.put("index.max_result_window", 300000);//设定最大检索值
System.out.println(doPut("http://ip:9200/search/_settings", jsonObject.toString()));
}
返回值如下,则代表修改配置成功,再次查询es时,300000条的数据之内都不会报错
{"acknowledged":true}
网上搜索了一些方案, 都不能满足自己的需求,于是整理的一套这个方案。算是比较笨的方法,如果有更好地方案欢迎指正。