Skip to content

Commit a16782e

Browse files
committed
Polishing.
1 parent 2d5f8e8 commit a16782e

File tree

8 files changed

+407
-414
lines changed

8 files changed

+407
-414
lines changed

src/main/antora/modules/ROOT/pages/elasticsearch/elasticsearch-new.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Add support for multi search template API.
1111
* Add support for SpEL in @Query.
1212
* Add support for field aliases in the index mapping.
13+
* Add support for has_child and has_parent queries.
1314

1415
[[new-features.5-2-0]]
1516
== New in Spring Data Elasticsearch 5.2

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

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,56 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client.elc;
1717

18+
import java.util.function.Consumer;
19+
1820
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
1921
import org.springframework.data.elasticsearch.core.query.Query;
2022
import org.springframework.data.elasticsearch.core.query.StringQuery;
2123
import org.springframework.lang.Nullable;
22-
import org.springframework.util.Assert;
23-
24-
import java.util.function.Consumer;
2524

2625
/**
27-
* An abstract class that serves as a base for query processors.
28-
* It provides a common interface and basic functionality for query processing.
26+
* An abstract class that serves as a base for query processors. It provides a common interface and basic functionality
27+
* for query processing.
2928
*
3029
* @author Aouichaoui Youssef
3130
* @since 5.3
3231
*/
3332
public abstract class AbstractQueryProcessor {
3433

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-
}
34+
/**
35+
* Convert a spring-data-elasticsearch {@literal query} to an Elasticsearch {@literal query}.
36+
*
37+
* @param query spring-data-elasticsearch {@literal query}.
38+
* @param queryConverter correct mapped field names and the values to the converted values.
39+
* @return an Elasticsearch {@literal query}.
40+
*/
41+
@Nullable
42+
static co.elastic.clients.elasticsearch._types.query_dsl.Query getEsQuery(@Nullable Query query,
43+
@Nullable Consumer<Query> queryConverter) {
44+
if (query == null) {
45+
return null;
46+
}
4847

49-
if (queryConverter != null) {
50-
queryConverter.accept(query);
51-
}
48+
if (queryConverter != null) {
49+
queryConverter.accept(query);
50+
}
5251

53-
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = null;
52+
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = null;
5453

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-
}
54+
if (query instanceof CriteriaQuery criteriaQuery) {
55+
esQuery = CriteriaQueryProcessor.createQuery(criteriaQuery.getCriteria());
56+
} else if (query instanceof StringQuery stringQuery) {
57+
esQuery = Queries.wrapperQueryAsQuery(stringQuery.getSource());
58+
} else if (query instanceof NativeQuery nativeQuery) {
59+
if (nativeQuery.getQuery() != null) {
60+
esQuery = nativeQuery.getQuery();
61+
} else if (nativeQuery.getSpringDataQuery() != null) {
62+
esQuery = getEsQuery(nativeQuery.getSpringDataQuery(), queryConverter);
63+
}
64+
} else {
65+
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
66+
}
6867

69-
return esQuery;
70-
}
68+
return esQuery;
69+
}
7170
}

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

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

1818
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
19-
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.scoreMode;
19+
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
2020
import static org.springframework.util.StringUtils.*;
2121

2222
import co.elastic.clients.elasticsearch._types.FieldValue;
@@ -32,12 +32,10 @@
3232

3333
import org.springframework.data.elasticsearch.annotations.FieldType;
3434
import org.springframework.data.elasticsearch.core.query.Criteria;
35-
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
3635
import org.springframework.data.elasticsearch.core.query.Field;
3736
import org.springframework.data.elasticsearch.core.query.HasChildQuery;
3837
import org.springframework.data.elasticsearch.core.query.HasParentQuery;
3938
import org.springframework.data.elasticsearch.core.query.InnerHitsQuery;
40-
import org.springframework.data.elasticsearch.core.query.StringQuery;
4139
import org.springframework.lang.Nullable;
4240
import org.springframework.util.Assert;
4341

@@ -357,27 +355,25 @@ private static Query.Builder queryFor(Criteria.CriteriaEntry entry, Field field,
357355
.query(getEsQuery(query.getQuery(), null))
358356
.innerHits(getInnerHits(query.getInnerHitsQuery()))
359357
.ignoreUnmapped(query.getIgnoreUnmapped())
360-
.minChildren(query.getMinChildren())
361-
.maxChildren(query.getMaxChildren())
362-
.scoreMode(scoreMode(query.getScoreMode()))
363-
);
358+
.minChildren(query.getMinChildren())
359+
.maxChildren(query.getMaxChildren())
360+
.scoreMode(scoreMode(query.getScoreMode())));
364361
} else {
365362
throw new CriteriaQueryException("value for " + fieldName + " is not a has_child query");
366363
}
367-
break;
368-
case HAS_PARENT:
369-
if (value instanceof HasParentQuery query) {
364+
break;
365+
case HAS_PARENT:
366+
if (value instanceof HasParentQuery query) {
370367
queryBuilder.hasParent(hpb -> hpb
371368
.parentType(query.getParentType())
372369
.query(getEsQuery(query.getQuery(), null))
373370
.innerHits(getInnerHits(query.getInnerHitsQuery()))
374371
.ignoreUnmapped(query.getIgnoreUnmapped())
375-
.score(query.getScore())
376-
);
377-
} else {
378-
throw new CriteriaQueryException("value for " + fieldName + " is not a has_parent query");
379-
}
380-
break;
372+
.score(query.getScore()));
373+
} else {
374+
throw new CriteriaQueryException("value for " + fieldName + " is not a has_parent query");
375+
}
376+
break;
381377
default:
382378
throw new CriteriaQueryException("Could not build query for " + entry);
383379
}
@@ -432,19 +428,19 @@ public static String escape(String s) {
432428
return sb.toString();
433429
}
434430

435-
/**
436-
* Convert a spring-data-elasticsearch {@literal inner_hits} to an Elasticsearch {@literal inner_hits} query.
437-
*
438-
* @param query spring-data-elasticsearch {@literal inner_hits}.
439-
* @return an Elasticsearch {@literal inner_hits} query.
440-
*/
441-
@Nullable
442-
private static InnerHits getInnerHits(@Nullable InnerHitsQuery query) {
443-
if (query == null) {
444-
return null;
445-
}
446-
447-
return InnerHits.of(iqb -> iqb.from(query.getFrom()).size(query.getSize()).name(query.getName()));
448-
}
431+
/**
432+
* Convert a spring-data-elasticsearch {@literal inner_hits} to an Elasticsearch {@literal inner_hits} query.
433+
*
434+
* @param query spring-data-elasticsearch {@literal inner_hits}.
435+
* @return an Elasticsearch {@literal inner_hits} query.
436+
*/
437+
@Nullable
438+
private static InnerHits getInnerHits(@Nullable InnerHitsQuery query) {
439+
if (query == null) {
440+
return null;
441+
}
442+
443+
return InnerHits.of(iqb -> iqb.from(query.getFrom()).size(query.getSize()).name(query.getName()));
444+
}
449445

450446
}

src/main/java/org/springframework/data/elasticsearch/core/query/Criteria.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,7 @@ public enum OperationKey { //
10071007
/**
10081008
* @since 5.3
10091009
*/
1010-
HAS_CHILD,
1011-
HAS_PARENT;
1010+
HAS_CHILD, HAS_PARENT;
10121011

10131012
/**
10141013
* @return true if this key does not have an associated value

0 commit comments

Comments
 (0)