|
16 | 16 | package org.springframework.data.elasticsearch.client.elc;
|
17 | 17 |
|
18 | 18 | import static org.springframework.data.elasticsearch.client.elc.Queries.*;
|
| 19 | +import static org.springframework.data.elasticsearch.client.elc.TypeUtils.scoreMode; |
19 | 20 | import static org.springframework.util.StringUtils.*;
|
20 | 21 |
|
21 | 22 | import co.elastic.clients.elasticsearch._types.FieldValue;
|
22 | 23 | import co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode;
|
23 | 24 | import co.elastic.clients.elasticsearch._types.query_dsl.Operator;
|
24 | 25 | import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
| 26 | +import co.elastic.clients.elasticsearch.core.search.InnerHits; |
25 | 27 | import co.elastic.clients.json.JsonData;
|
26 | 28 |
|
27 | 29 | import java.util.ArrayList;
|
|
30 | 32 |
|
31 | 33 | import org.springframework.data.elasticsearch.annotations.FieldType;
|
32 | 34 | import org.springframework.data.elasticsearch.core.query.Criteria;
|
| 35 | +import org.springframework.data.elasticsearch.core.query.CriteriaQuery; |
33 | 36 | import org.springframework.data.elasticsearch.core.query.Field;
|
| 37 | +import org.springframework.data.elasticsearch.core.query.HasChildQuery; |
| 38 | +import org.springframework.data.elasticsearch.core.query.HasParentQuery; |
| 39 | +import org.springframework.data.elasticsearch.core.query.InnerHitsQuery; |
| 40 | +import org.springframework.data.elasticsearch.core.query.StringQuery; |
34 | 41 | import org.springframework.lang.Nullable;
|
35 | 42 | import org.springframework.util.Assert;
|
36 | 43 |
|
@@ -343,6 +350,34 @@ private static Query.Builder queryFor(Criteria.CriteriaEntry entry, Field field,
|
343 | 350 | .value(value.toString()) //
|
344 | 351 | .boost(boost)); //
|
345 | 352 | break;
|
| 353 | + case HAS_CHILD: |
| 354 | + if (value instanceof HasChildQuery query) { |
| 355 | + queryBuilder.hasChild(hcb -> hcb |
| 356 | + .type(query.getType()) |
| 357 | + .query(getQuery(query.getQuery())) |
| 358 | + .innerHits(getInnerHits(query.getInnerHitsQuery())) |
| 359 | + .ignoreUnmapped(query.getIgnoreUnmapped()) |
| 360 | + .minChildren(query.getMinChildren()) |
| 361 | + .maxChildren(query.getMaxChildren()) |
| 362 | + .scoreMode(scoreMode(query.getScoreMode())) |
| 363 | + ); |
| 364 | + } else { |
| 365 | + throw new CriteriaQueryException("value for " + fieldName + " is not a has_child query"); |
| 366 | + } |
| 367 | + break; |
| 368 | + case HAS_PARENT: |
| 369 | + if (value instanceof HasParentQuery query) { |
| 370 | + queryBuilder.hasParent(hpb -> hpb |
| 371 | + .parentType(query.getParentType()) |
| 372 | + .query(getQuery(query.getQuery())) |
| 373 | + .innerHits(getInnerHits(query.getInnerHitsQuery())) |
| 374 | + .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; |
346 | 381 | default:
|
347 | 382 | throw new CriteriaQueryException("Could not build query for " + entry);
|
348 | 383 | }
|
@@ -397,4 +432,48 @@ public static String escape(String s) {
|
397 | 432 | return sb.toString();
|
398 | 433 | }
|
399 | 434 |
|
| 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 | + |
| 464 | + /** |
| 465 | + * Convert a spring-data-elasticsearch {@literal inner_hits} to an Elasticsearch {@literal inner_hits} query. |
| 466 | + * |
| 467 | + * @param query spring-data-elasticsearch {@literal inner_hits}. |
| 468 | + * @return an Elasticsearch {@literal inner_hits} query. |
| 469 | + */ |
| 470 | + @Nullable |
| 471 | + private static InnerHits getInnerHits(@Nullable InnerHitsQuery query) { |
| 472 | + if (query == null) { |
| 473 | + return null; |
| 474 | + } |
| 475 | + |
| 476 | + return InnerHits.of(iqb -> iqb.from(query.getFrom()).size(query.getSize()).name(query.getName())); |
| 477 | + } |
| 478 | + |
400 | 479 | }
|
0 commit comments