1 java Api中只能显示10条检索出来的数据
这是es高级客户端的默认配置,通过size修改即可
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// todo 这里写你的查询条件
...
builder.query(boolQuery).size(9999);
2 es检索到10000条以上数据就会抛错
错误日志:
Result window is too large, from + size must be less than or equal to:[10000] but was [10500]. 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
原因:这是es官方默认限制索引查询最多只能查询10000条数据
解决:
2.1 kibana方案
第一种办法.在kibana中执行,修改索引最大查询数的限制
修改全部索引:
put _all/_settings
{
“index.max_result_window”:200000
}
修改单个索引
PUT /us_tm_info_v1/_settings
{
“index.max_result_window”: “20000”
}
2.2 修改es配置文件
第二种方式
在config/elasticsearch.yml文件中的最后加上
index.max_result_window: 100000000
2.3 linux无kibana又不能修改es配置的解决方案(推荐)
curl -H "Content-Type: application/json" -XPUT http://ip:port/_settings -d '{ "index" : { "max_result_window" : 100000000}}'
如果带密码
curl -H "Content-Type: application/json" -u elastic:'password' -XPUT http://192.168.111.11:9200/_settings -d '{ "index" : { "max_result_window" : 10000000}}'
如果java调用es client还需要加
searchSourceBuilder.trackTotalHits(true);
2.4 创建索引时设置
"settings":{
"index":{
"max_result_window":1000000
}
}
3 在java中发送请求
偷个懒,不自己写了
借鉴:https://blog.csdn.net/yanzi920403/article/details/120529122
/**
* 原生字符串发送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()));
}