elasticsearch - Spring data elasticsearch在索引为空时返回NPE
问题描述
我使用 spring 存储库在我的 elasticsearch 中搜索数据
@Repository
public interface EbookReviewRepository extends ElasticsearchRepository<EbookReview, Long> {
/**
* Find all ebooks reviews by ebookkId
* @param ebookId
* @param page
* @return
*/
Page<EbookReview> findByEbookIdOrderByCommentDateDesc(Long apkId, Pageable pageable);
}
在我的服务中,我这样使用它:
public ResponseList findReviewsByEbookId(Long ebookId, int page, int size, Optional appUserId) { logger.info("通过id查找所有电子书用户评论{} ", ebookId); 列表 rewiewsDtoResult = new ArrayList<>();
Page<EbookReview> ebookReviewPage = ebookReviewRepository.findByEbookIdOrderByCommentDateDesc(ebookId, PageRequest.of(page, size));
// We add app user name to the review
for(EbookReview review : ebookReviewPage) {
EbookReviewDto ebookReviewDto = createCompleteEbookReviewDto(review, appUserId);
if(ebookReviewDto.getAppUserId() != null) {
rewiewsDtoResult.add(ebookReviewDto);
}
}
// Prepare what we have to return
return new ResponseList(rewiewsDtoResult, ebookReviewPage.hasNext(), page);
}
但在findByEbookIdOrderByCommentDateDesc
我得到一个错误。
Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
在日志中,发出的请求是:
SearchRequest{searchType=DFS_QUERY_THEN_FETCH,indices=[ebookreview],indicesOptions=IndicesOptions[ignore_unavailable=false,allow_no_indices=true,expand_wildcards_open=true,expand_wildcards_closed=false,allow_aliases_to_multiple_indices=true,forbid_closed_indices=true,ignore_aliases=false,ignore_throttled=true],类型=[ebookreview],routing='null',preference='null',requestCache=null,scroll=null,maxConcurrentShardRequests=0,batchedReduceSize=512,preFilterShardSize=128,allowPartialSearchResults=null,localClusterAlias=null,getOrCreateAbsoluteStartMillis=-1, source={"from":0,"size":10,"query":{"bool":{"must":[{"query_string":{"query":"53","fields":[" ebookId^1.0"],"type":"best_fields","default_operator":"and","max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false ,"auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true,"sort":[{"commentDate ":{"order":"desc"}}]}}auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true,"sort":[{"commentDate": {"order":"desc"}}]}}auto_generate_synonyms_phrase_query":true,"fuzzy_transpositions":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"version":true,"sort":[{"commentDate": {"order":"desc"}}]}}
当我在索引中创建评论时,该方法效果很好。我真的不知道为什么它不起作用。如果索引为空,它应该返回一个数据为 0 的页面。
请问我该如何纠正?谢谢
编辑 我注意到当结果为空并且请求有排序部分时,它会失败。 https://discuss.elastic.co/t/searching-an-empty-index-with-a-sort-field-specified-should-not-fail/7862
他们"ignore_unmapped": true
在排序请求中添加如下:
{
"from": 0,
"size": "15",
"sort": {
"title": {
"order": "asc",
"ignore_unmapped": true
}
}
}
但是要ignore_unmapped
在 Spring 存储库请求中添加这个?
解决方案
推荐阅读
- javascript - Vue js:比较数组中的元素
- python - 我可以用命名参数作为变量在python中调用函数吗?
- r - R 字符串方法
- python - Pandas:如何在多级列选择和值设置中解释这种 .loc 行为
- python - 如何更新 miniconda 和 Spyder 4.2.1
- reactjs - 我的反应应用程序中的路由器引发错误
- php - PHPUnit 总是在每个测试方法中运行 setUpDatabase
- ios - 无法在 Xcode 中分发我的 iOS 版 Flutter 应用程序 - “包中的 ipa 包含无效字符”
- karate - 空手道 - JSON 值与变量的字符串连接
- android - Android MVVM - 在哪里绑定到 Mediaplayer 绑定服务?