简体   繁体   中英

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.

Since the BoolQuery doesn't implement Query interface we can't simply build it so for now I'm doing this:

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() comes from this 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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