|
| 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 | +} |
0 commit comments