gpt4 book ai didi

spring-boot - 使用Spring数据Elasticsearch注释查询并仅返回特定的嵌套字段

转载 作者:行者123 更新时间:2023-12-02 23:48:48 28 4
gpt4 key购买 nike

使用Spring数据Elasticsearch注释查询并仅返回特定的嵌套字段

版本:

springboot:2.1.7。发布

Spring 数据 flex 搜索:2.1.7.RELEASE

elasticsearch:6.5.4

文件:

@Data
@Document(indexName = "test_book", type = "test_book")
public class Book {
@Id
private String id;

private String name;

private LocalDateTime time;
/**
*
*/
private Publishing publishing;

/**
*
*/
private Author author;
}

仓库:
public interface BookRepository extends ElasticsearchRepository<Book,String> {

@Query("{" +
"\"_source\": {" +
" \"includes\": " +
" [ \"name\"]" +
"}," +
"\"bool\" : {" +
" \"must\" : [{" +
" \"term\" : {" +
" \"id.keyword\" : \"?0\"" +
" }" +
" }]" +
" }" +
"}")
Book queryBookNameById(String id);
}

我只想获取 名称的数据,可以相对节省内存资源。但是我有一个错误,我不能这样使用吗?还是只能使用elasticsearchTemplate?
ParsingException[no [query] registered for [_source]
]
at org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder(AbstractQueryBuilder.java:332)
at org.elasticsearch.index.query.WrapperQueryBuilder.doRewrite(WrapperQueryBuilder.java:165)
at org.elasticsearch.index.query.AbstractQueryBuilder.rewrite(AbstractQueryBuilder.java:279)
at org.elasticsearch.search.builder.SearchSourceBuilder.rewrite(SearchSourceBuilder.java:921)
at org.elasticsearch.search.builder.SearchSourceBuilder.rewrite(SearchSourceBuilder.java:80)
at org.elasticsearch.index.query.Rewriteable.rewriteAndFetch(Rewriteable.java:97)
at org.elasticsearch.index.query.Rewriteable.rewriteAndFetch(Rewriteable.java:87)
at org.elasticsearch.action.search.TransportSearchAction.doExecute(TransportSearchAction.java:215)
at org.elasticsearch.action.search.TransportSearchAction.doExecute(TransportSearchAction.java:68)
at org.elasticsearch.action.support.TransportAction$RequestFilterChain.proceed(TransportAction.java:167)
at org.elasticsearch.xpack.security.action.filter.SecurityActionFilter.apply(SecurityActionFilter.java:126)
......

最佳答案

当前,此功能不适用于存储库的@Query批注。 Spring Data Elasticsearch会将注释的值包装为查询值,因此将_source部分包括在查询中。

我们将需要在批注中添加其他参数(includesexcludes),以便能够构建正确的查询并使其正常工作。我将为此创建一个Jira问题来跟踪此功能请求。

Jira issue

关于spring-boot - 使用Spring数据Elasticsearch注释查询并仅返回特定的嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com