国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

spring-data-elasticsearch使用Sort排序時Please use a keyword field instead. ……異常解決

這篇具有很好參考價值的文章主要介紹了spring-data-elasticsearch使用Sort排序時Please use a keyword field instead. ……異常解決。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

異常信息

核心提示在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.Keywordfielddata = 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
spring-data-elasticsearch使用Sort排序時Please use a keyword field instead. ……異常解決
如果沒有請檢查,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類型,直接使用原字段就可以
spring-data-elasticsearch使用Sort排序時Please use a keyword field instead. ……異常解決

原因

查詢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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Elasticsearch】Elasticsearch中使用_id排序?qū)е?data too large 問題

    【Elasticsearch】Elasticsearch中使用_id排序?qū)е?data too large 問題

    Elasticsearch 一個彈性伸縮的搜索數(shù)據(jù)庫,后文簡稱 :ES ,最近有一個ES 服務(wù)查詢數(shù)據(jù)時候出現(xiàn)了數(shù)據(jù) Hits 結(jié)果多次查詢不一致的問題,而且這塊代碼已經(jīng)很長時間沒有修改,一直穩(wěn)定運(yùn)行了很長時間,用戶翻譯查詢列表數(shù)據(jù)的時候又是出現(xiàn)1條,有時候出現(xiàn)2條或者3條。(再加上

    2024年02月03日
    瀏覽(30)
  • ElasticSearch 實戰(zhàn):查詢Sort(查詢排序)

    在Elasticsearch中,查詢排序(Sort)功能允許用戶控制搜索結(jié)果的返回順序。這有助于根據(jù)特定字段的值對匹配文檔進(jìn)行升序(asc)或降序(desc)排列。以下是如何在實戰(zhàn)中使用Elasticsearch查詢排序的示例: 一、基本排序 **1. 在URL參數(shù)中指定排序 : 此請求將按照 title 字段的值

    2024年04月16日
    瀏覽(18)
  • Spring Boot中使用Spring Data Elasticsearch訪問Elasticsearch

    Spring Boot中使用Spring Data Elasticsearch訪問Elasticsearch

    Elasticsearch是一個分布式的全文搜索和分析引擎,它可以將海量數(shù)據(jù)進(jìn)行快速的查詢和聚合。Spring Data Elasticsearch是Spring Data家族中的一個成員,它提供了與Elasticsearch的集成,可以方便地使用Spring框架來訪問Elasticsearch。 在本文中,我們將會介紹如何在Spring Boot中使用Spring Data

    2024年02月08日
    瀏覽(21)
  • Spring Data Elasticsearch--使用/教程/實例

    Spring Data Elasticsearch--使用/教程/實例

    原文網(wǎng)址:Spring Data Elasticsearch--使用/教程/實例_IT利刃出鞘的博客-CSDN博客 技術(shù)星球 歡迎來到IT技術(shù)星球,網(wǎng)站是:learn.skyofit.com(或者百度直接搜:自學(xué)精靈)。內(nèi)容有: Java真實面試題 、 Java設(shè)計模式實戰(zhàn) 、Shiro項目實戰(zhàn)、 Idea和Navicat的“魔法” 教程、 SpringBoot進(jìn)階 、架構(gòu)

    2023年04月09日
    瀏覽(45)
  • Spring Data Elasticsearch 的簡單使用

    目錄 一、簡介 二、配置 三、映射 四、?常用方法 五、操作(重點(diǎn)) 1、對索引表的操作 2、對文檔的操作(重點(diǎn)) (1)、添加文檔 ?(2)、刪除文檔 (3)、查詢文檔(重點(diǎn)) 查詢?nèi)课臋n?(兩種方式) matchQuery根據(jù)拆分進(jìn)行全局搜索 matchPhraseQuery短語搜索--完整搜

    2024年02月12日
    瀏覽(17)
  • Spring Data Elasticsearch配置及使用

    以POJO為中心模型用于與Elastichsearch文檔交互,并輕松編寫存儲庫樣式的數(shù)據(jù)訪問層框架 我們學(xué)習(xí)的是底層封裝了Rest High Level的ElasticsearchRestTemplate模板類型。需要使用Java API Client(Transport),則應(yīng)用ElasticsearchTemplate模板類型即可。兩種類型中的方法API幾乎完全一樣,學(xué)會了一

    2024年02月11日
    瀏覽(28)
  • ElasticSearch系列 - SpringBoot整合ES:實現(xiàn)搜索結(jié)果排序 sort

    00. 數(shù)據(jù)準(zhǔn)備 01. Elasticsearch 默認(rèn)的排序方式是什么? ElasticSearch 默認(rèn)的排序方式是相關(guān)性排序。相關(guān)性排序是根據(jù)查詢條件與文檔的匹配程度來計算每個文檔的相關(guān)性得分,然后按照得分從高到低進(jìn)行排序。相關(guān)性排序是 ElasticSearch 中最常用的排序方式,因為它可以根據(jù)查詢

    2024年02月02日
    瀏覽(23)
  • 解決使用@Field注解配置分詞器失效問題(Spring Data Elasticsearch)

    解決使用@Field注解配置分詞器失效問題(Spring Data Elasticsearch)

    問題復(fù)現(xiàn):插入數(shù)據(jù)時,實體類配置的@Field注解沒有生效 實體類: 查看索引庫,發(fā)現(xiàn)它使用動態(tài)映射,并沒有使用靜態(tài)映射: 解決方案:在插入數(shù)據(jù)時,提前創(chuàng)建索引庫和映射。

    2024年02月16日
    瀏覽(21)
  • 【Elasticsearch】spring-boot-starter-data-elasticsearch的使用以及Elasticsearch集群的連接

    【Elasticsearch】spring-boot-starter-data-elasticsearch的使用以及Elasticsearch集群的連接

    更多有關(guān)博主寫的往期Elasticsearch文章 標(biāo)題 地址 【ElasticSearch 集群】Linux安裝ElasticSearch集群(圖文解說詳細(xì)版) https://masiyi.blog.csdn.net/article/details/131109454 基于SpringBoot+ElasticSearch 的Java底層框架的實現(xiàn) https://masiyi.blog.csdn.net/article/details/121534307 ElasticSearch對標(biāo)Mysql,誰能拔得頭籌

    2024年02月11日
    瀏覽(26)
  • spring data elasticsearch使用7.x客戶端兼容es 8.x和使用ssl構(gòu)建RestHighLevelClient

    es在7.x中默認(rèn)加入elastic security組件所以java client需要使用ssl連接es server. es 8.x 中廢棄了 RestHighLevelClient ,使用新版的 java api client ,但是spring data elasticsearch還未更新到該版本.所以需要兼容es 8.x 如下是RestHighLevelClient構(gòu)建方法: spring data elasticsearch客戶端依賴(基于spring boot2.7使用最新

    2024年02月13日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包