ElasticSearch查询报错index has exceeded [1000000] - maximum allowed to be analyzed for highlighting 解决方案

一. 问题描述


        项目中通过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值设置过大可能会影响性能,需根据实际需要和服务器具体条件酌情考虑。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值