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 查询。文章来源:https://www.toymoban.com/news/detail-644960.html
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
这个文件,所以输入下面的命令:文章来源地址https://www.toymoban.com/news/detail-644960.html
./bin/elasticsearch-keystore create
到了这里,关于Elasticsearch使用中出现的错误的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!