关于es的一些默认限制解除

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()));
	}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L-960

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值