Skip to content

Rename QueryBuilders to avoid name clash wih Elasticsearch class. #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.data.elasticsearch.client.elc;

import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
import static org.springframework.util.StringUtils.*;

import co.elastic.clients.elasticsearch._types.FieldValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.client.elc;

import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.LatLonGeoLocation;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Operator;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.WildcardQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.WrapperQuery;
import co.elastic.clients.util.ObjectBuilder;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.function.Function;

import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Utility class simplifying the creation of some more complex queries and type.
*
* @author Peter-Josef Meisch
* @since 4.4
*/
public final class Queries {

private Queries() {}

public static IdsQuery idsQuery(List<String> ids) {

Assert.notNull(ids, "ids must not be null");

return IdsQuery.of(i -> i.values(ids));
}

public static Query idsQueryAsQuery(List<String> ids) {

Assert.notNull(ids, "ids must not be null");

Function<Query.Builder, ObjectBuilder<Query>> builder = b -> b.ids(idsQuery(ids));

return builder.apply(new Query.Builder()).build();
}

public static MatchQuery matchQuery(String fieldName, String query, @Nullable Operator operator,
@Nullable Float boost) {

Assert.notNull(fieldName, "fieldName must not be null");
Assert.notNull(query, "query must not be null");

return MatchQuery.of(mb -> mb.field(fieldName).query(FieldValue.of(query)).operator(operator).boost(boost));
}

public static Query matchQueryAsQuery(String fieldName, String query, @Nullable Operator operator,
@Nullable Float boost) {

Function<Query.Builder, ObjectBuilder<Query>> builder = b -> b.match(matchQuery(fieldName, query, operator, boost));

return builder.apply(new Query.Builder()).build();
}

public static MatchAllQuery matchAllQuery() {

return MatchAllQuery.of(b -> b);
}

public static Query matchAllQueryAsQuery() {

Function<Query.Builder, ObjectBuilder<Query>> builder = b -> b.matchAll(matchAllQuery());

return builder.apply(new Query.Builder()).build();
}

public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Float boost) {
return queryStringQuery(fieldName, query, null, null, boost);
}

public static QueryStringQuery queryStringQuery(String fieldName, String query, Operator defaultOperator,
@Nullable Float boost) {
return queryStringQuery(fieldName, query, null, defaultOperator, boost);
}

public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Boolean analyzeWildcard,
@Nullable Float boost) {
return queryStringQuery(fieldName, query, analyzeWildcard, null, boost);
}

public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Boolean analyzeWildcard,
@Nullable Operator defaultOperator, @Nullable Float boost) {

Assert.notNull(fieldName, "fieldName must not be null");
Assert.notNull(query, "query must not be null");

return QueryStringQuery.of(qs -> qs.fields(fieldName).query(query).analyzeWildcard(analyzeWildcard)
.defaultOperator(defaultOperator).boost(boost));
}

public static TermQuery termQuery(String fieldName, String value) {

Assert.notNull(fieldName, "fieldName must not be null");
Assert.notNull(value, "value must not be null");

return TermQuery.of(t -> t.field(fieldName).value(FieldValue.of(value)));
}

public static Query termQueryAsQuery(String fieldName, String value) {

Function<Query.Builder, ObjectBuilder<Query>> builder = q -> q.term(termQuery(fieldName, value));
return builder.apply(new Query.Builder()).build();
}

public static WildcardQuery wildcardQuery(String field, String value) {

Assert.notNull(field, "field must not be null");
Assert.notNull(value, "value must not be null");

return WildcardQuery.of(w -> w.field(field).wildcard(value));
}

public static Query wildcardQueryAsQuery(String field, String value) {
Function<Query.Builder, ObjectBuilder<Query>> builder = q -> q.wildcard(wildcardQuery(field, value));
return builder.apply(new Query.Builder()).build();
}

public static Query wrapperQueryAsQuery(String query) {

Function<Query.Builder, ObjectBuilder<Query>> builder = q -> q.wrapper(wrapperQuery(query));

return builder.apply(new Query.Builder()).build();
}

public static WrapperQuery wrapperQuery(String query) {

Assert.notNull(query, "query must not be null");

String encodedValue = Base64.getEncoder().encodeToString(query.getBytes(StandardCharsets.UTF_8));

return WrapperQuery.of(wq -> wq.query(encodedValue));
}

public static LatLonGeoLocation latLon(GeoPoint geoPoint) {

Assert.notNull(geoPoint, "geoPoint must not be null");

return latLon(geoPoint.getLat(), geoPoint.getLon());
}

public static LatLonGeoLocation latLon(double lat, double lon) {
return LatLonGeoLocation.of(_0 -> _0.lat(lat).lon(lon));
}

public static org.springframework.data.elasticsearch.core.query.Query getTermsAggsQuery(String aggsName,
String aggsField) {
return NativeQuery.builder() //
.withQuery(Queries.matchAllQueryAsQuery()) //
.withAggregation(aggsName, Aggregation.of(a -> a //
.terms(ta -> ta.field(aggsField)))) //
.withMaxResults(0) //
.build();
}

public static org.springframework.data.elasticsearch.core.query.Query queryWithIds(String... ids) {
return NativeQuery.builder().withIds(ids).build();
}

public static BaseQueryBuilder<?, ?> getBuilderWithMatchAllQuery() {
return NativeQuery.builder().withQuery(matchAllQueryAsQuery());
}

public static BaseQueryBuilder<?, ?> getBuilderWithTermQuery(String field, String value) {
return NativeQuery.builder().withQuery(termQueryAsQuery(field, value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
*
* @author Peter-Josef Meisch
* @since 4.4
* @deprecated since 5.1, use {@link Queries} instead.
*/
@Deprecated(forRemoval = true)
public final class QueryBuilders {

private QueryBuilders() {}
Expand All @@ -62,6 +64,7 @@ public static Query idsQueryAsQuery(List<String> ids) {

return builder.apply(new Query.Builder()).build();
}

public static MatchQuery matchQuery(String fieldName, String query, @Nullable Operator operator,
@Nullable Float boost) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,12 @@ public ReactiveClusterOperations cluster() {

@Override
public Query matchAllQuery() {
return NativeQuery.builder().withQuery(QueryBuilders.matchAllQueryAsQuery()).build();
return NativeQuery.builder().withQuery(Queries.matchAllQueryAsQuery()).build();
}

@Override
public Query idsQuery(List<String> ids) {
return NativeQuery.builder().withQuery(QueryBuilders.idsQueryAsQuery(ids)).build();
return NativeQuery.builder().withQuery(Queries.idsQueryAsQuery(ids)).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ private SortOptions getSortOptions(Sort.Order order, @Nullable ElasticsearchPers
return SortOptions.of(so -> so //
.geoDistance(gd -> gd //
.field(fieldName) //
.location(loc -> loc.latlon(QueryBuilders.latLon(geoDistanceOrder.getGeoPoint())))//
.location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint())))//
.distanceType(TypeUtils.geoDistanceType(geoDistanceOrder.getDistanceType()))
.mode(TypeUtils.sortMode(finalMode)) //
.unit(TypeUtils.distanceUnit(geoDistanceOrder.getUnit())) //
Expand Down Expand Up @@ -1443,7 +1443,7 @@ private co.elastic.clients.elasticsearch._types.query_dsl.Query getQuery(@Nullab
if (query instanceof CriteriaQuery) {
esQuery = CriteriaQueryProcessor.createQuery(((CriteriaQuery) query).getCriteria());
} else if (query instanceof StringQuery) {
esQuery = QueryBuilders.wrapperQueryAsQuery(((StringQuery) query).getSource());
esQuery = Queries.wrapperQueryAsQuery(((StringQuery) query).getSource());
} else if (query instanceof NativeQuery nativeQuery) {

if (nativeQuery.getQuery() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package org.springframework.data.elasticsearch;

import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;

import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;

import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.QueryBuilders;
import org.springframework.data.elasticsearch.client.elc.Queries;
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;

Expand All @@ -29,14 +29,16 @@
*
* @author Peter-Josef Meisch
* @since 4.4
* @deprecated since 5.1, use the corresponding methods from {@link Queries}.
*/
@Deprecated(forRemoval = true)
public final class ELCQueries {

private ELCQueries() {}

public static Query getTermsAggsQuery(String aggsName, String aggsField){
public static Query getTermsAggsQuery(String aggsName, String aggsField) {
return NativeQuery.builder() //
.withQuery(QueryBuilders.matchAllQueryAsQuery()) //
.withQuery(Queries.matchAllQueryAsQuery()) //
.withAggregation(aggsName, Aggregation.of(a -> a //
.terms(ta -> ta.field(aggsField)))) //
.withMaxResults(0) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.data.elasticsearch;

import static co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;

import co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.data.elasticsearch.core;

import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;

import co.elastic.clients.elasticsearch._types.SortOrder;
Expand All @@ -35,9 +35,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.ELCQueries;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.data.elasticsearch.client.elc.Queries;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder;
Expand Down Expand Up @@ -93,7 +93,7 @@ public void shouldSortResultsBySortOptions() {

@Override
protected Query queryWithIds(String... ids) {
return ELCQueries.queryWithIds(ids);
return Queries.queryWithIds(ids);
}

@Override
Expand All @@ -103,7 +103,7 @@ protected Query getTermQuery(String field, String value) {

@Override
protected BaseQueryBuilder<?, ?> getBuilderWithMatchAllQuery() {
return ELCQueries.getBuilderWithMatchAllQuery();
return Queries.getBuilderWithMatchAllQuery();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.data.elasticsearch.core;

import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;

import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Buckets;
Expand All @@ -30,10 +30,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.ELCQueries;
import org.springframework.data.elasticsearch.client.elc.Aggregation;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregation;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.Queries;
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchTemplateConfiguration;
Expand All @@ -59,17 +59,17 @@ IndexNameProvider indexNameProvider() {

@Override
protected Query getTermsAggsQuery(String aggsName, String aggsField) {
return ELCQueries.getTermsAggsQuery(aggsName, aggsField);
return Queries.getTermsAggsQuery(aggsName, aggsField);
}

@Override
protected BaseQueryBuilder<?, ?> getBuilderWithMatchAllQuery() {
return ELCQueries.getBuilderWithMatchAllQuery();
return Queries.getBuilderWithMatchAllQuery();
}

@Override
protected BaseQueryBuilder<?, ?> getBuilderWithTermQuery(String field, String value) {
return ELCQueries.getBuilderWithTermQuery(field, value);
return Queries.getBuilderWithTermQuery(field, value);
}

@Override
Expand All @@ -88,7 +88,7 @@ protected Query getQueryWithCollapse(String collapseField, @Nullable String inne

@Override
protected Query queryWithIds(String... ids) {
return ELCQueries.queryWithIds(ids);
return Queries.queryWithIds(ids);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.data.elasticsearch.core;

import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Loading