Elasticsearch使用中出现的错误

news/2024/7/7 19:03:34

Elasticsearch使用中出现的错误

1、分页查询异常

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

from + size must be less than or equal to: [10000]

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

curl -XPUT http://127.0.0.1:9200/customer/_settings -d '{ "index" : { "max_result_window" : 500000}}'

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

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

是上面的这段代码:

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 类型

// 创建时间
@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

@Field(type = FieldType.Keyword, fielddata=true)
private String categoryName;

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

PUT shop_info/_mapping/docs
{
  "properties": {
    "categoryName": {
      "type": "text",
      "fielddata": "true"
    }
  }
}

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

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 类型

// 获取分组数据
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启动出现初始化密钥库问题

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这个文件,所以输入下面的命令:

./bin/elasticsearch-keystore create

http://lihuaxi.xjx100.cn/news/1427635.html

相关文章

07 |「异步任务」

前言 实践是最好的学习方式,技术也如此。 文章目录 前言一、进程与线程1、进程2、线程 二、实现 一、进程与线程 1、进程 进程(Process)是操作系统分配资源的基本单位,它是一个执行中的程序实例;每个进程都有自己独立的内存空间,不同进程的内存是相互独…

vue 时间只能选择当天和未来

<el-date-picker class"elpicker" clearable size"small" v-model"form.openTime" type"datetime"value-format"yyyy-MM-dd HH:mm:ss" :picker-options"pickerOptions" placeholder"选择开始时间"&g…

@RequestBody注解,自定义注解

public Result update(RequestBody EmployeeDTO employeeDTO){&#xff0c;为什么要加RequestBody &#xff0c;什么时候加什么时候不加&#xff1f; 在这段代码中&#xff0c;RequestBody EmployeeDTO employeeDTO 表示将请求体中的数据解析为 EmployeeDTO 对象&#xff0c;并…

ubuntu磁盘管理

show partition information 挂载设备在这 显示文件系统信息 build file system mkfs -t ext4 /dev/nvme0n1p4命令作用&#xff1a;将/dev/nvme0n1p4 格式化为 ext4 建立交换分区 mkswap -c -v1 /dev/nvme0n1p4 102400-c&#xff1a;check -v1&#xff1a;新版交换分区 -v0&…

Vscode无法写入文件 NoPermissions (FileSystemError): Error: EACCES: permission

用Vscode想要新建一个index.html的时候遇到了下图问题&#xff0c;说没有权限无法写入文件。 没有权限&#xff0c;咱们给他加上权限哈哈哈&#xff0c;博主是Mac电脑&#xff0c;如下操作&#xff1a; 1.找到你项目的根目录&#xff0c;右键&#xff0c;点击“显示简介”。 …

何时使用MongoDB而不是MySql

什么是 MySQL 和 MongoDB MySQL 和 MongoDB 是两个可用于存储和管理数据的数据库管理系统。MySQL 是一个关系数据库系统&#xff0c;以结构化表格格式存储数据。相比之下&#xff0c;MongoDB 以更灵活的格式将数据存储为 JSON 文档。两者都提供性能和可扩展性&#xff0c;但它…

基于MATLAB小波变换的信号突变点检测

之前在不经意间也有接触过求突变点的问题。在我看来&#xff0c;与其说是求突变点&#xff0c;不如说是我们常常玩的"找不同"。给你两幅图像&#xff0c;让你找出两个图像中不同的地方&#xff0c;我认为这其实也是找突变点在生活中的应用之一吧。回到找突变点位置上…

文件夹下图片批量重命名

目录 1.批处理脚本 2.赋予脚本执行权限&#xff1a; 3.运行脚本&#xff1a; 您可以使用以下脚本来批量重命名Mac上的图片文件&#xff1a; 1.批处理脚本 #!/bin/bash# 设置文件夹路径和要替换的字符串及其替换内容 folder_path"/您的/文件夹/路径" old_string&…