Skip to content

Commit 7bf0425

Browse files
committed
Create a common class for processing queries.
Signed-off-by: Youssef Aouichaoui <youssef3wi@icloud.com>
1 parent 809256f commit 7bf0425

File tree

4 files changed

+77
-59
lines changed

4 files changed

+77
-59
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 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 org.springframework.data.elasticsearch.core.query.CriteriaQuery;
19+
import org.springframework.data.elasticsearch.core.query.Query;
20+
import org.springframework.data.elasticsearch.core.query.StringQuery;
21+
import org.springframework.lang.Nullable;
22+
import org.springframework.util.Assert;
23+
24+
import java.util.function.Consumer;
25+
26+
/**
27+
* An abstract class that serves as a base for query processors.
28+
* It provides a common interface and basic functionality for query processing.
29+
*
30+
* @author Aouichaoui Youssef
31+
* @since 5.3
32+
*/
33+
public abstract class AbstractQueryProcessor {
34+
35+
/**
36+
* Convert a spring-data-elasticsearch {@literal query} to an Elasticsearch {@literal query}.
37+
*
38+
* @param query spring-data-elasticsearch {@literal query}.
39+
* @param queryConverter correct mapped field names and the values to the converted values.
40+
* @return an Elasticsearch {@literal query}.
41+
*/
42+
@Nullable
43+
static co.elastic.clients.elasticsearch._types.query_dsl.Query getEsQuery(@Nullable Query query,
44+
@Nullable Consumer<Query> queryConverter) {
45+
if (query == null) {
46+
return null;
47+
}
48+
49+
if (queryConverter != null) {
50+
queryConverter.accept(query);
51+
}
52+
53+
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = null;
54+
55+
if (query instanceof CriteriaQuery criteriaQuery) {
56+
esQuery = CriteriaQueryProcessor.createQuery(criteriaQuery.getCriteria());
57+
} else if (query instanceof StringQuery stringQuery) {
58+
esQuery = Queries.wrapperQueryAsQuery(stringQuery.getSource());
59+
} else if (query instanceof NativeQuery nativeQuery) {
60+
if (nativeQuery.getQuery() != null) {
61+
esQuery = nativeQuery.getQuery();
62+
} else if (nativeQuery.getSpringDataQuery() != null) {
63+
esQuery = getEsQuery(nativeQuery.getSpringDataQuery(), queryConverter);
64+
}
65+
} else {
66+
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
67+
}
68+
69+
Assert.notNull(esQuery, "query must not be null.");
70+
return esQuery;
71+
}
72+
}

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

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @author Ezequiel Antúnez Camacho
5050
* @since 4.4
5151
*/
52-
class CriteriaQueryProcessor {
52+
class CriteriaQueryProcessor extends AbstractQueryProcessor {
5353

5454
/**
5555
* creates a query from the criteria
@@ -354,7 +354,7 @@ private static Query.Builder queryFor(Criteria.CriteriaEntry entry, Field field,
354354
if (value instanceof HasChildQuery query) {
355355
queryBuilder.hasChild(hcb -> hcb
356356
.type(query.getType())
357-
.query(getQuery(query.getQuery()))
357+
.query(getEsQuery(query.getQuery(), null))
358358
.innerHits(getInnerHits(query.getInnerHitsQuery()))
359359
.ignoreUnmapped(query.getIgnoreUnmapped())
360360
.minChildren(query.getMinChildren())
@@ -369,7 +369,7 @@ private static Query.Builder queryFor(Criteria.CriteriaEntry entry, Field field,
369369
if (value instanceof HasParentQuery query) {
370370
queryBuilder.hasParent(hpb -> hpb
371371
.parentType(query.getParentType())
372-
.query(getQuery(query.getQuery()))
372+
.query(getEsQuery(query.getQuery(), null))
373373
.innerHits(getInnerHits(query.getInnerHitsQuery()))
374374
.ignoreUnmapped(query.getIgnoreUnmapped())
375375
.score(query.getScore())
@@ -432,35 +432,6 @@ public static String escape(String s) {
432432
return sb.toString();
433433
}
434434

435-
/**
436-
* Convert a spring-data-elasticsearch {@literal query} to an Elasticsearch {@literal query}.
437-
* <p>
438-
* The reason it was copied from {@link RequestConverter#getQuery(org.springframework.data.elasticsearch.core.query.Query, Class)} is because of a circular dependency.
439-
*
440-
* @param query spring-data-elasticsearch {@literal query}.
441-
* @return an Elasticsearch {@literal query}.
442-
*/
443-
private static Query getQuery(org.springframework.data.elasticsearch.core.query.Query query) {
444-
Query esQuery = null;
445-
446-
if (query instanceof CriteriaQuery) {
447-
esQuery = createQuery(((CriteriaQuery) query).getCriteria());
448-
} else if (query instanceof StringQuery) {
449-
esQuery = Queries.wrapperQueryAsQuery(((StringQuery) query).getSource());
450-
} else if (query instanceof NativeQuery nativeQuery) {
451-
if (nativeQuery.getQuery() != null) {
452-
esQuery = nativeQuery.getQuery();
453-
} else if (nativeQuery.getSpringDataQuery() != null) {
454-
esQuery = getQuery(nativeQuery.getSpringDataQuery());
455-
}
456-
} else {
457-
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
458-
}
459-
460-
Assert.notNull(query, "query must not be null.");
461-
return esQuery;
462-
}
463-
464435
/**
465436
* Convert a spring-data-elasticsearch {@literal inner_hits} to an Elasticsearch {@literal inner_hits} query.
466437
*

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

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
* @since 4.4
113113
*/
114114
@SuppressWarnings("ClassCanBeRecord")
115-
class RequestConverter {
115+
class RequestConverter extends AbstractQueryProcessor {
116116

117117
private static final Log LOGGER = LogFactory.getLog(RequestConverter.class);
118118

@@ -1755,31 +1755,7 @@ private void prepareNativeSearch(NativeQuery query, MultisearchBody.Builder buil
17551755
@Nullable
17561756
co.elastic.clients.elasticsearch._types.query_dsl.Query getQuery(@Nullable Query query,
17571757
@Nullable Class<?> clazz) {
1758-
1759-
if (query == null) {
1760-
return null;
1761-
}
1762-
1763-
elasticsearchConverter.updateQuery(query, clazz);
1764-
1765-
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = null;
1766-
1767-
if (query instanceof CriteriaQuery) {
1768-
esQuery = CriteriaQueryProcessor.createQuery(((CriteriaQuery) query).getCriteria());
1769-
} else if (query instanceof StringQuery) {
1770-
esQuery = Queries.wrapperQueryAsQuery(((StringQuery) query).getSource());
1771-
} else if (query instanceof NativeQuery nativeQuery) {
1772-
1773-
if (nativeQuery.getQuery() != null) {
1774-
esQuery = nativeQuery.getQuery();
1775-
} else if (nativeQuery.getSpringDataQuery() != null) {
1776-
esQuery = getQuery(nativeQuery.getSpringDataQuery(), clazz);
1777-
}
1778-
} else {
1779-
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
1780-
}
1781-
1782-
return esQuery;
1758+
return getEsQuery(query, (q) -> elasticsearchConverter.updateQuery(q, clazz));
17831759
}
17841760

17851761
private void addFilter(Query query, SearchRequest.Builder builder) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.springframework.data.elasticsearch.annotations.*;
5050
import org.springframework.data.elasticsearch.annotations.Field;
5151
import org.springframework.data.elasticsearch.annotations.ScriptedField;
52-
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
5352
import org.springframework.data.elasticsearch.core.document.Explanation;
5453
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
5554
import org.springframework.data.elasticsearch.core.index.AliasAction;

0 commit comments

Comments
 (0)