简体   繁体   English

如何在 Spring 数据中构建 Query Elasticsearch 5.0.0 API

[英]How to build a Query in Spring Data Elasticsearch 5.0.0 API

I'm migrating my code to Spring Data Elasticsearch 5.0.0 API and I'm confused on what is the cleanest way of constructig the query for the ElasticsearchTemplate.我正在将我的代码迁移到 Spring Data Elasticsearch 5.0.0 API 我对构造 ElasticsearchTemplate 查询的最简洁方法感到困惑。

Since the BoolQuery doesn't implement Query interface we can't simply build it so for now I'm doing this:由于 BoolQuery 没有实现 Query 接口,我们不能简单地构建它,所以现在我正在这样做:

BoolQuery boolQuery = QueryBuilders.bool().build();
    
var nativeQuery = new NativeQueryBuilder()
            .withQuery(query.build()._toQuery())
            .build();
    
elasticsearchTemplate.search(nativeQuery, Object.class);

I'm confused about '_toQuery()' the underscore indicates it shouldn't be used outside of library even though it's public, so is there any better way of doing this?我对 '_toQuery()' 感到困惑,下划线表示它不应该在库外使用,即使它是公开的,那么有没有更好的方法来做到这一点?

_toQuery() comes from this class: _toQuery()来自这个 class:

package co.elastic.clients.elasticsearch._types.query_dsl;

/**
 * Base interface for {@link Query} variants.
 */
public interface QueryVariant {

    Query.Kind _queryKind();

    default Query _toQuery() {
        return new Query(this);
    }

}

So it's part of the Elasticsearch API. And fif you don not want to use it, just do the new Query(query.build()) by yourself.所以它是 Elasticsearch API 的一部分。如果你不想使用它,只需自己执行new Query(query.build())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM