一. 问题描述
项目中通过ElasticSearch实现全文检索功能,查询含有指定关键字的文档并将关键字高亮显示,搜索过程中出现报错index has exceeded [1000000] - maximum allowed to be analyzed for highlighting.
错误详细信息如下图所示:
二. 问题分析
通过报错描述可知,检索结果的长度已超过[100000],即允许分析以突出显示的最大值。这个最大值的限制可以通过更改[index.highlight.max_analysed offset]索引级别来设置。
官方文档中有明确解释,index.highlight.max_analyzed_offset配置的是分析高亮请求的最大字符数。此设置仅适用于在没有偏移或术语矢量的情况下索引的文本上请求高亮显示的情况。默认值为1000000。
原文如下:
原文地址:https://www.elastic.co/guide/en/elasticsearch/reference/7.15/index-modules.html#index-modules-settings
三. 解决方案
1.通过kibana配置
PUT index_name/_settings
{
"index" : {
"highlight.max_analyzed_offset" : 100000000
}
}
2.通过java配置
package com.dmp.framework.config;
import lombok.Setter;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Setter
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
public class ElasticSearchConfig {
private String host;
private Integer port;
private String indexName;
@Bean
public RestHighLevelClient restHighLevelClient() throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(this.host,this.port,"http")
).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setConnectTimeout(90000000)//25hours
.setSocketTimeout(90000000);
}
}).setHttpClientConfigCallback((httpAsyncClientBuilder -> {
httpAsyncClientBuilder.disableAuthCaching();//禁用身份验证缓存
//显式设置keepAliveStrategy
httpAsyncClientBuilder.setKeepAliveStrategy((httpResponse,httpContext) -> TimeUnit.MINUTES.toMillis(3));
//显式开启tcp keepalive
httpAsyncClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(true).build());
return httpAsyncClientBuilder;
}))
);
//索引高亮最大分析偏移量 index.highlight.max_analyzed_offset
UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexName);
updateSettingsRequest.settings(Settings.builder().put("index.highlight.max_analyzed_offset",100000000));
client.indices().putSettings(updateSettingsRequest, RequestOptions.DEFAULT);
return client;
}
}
另外,index.highlight.max_analysed offset值设置过大可能会影响性能,需根据实际需要和服务器具体条件酌情考虑。