異常信息
核心提示在Please use a keyword field instead. Alternatively, set fielddata=true on [dataTimestamp] in order to load field data by uninverting the inverted index.
待排序字段dataTimestamp
沒有為排序優(yōu)化,所以無法排序,需要配置FieldType.Keyword
或fielddata = true
,可是代碼中都配置了為什么還提示呢,往下看……
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 [dataTimestamp] 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 [dataTimestamp] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]]
環(huán)境
spring-data-elasticsearch 4.1.3
ElasticSearch 7.9.3
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.1.3</version>
</dependency>
spring-data-elasticsearch這塊,對ElasticSearch 7.16之前的都差不多,相似版本可以嘗試
代碼配置
ElasticSearch實體
如下配置,異常提示需要將排序字段設(shè)置為Keyword類型(推薦),或者將fielddata設(shè)置為true(不推薦),可是我都設(shè)置了,還是提示上面的注意:這里使用Spring ElasticSearch注解是否生效一定要注意,如果注解未生效就會導(dǎo)致聲明的索引、字段信息都是無效的,所以配置了也沒有,下面說如何檢查
@Data
@Document(indexName = "test-index")
public class ElasticSearchEntity {
/**
* ElasticSearch Long型精度會丟失
*/
@Id
@Field(type = FieldType.Keyword)
private String dataId;
// 配置二選一即可,推薦FieldType.Keyword,使用fielddata = true相對更占資源
@Field(type = FieldType.Keyword, fielddata = true)
private String dataTimestamp;
}
檢查ElasticSearch實體映射結(jié)果
項目啟動后會自動在ElasticSearch創(chuàng)建索引,查詢索引信息,有mapping才說明注解配置生效了,因為注解聲明的字段信息在ElasticSearch對應(yīng)的就是mapping
如果沒有請檢查,Java日志是否有如下類似的創(chuàng)建失敗警告
,按照警告提示修改實體注解配置,嘗試至無警告創(chuàng)建,再到ElasticSearch查詢索引是否正確加載mapping
[o.s.d.e.r.s.SimpleElasticsearchRepository,<init> : 96] - Cannot create index: Elasticsearch exception [type=mapper_parsing_exception, reason=Mapping definition for
ElasticSearch數(shù)據(jù)操作接口
public interface IndexRepository extends ElasticsearchRepository<ElasticSearchEntity, String> {
}
查詢調(diào)用(高級查詢接口自定義)
這里先列出來兩種寫法,因為mapping字段聲明未加載成功可導(dǎo)致使用時的問題,推薦第一種,所以上面如果有問題的檢查一下
Criteria criteria = new Criteria("title").contains(keyword);
// 1、沒有加載mapping默認(rèn)識別string類型,如果keyword聲明加載成功這么寫就可以了(推薦這種,所以上面如果有問題的檢查一下)
Sort sort = Sort.by("_score", "dataTimestamp").descending();
// 2、沒有加載mapping默認(rèn)為string類型創(chuàng)建keyword,Keyword類型才有索引可以排序,想使用Keyword索引要加.keyword使用,否則還是原字段
Sort sort = Sort.by("_score", "dataTimestamp.keyword").descending();
Query query = new CriteriaQuery(criteria)
.setPageable(
PageRequest.of(pageNum, pageSize, sort)
);
SearchHits<ElasticSearchEntity> searchHits = elasticsearchRestTemplate.search(query, ElasticSearchEntity.class, IndexCoordinates.of("index-*"));
發(fā)現(xiàn)問題
各種查文檔,都沒有對spring-data-elasticsearch的這個異常處理方案,所以只能自己查了
既然是ElasticSearch的錯誤就去原生查詢,查詢時發(fā)現(xiàn),ElasticSearch的dataTimestamp的字段有兩個,因為沒有加載mapping默認(rèn)識別string類型,默認(rèn)還會為string類型創(chuàng)建keyword,所以這時候有兩種類型,使用的時候就要注意調(diào)用區(qū)分,如果keyword聲明成功,就只有一個keyword類型,直接使用原字段就可以文章來源:http://www.zghlxwxcb.cn/news/detail-423566.html
原因
查詢ElasticSearch文檔對keyword的介紹,大致說一下我的理解,有問題望指正
Keyword是相當(dāng)于對Text的一個補(bǔ)充,設(shè)置Keyword類型或當(dāng)Text設(shè)置分詞器、索引的時候,就會創(chuàng)建一個.keyword字段,用于更快速的查詢,所以文本字段需要使用索引時,不可以直接使用原字段,要利用.keyword字段操作就可以了文章來源地址http://www.zghlxwxcb.cn/news/detail-423566.html
到了這里,關(guān)于spring-data-elasticsearch使用Sort排序時Please use a keyword field instead. ……異常解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!