首页 > 解决方案 > 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 存储库请求中添加这个?

标签: elasticsearchspring-data-elasticsearchelkspring-repositories

解决方案


推荐阅读