Skip to content

Commit 2ea568d

Browse files
authored
Rename QueryBuilders to avoid name clash wih Elasticsearch class.
Original Pull Request #2397 Closes #2390
1 parent c460a5f commit 2ea568d

12 files changed

+228
-28
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/CriteriaQueryProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client.elc;
1717

18-
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
18+
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
1919
import static org.springframework.util.StringUtils.*;
2020

2121
import co.elastic.clients.elasticsearch._types.FieldValue;
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client.elc;
17+
18+
import co.elastic.clients.elasticsearch._types.FieldValue;
19+
import co.elastic.clients.elasticsearch._types.LatLonGeoLocation;
20+
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
21+
import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
22+
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
23+
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
24+
import co.elastic.clients.elasticsearch._types.query_dsl.Operator;
25+
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
26+
import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
27+
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
28+
import co.elastic.clients.elasticsearch._types.query_dsl.WildcardQuery;
29+
import co.elastic.clients.elasticsearch._types.query_dsl.WrapperQuery;
30+
import co.elastic.clients.util.ObjectBuilder;
31+
32+
import java.nio.charset.StandardCharsets;
33+
import java.util.Base64;
34+
import java.util.List;
35+
import java.util.function.Function;
36+
37+
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
38+
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
39+
import org.springframework.lang.Nullable;
40+
import org.springframework.util.Assert;
41+
42+
/**
43+
* Utility class simplifying the creation of some more complex queries and type.
44+
*
45+
* @author Peter-Josef Meisch
46+
* @since 4.4
47+
*/
48+
public final class Queries {
49+
50+
private Queries() {}
51+
52+
public static IdsQuery idsQuery(List<String> ids) {
53+
54+
Assert.notNull(ids, "ids must not be null");
55+
56+
return IdsQuery.of(i -> i.values(ids));
57+
}
58+
59+
public static Query idsQueryAsQuery(List<String> ids) {
60+
61+
Assert.notNull(ids, "ids must not be null");
62+
63+
Function<Query.Builder, ObjectBuilder<Query>> builder = b -> b.ids(idsQuery(ids));
64+
65+
return builder.apply(new Query.Builder()).build();
66+
}
67+
68+
public static MatchQuery matchQuery(String fieldName, String query, @Nullable Operator operator,
69+
@Nullable Float boost) {
70+
71+
Assert.notNull(fieldName, "fieldName must not be null");
72+
Assert.notNull(query, "query must not be null");
73+
74+
return MatchQuery.of(mb -> mb.field(fieldName).query(FieldValue.of(query)).operator(operator).boost(boost));
75+
}
76+
77+
public static Query matchQueryAsQuery(String fieldName, String query, @Nullable Operator operator,
78+
@Nullable Float boost) {
79+
80+
Function<Query.Builder, ObjectBuilder<Query>> builder = b -> b.match(matchQuery(fieldName, query, operator, boost));
81+
82+
return builder.apply(new Query.Builder()).build();
83+
}
84+
85+
public static MatchAllQuery matchAllQuery() {
86+
87+
return MatchAllQuery.of(b -> b);
88+
}
89+
90+
public static Query matchAllQueryAsQuery() {
91+
92+
Function<Query.Builder, ObjectBuilder<Query>> builder = b -> b.matchAll(matchAllQuery());
93+
94+
return builder.apply(new Query.Builder()).build();
95+
}
96+
97+
public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Float boost) {
98+
return queryStringQuery(fieldName, query, null, null, boost);
99+
}
100+
101+
public static QueryStringQuery queryStringQuery(String fieldName, String query, Operator defaultOperator,
102+
@Nullable Float boost) {
103+
return queryStringQuery(fieldName, query, null, defaultOperator, boost);
104+
}
105+
106+
public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Boolean analyzeWildcard,
107+
@Nullable Float boost) {
108+
return queryStringQuery(fieldName, query, analyzeWildcard, null, boost);
109+
}
110+
111+
public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Boolean analyzeWildcard,
112+
@Nullable Operator defaultOperator, @Nullable Float boost) {
113+
114+
Assert.notNull(fieldName, "fieldName must not be null");
115+
Assert.notNull(query, "query must not be null");
116+
117+
return QueryStringQuery.of(qs -> qs.fields(fieldName).query(query).analyzeWildcard(analyzeWildcard)
118+
.defaultOperator(defaultOperator).boost(boost));
119+
}
120+
121+
public static TermQuery termQuery(String fieldName, String value) {
122+
123+
Assert.notNull(fieldName, "fieldName must not be null");
124+
Assert.notNull(value, "value must not be null");
125+
126+
return TermQuery.of(t -> t.field(fieldName).value(FieldValue.of(value)));
127+
}
128+
129+
public static Query termQueryAsQuery(String fieldName, String value) {
130+
131+
Function<Query.Builder, ObjectBuilder<Query>> builder = q -> q.term(termQuery(fieldName, value));
132+
return builder.apply(new Query.Builder()).build();
133+
}
134+
135+
public static WildcardQuery wildcardQuery(String field, String value) {
136+
137+
Assert.notNull(field, "field must not be null");
138+
Assert.notNull(value, "value must not be null");
139+
140+
return WildcardQuery.of(w -> w.field(field).wildcard(value));
141+
}
142+
143+
public static Query wildcardQueryAsQuery(String field, String value) {
144+
Function<Query.Builder, ObjectBuilder<Query>> builder = q -> q.wildcard(wildcardQuery(field, value));
145+
return builder.apply(new Query.Builder()).build();
146+
}
147+
148+
public static Query wrapperQueryAsQuery(String query) {
149+
150+
Function<Query.Builder, ObjectBuilder<Query>> builder = q -> q.wrapper(wrapperQuery(query));
151+
152+
return builder.apply(new Query.Builder()).build();
153+
}
154+
155+
public static WrapperQuery wrapperQuery(String query) {
156+
157+
Assert.notNull(query, "query must not be null");
158+
159+
String encodedValue = Base64.getEncoder().encodeToString(query.getBytes(StandardCharsets.UTF_8));
160+
161+
return WrapperQuery.of(wq -> wq.query(encodedValue));
162+
}
163+
164+
public static LatLonGeoLocation latLon(GeoPoint geoPoint) {
165+
166+
Assert.notNull(geoPoint, "geoPoint must not be null");
167+
168+
return latLon(geoPoint.getLat(), geoPoint.getLon());
169+
}
170+
171+
public static LatLonGeoLocation latLon(double lat, double lon) {
172+
return LatLonGeoLocation.of(_0 -> _0.lat(lat).lon(lon));
173+
}
174+
175+
public static org.springframework.data.elasticsearch.core.query.Query getTermsAggsQuery(String aggsName,
176+
String aggsField) {
177+
return NativeQuery.builder() //
178+
.withQuery(Queries.matchAllQueryAsQuery()) //
179+
.withAggregation(aggsName, Aggregation.of(a -> a //
180+
.terms(ta -> ta.field(aggsField)))) //
181+
.withMaxResults(0) //
182+
.build();
183+
}
184+
185+
public static org.springframework.data.elasticsearch.core.query.Query queryWithIds(String... ids) {
186+
return NativeQuery.builder().withIds(ids).build();
187+
}
188+
189+
public static BaseQueryBuilder<?, ?> getBuilderWithMatchAllQuery() {
190+
return NativeQuery.builder().withQuery(matchAllQueryAsQuery());
191+
}
192+
193+
public static BaseQueryBuilder<?, ?> getBuilderWithTermQuery(String field, String value) {
194+
return NativeQuery.builder().withQuery(termQueryAsQuery(field, value));
195+
}
196+
}

src/main/java/org/springframework/data/elasticsearch/client/elc/QueryBuilders.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
*
4343
* @author Peter-Josef Meisch
4444
* @since 4.4
45+
* @deprecated since 5.1, use {@link Queries} instead.
4546
*/
47+
@Deprecated(forRemoval = true)
4648
public final class QueryBuilders {
4749

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

6365
return builder.apply(new Query.Builder()).build();
6466
}
67+
6568
public static MatchQuery matchQuery(String fieldName, String query, @Nullable Operator operator,
6669
@Nullable Float boost) {
6770

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,12 @@ public ReactiveClusterOperations cluster() {
571571

572572
@Override
573573
public Query matchAllQuery() {
574-
return NativeQuery.builder().withQuery(QueryBuilders.matchAllQueryAsQuery()).build();
574+
return NativeQuery.builder().withQuery(Queries.matchAllQueryAsQuery()).build();
575575
}
576576

577577
@Override
578578
public Query idsQuery(List<String> ids) {
579-
return NativeQuery.builder().withQuery(QueryBuilders.idsQueryAsQuery(ids)).build();
579+
return NativeQuery.builder().withQuery(Queries.idsQueryAsQuery(ids)).build();
580580
}
581581

582582
/**

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ private SortOptions getSortOptions(Sort.Order order, @Nullable ElasticsearchPers
13551355
return SortOptions.of(so -> so //
13561356
.geoDistance(gd -> gd //
13571357
.field(fieldName) //
1358-
.location(loc -> loc.latlon(QueryBuilders.latLon(geoDistanceOrder.getGeoPoint())))//
1358+
.location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint())))//
13591359
.distanceType(TypeUtils.geoDistanceType(geoDistanceOrder.getDistanceType()))
13601360
.mode(TypeUtils.sortMode(finalMode)) //
13611361
.unit(TypeUtils.distanceUnit(geoDistanceOrder.getUnit())) //
@@ -1443,7 +1443,7 @@ private co.elastic.clients.elasticsearch._types.query_dsl.Query getQuery(@Nullab
14431443
if (query instanceof CriteriaQuery) {
14441444
esQuery = CriteriaQueryProcessor.createQuery(((CriteriaQuery) query).getCriteria());
14451445
} else if (query instanceof StringQuery) {
1446-
esQuery = QueryBuilders.wrapperQueryAsQuery(((StringQuery) query).getSource());
1446+
esQuery = Queries.wrapperQueryAsQuery(((StringQuery) query).getSource());
14471447
} else if (query instanceof NativeQuery nativeQuery) {
14481448

14491449
if (nativeQuery.getQuery() != null) {

src/test/java/org/springframework/data/elasticsearch/ELCQueries.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.springframework.data.elasticsearch;
1717

18-
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
18+
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
1919

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

2222
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
23-
import org.springframework.data.elasticsearch.client.elc.QueryBuilders;
23+
import org.springframework.data.elasticsearch.client.elc.Queries;
2424
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
2525
import org.springframework.data.elasticsearch.core.query.Query;
2626

@@ -29,14 +29,16 @@
2929
*
3030
* @author Peter-Josef Meisch
3131
* @since 4.4
32+
* @deprecated since 5.1, use the corresponding methods from {@link Queries}.
3233
*/
34+
@Deprecated(forRemoval = true)
3335
public final class ELCQueries {
3436

3537
private ELCQueries() {}
3638

37-
public static Query getTermsAggsQuery(String aggsName, String aggsField){
39+
public static Query getTermsAggsQuery(String aggsName, String aggsField) {
3840
return NativeQuery.builder() //
39-
.withQuery(QueryBuilders.matchAllQueryAsQuery()) //
41+
.withQuery(Queries.matchAllQueryAsQuery()) //
4042
.withAggregation(aggsName, Aggregation.of(a -> a //
4143
.terms(ta -> ta.field(aggsField)))) //
4244
.withMaxResults(0) //

src/test/java/org/springframework/data/elasticsearch/NestedObjectELCIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.data.elasticsearch;
1717

1818
import static co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders.*;
19-
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
19+
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
2020

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

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchELCIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.data.elasticsearch.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
19+
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
2020
import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
2121

2222
import co.elastic.clients.elasticsearch._types.SortOrder;
@@ -35,9 +35,9 @@
3535
import org.springframework.context.annotation.Bean;
3636
import org.springframework.context.annotation.Configuration;
3737
import org.springframework.context.annotation.Import;
38-
import org.springframework.data.elasticsearch.ELCQueries;
3938
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
4039
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
40+
import org.springframework.data.elasticsearch.client.elc.Queries;
4141
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
4242
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
4343
import org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder;
@@ -93,7 +93,7 @@ public void shouldSortResultsBySortOptions() {
9393

9494
@Override
9595
protected Query queryWithIds(String... ids) {
96-
return ELCQueries.queryWithIds(ids);
96+
return Queries.queryWithIds(ids);
9797
}
9898

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

104104
@Override
105105
protected BaseQueryBuilder<?, ?> getBuilderWithMatchAllQuery() {
106-
return ELCQueries.getBuilderWithMatchAllQuery();
106+
return Queries.getBuilderWithMatchAllQuery();
107107
}
108108

109109
@Override

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchELCIntegrationTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.data.elasticsearch.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
19+
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
2020

2121
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
2222
import co.elastic.clients.elasticsearch._types.aggregations.Buckets;
@@ -30,10 +30,10 @@
3030
import org.springframework.context.annotation.Bean;
3131
import org.springframework.context.annotation.Configuration;
3232
import org.springframework.context.annotation.Import;
33-
import org.springframework.data.elasticsearch.ELCQueries;
3433
import org.springframework.data.elasticsearch.client.elc.Aggregation;
3534
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregation;
3635
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
36+
import org.springframework.data.elasticsearch.client.elc.Queries;
3737
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
3838
import org.springframework.data.elasticsearch.core.query.Query;
3939
import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchTemplateConfiguration;
@@ -59,17 +59,17 @@ IndexNameProvider indexNameProvider() {
5959

6060
@Override
6161
protected Query getTermsAggsQuery(String aggsName, String aggsField) {
62-
return ELCQueries.getTermsAggsQuery(aggsName, aggsField);
62+
return Queries.getTermsAggsQuery(aggsName, aggsField);
6363
}
6464

6565
@Override
6666
protected BaseQueryBuilder<?, ?> getBuilderWithMatchAllQuery() {
67-
return ELCQueries.getBuilderWithMatchAllQuery();
67+
return Queries.getBuilderWithMatchAllQuery();
6868
}
6969

7070
@Override
7171
protected BaseQueryBuilder<?, ?> getBuilderWithTermQuery(String field, String value) {
72-
return ELCQueries.getBuilderWithTermQuery(field, value);
72+
return Queries.getBuilderWithTermQuery(field, value);
7373
}
7474

7575
@Override
@@ -88,7 +88,7 @@ protected Query getQueryWithCollapse(String collapseField, @Nullable String inne
8888

8989
@Override
9090
protected Query queryWithIds(String... ids) {
91-
return ELCQueries.queryWithIds(ids);
91+
return Queries.queryWithIds(ids);
9292
}
9393

9494
@Override

src/test/java/org/springframework/data/elasticsearch/core/ReindexELCIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18-
import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.*;
18+
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
1919

2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.context.annotation.Configuration;

0 commit comments

Comments
 (0)