Elasticsearch使用中出现的错误

Elasticsearch使用中出现的错误

1、分页查询异常

在分页的过程中出现了一个问题是当查询的数据超过10000条的时候报了异常:

tex 复制代码
from + size must be less than or equal to: [10000]

这个问题最快捷的解决方式是增大窗口大小:

shell 复制代码
curl -XPUT http://127.0.0.1:9200/customer/_settings -d '{ "index" : { "max_result_window" : 500000}}'

但是对应增大窗口大小,会牺牲更多的服务器的内存、CPU资源,在我们这边的使用场景下,这样做是划不来的,

因为我们的目的是做目标数据的搜索,而不是大规模的遍历,所以我们这边会直接放弃超过这个数量的查询,也就

是上面的这段代码:

java 复制代码
if (from > 10000) {
	System.out.println("测试:超过10000条直接中断");
	break;
}

2、SpringBoot Elasticsearch 7.x 聚合查询遇到的问题

2.1 时间的问题

报错:

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO

resolved to 2019-04-30T16:00 of type java.time.format.Parsed

解决:

POJO 类中 Date 类型转化为 LocalDate 类型

java 复制代码
// 创建时间
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
private LocalDate createTime;

// 更新时间
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
private LocalDate updateTime;

先试第一个再试第二个

java.time.LocalDate

org.joda.time.LocalDate

2.2 无法进行聚类的问题

报错:

org.springframework.data.elasticsearch.UncategorizedElasticsearchException:

Elasticsearch exception [type=search_phase_execution_exception, reason=all shards

failed]; nested exception is ElasticsearchStatusException[Elasticsearch exception

[type=search_phase_execution_exception, reason=all shards failed]]; nested:

ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception,

reason=Text fields are not optimised for operations that require per-document field

data like aggregations and sorting, so these operations are disabled by default.

Please use a keyword field instead. Alternatively, set fielddata=true on

[categoryName] in order to load field data by uninverting the inverted index. Note

that this can use significant memory.]]; nested: ElasticsearchException[Elasticsearch

exception [type=illegal_argument_exception, reason=Text fields are not optimised for

operations that require per-document field data like aggregations and sorting, so

these operations are disabled by default. Please use a keyword field instead.

Alternatively, set fielddata=true on [categoryName] in order to load field data by

uninverting the inverted index. Note that this can use significant memory.]];

解决:

报错中有这样一句:set fielddata=true on [categoryName]

如果聚类结果是需要分词的:

在 POJO 类中添加:fielddata=true

java 复制代码
@Field(type = FieldType.Keyword, fielddata=true)
private String categoryName;

设置了之后发现不生效,还需要进行如下操作:

shell 复制代码
PUT shop_info/_mapping/docs
{
  "properties": {
    "categoryName": {
      "type": "text",
      "fielddata": "true"
    }
  }
}

如果聚类结果是不需要分词的,可以这样处理:

java 复制代码
builder.addAggregation(AggregationBuilders.terms("skuCategory").field("categoryName.keyword"))

2.3 类型转换的问题

报错:

java.lang.ClassCastException: class

org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms cannot be cast to

class org.elasticsearch.search.aggregations.bucket.terms.StringTerms

(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms and

org.elasticsearch.search.aggregations.bucket.terms.StringTerms are in unnamed module

of loader 'app')

原因:

报错中是无法转化成 StringTerms 类型

在之前的版本中是可以的,但在 7 版本以上就不好使了

解决:

需要将 StringTerms 类型改为 Terms 类型

java 复制代码
// 获取分组数据
Terms terms = Objects.requireNonNull(searchSkuInfo.getAggregations()).get(termsId);

2.4 QueryBuilders.termQuery() 查询无数据的问题

报错:

QueryBuilders.termQuery() 查询没有数据

原因:

原因第一个可能是中文的缘故,这时候可以用英文试一试,需中文查询还需要解决。

原因第二个可能是含义没有弄清楚,QueryBuilders.termQuery() 精准匹配,不进行分词,也不是模糊匹

配, 是完全匹配才可以好使,而且只支持单个添加,多个条件需要用 QueryBuilders.termsQuery()

原因第三个可能是 Java Rest Client 客户端自带的 bug。

解决:

方法一:可以将 QueryBuilders.termQuery(name, value) 中的 name 加上 .keyword

方法二:可以将 QueryBuilders.termQuery() 直接用 QueryBuilders.matchPhraseQuery() 代替,

QueryBuilders.matchPhraseQuery() 也是进行精准匹配,match 查询是高级查询, 底层使用了 term 查

询。

3、安装中出现的问题

3.1 Elasticsearch7.1.0启动出现初始化密钥库问题

tex 复制代码
Exception in thread "main" org.elasticsearch.bootstrap.BootstrapException: java.nio.file.AccessDeniedException: /usr/local/elasticsearch/elasticsearch-6.6.0/config/elasticsearch.keystore
Likely root cause: java.nio.file.AccessDeniedException: /usr/local/elasticsearch/elasticsearch-7.1.0/config/elasticsearch.keystore
 at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
 at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
 at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
 at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
 at java.nio.file.Files.newByteChannel(Files.java:361)
 at java.nio.file.Files.newByteChannel(Files.java:407)
 at org.apache.lucene.store.SimpleFSDirectory.openInput(SimpleFSDirectory.java:77)
 at org.elasticsearch.common.settings.KeyStoreWrapper.load(KeyStoreWrapper.java:206)
 at org.elasticsearch.bootstrap.Bootstrap.loadSecureSettings(Bootstrap.java:224)
 at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:289)
 at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159)
 at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150)
 at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
 at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124)
 at org.elasticsearch.cli.Command.main(Command.java:90)
 at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115)
 at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
Refer to the log for complete error details.

这个版本需要进行安全认证功能需要创建elasticsearch.keystore这个文件,所以输入下面的命令:

shell 复制代码
./bin/elasticsearch-keystore create
相关推荐
阿标在干嘛2 小时前
政策快报平台全文检索的3次升级:从ES到向量检索
大数据·elasticsearch·全文检索
阿里云大数据AI技术16 小时前
阿里云 Elasticsearch 9.4 Agent Builder 实战
人工智能·elasticsearch·agent
暖和_白开水1 天前
数据分析agent(九-2):es启动补充
大数据·elasticsearch·搜索引擎
海兰1 天前
基于 ES|QL 的 Kubernetes 监控工具箱:使用 ES|QL 进行 Kubernetes 监控,从内存压力到错误日志
大数据·elasticsearch·kubernetes
稚南城才子,乌衣巷风流1 天前
树哈希(Tree Hashing)原理与应用详解
算法·elasticsearch·哈希算法
暖和_白开水1 天前
数据分析agent(十):loguru日志输出优化 和es 启动流程
java·elasticsearch·数据分析
一次旅行2 天前
【效率秘籍】Shell 函数+别名组合拳,让 80% 的重复命令变 1 个字母
大数据·elasticsearch·搜索引擎
暖和_白开水2 天前
数据分析agent(九):es_client_manager.py 和分词器ik_max_word问题
elasticsearch·数据分析·c#
码农学院2 天前
Elasticsearch构建汽车零配件智能搜索与匹配系统
大数据·elasticsearch·汽车