更改前:
@ApiOperation("分頁(yè)排序")
@GetMapping("pageAndSort/{page}/{size}")
public Page<Hero> pageAndSort(@PathVariable Integer page,@PathVariable Integer size){
//構(gòu)建本地查詢(xún)對(duì)象
NativeSearchQueryBuilder query = new NativeSearchQueryBuilder();
//
PageRequest pageRequest = PageRequest.of(page, size);
query.withPageable(pageRequest);
query.withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC));
SearchHits<Hero> search = elasticTemplate.search(query.build(), Hero.class);
List<Hero> blogs = new ArrayList<>();
for (SearchHit<Hero> hero : search) {
blogs.add(hero.getContent());
}
PageImpl<Hero> heroes = new PageImpl<>(blogs, pageRequest, search.getTotalHits());
return heroes;
}
錯(cuò)誤提示信息: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 [createTime] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]
翻譯過(guò)來(lái)就是:對(duì)于需要每個(gè)文檔字段數(shù)據(jù)(如聚合和排序)的操作,文本字段沒(méi)有進(jìn)行優(yōu)化,因此這些操作在默認(rèn)情況下是禁用的。請(qǐng)使用關(guān)鍵字字段代替。或者,在[createTime]上設(shè)置fielddata=true,以便通過(guò)反求倒排索引來(lái)加載字段數(shù)據(jù)。注意,這可能會(huì)使用有效內(nèi)存。]
在es中,text類(lèi)型的字段使用一種叫做fielddata的查詢(xún)時(shí)內(nèi)存數(shù)據(jù)結(jié)構(gòu)。當(dāng)字段被排序,聚合或者通過(guò)腳本訪(fǎng)問(wèn)時(shí)這種數(shù)據(jù)結(jié)構(gòu)會(huì)被創(chuàng)建。它是通過(guò)從磁盤(pán)讀取每個(gè)段的整個(gè)反向索引來(lái)構(gòu)建的,然后存存儲(chǔ)在java的堆內(nèi)存中。
這里fileddata默認(rèn)是不開(kāi)啟的。Fielddata可能會(huì)消耗大量的堆空間
所以換一種方式解決:在需要的字段上添加關(guān)鍵字? keyword
更改后:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-506932.html
@ApiOperation("分頁(yè)排序")
@GetMapping("pageAndSort/{page}/{size}")
public Page<Hero> pageAndSort(@PathVariable Integer page,@PathVariable Integer size){
//構(gòu)建本地查詢(xún)對(duì)象
NativeSearchQueryBuilder query = new NativeSearchQueryBuilder();
//
PageRequest pageRequest = PageRequest.of(page, size);
query.withPageable(pageRequest);
query.withSort(SortBuilders.fieldSort("createTime.keyword").order(SortOrder.DESC));
SearchHits<Hero> search = elasticTemplate.search(query.build(), Hero.class);
List<Hero> blogs = new ArrayList<>();
for (SearchHit<Hero> hero : search) {
blogs.add(hero.getContent());
}
PageImpl<Hero> heroes = new PageImpl<>(blogs, pageRequest, search.getTotalHits());
return heroes;
}
? ? ? ? query.withSort(SortBuilders.fieldSort("createTime.keyword").order(SortOrder.DESC));文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-506932.html
到了這里,關(guān)于ES排序報(bào)錯(cuò):Elasticsearch exception [type=illegal_argument_exception, reason=Text的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!