Polishing.

This commit is contained in:
Peter-Josef Meisch 2024-04-16 20:40:53 +02:00
parent 2d5f8e8219
commit a16782ec73
No known key found for this signature in database
GPG Key ID: DE108246970C7708
8 changed files with 353 additions and 360 deletions

View File

@ -10,6 +10,7 @@
* Add support for multi search template API.
* Add support for SpEL in @Query.
* Add support for field aliases in the index mapping.
* Add support for has_child and has_parent queries.
[[new-features.5-2-0]]
== New in Spring Data Elasticsearch 5.2

View File

@ -15,57 +15,56 @@
*/
package org.springframework.data.elasticsearch.client.elc;
import java.util.function.Consumer;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.util.function.Consumer;
/**
* An abstract class that serves as a base for query processors.
* It provides a common interface and basic functionality for query processing.
* An abstract class that serves as a base for query processors. It provides a common interface and basic functionality
* for query processing.
*
* @author Aouichaoui Youssef
* @since 5.3
*/
public abstract class AbstractQueryProcessor {
/**
* Convert a spring-data-elasticsearch {@literal query} to an Elasticsearch {@literal query}.
*
* @param query spring-data-elasticsearch {@literal query}.
* @param queryConverter correct mapped field names and the values to the converted values.
* @return an Elasticsearch {@literal query}.
*/
@Nullable
static co.elastic.clients.elasticsearch._types.query_dsl.Query getEsQuery(@Nullable Query query,
@Nullable Consumer<Query> queryConverter) {
if (query == null) {
return null;
}
/**
* Convert a spring-data-elasticsearch {@literal query} to an Elasticsearch {@literal query}.
*
* @param query spring-data-elasticsearch {@literal query}.
* @param queryConverter correct mapped field names and the values to the converted values.
* @return an Elasticsearch {@literal query}.
*/
@Nullable
static co.elastic.clients.elasticsearch._types.query_dsl.Query getEsQuery(@Nullable Query query,
@Nullable Consumer<Query> queryConverter) {
if (query == null) {
return null;
}
if (queryConverter != null) {
queryConverter.accept(query);
}
if (queryConverter != null) {
queryConverter.accept(query);
}
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = null;
co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = null;
if (query instanceof CriteriaQuery criteriaQuery) {
esQuery = CriteriaQueryProcessor.createQuery(criteriaQuery.getCriteria());
} else if (query instanceof StringQuery stringQuery) {
esQuery = Queries.wrapperQueryAsQuery(stringQuery.getSource());
} else if (query instanceof NativeQuery nativeQuery) {
if (nativeQuery.getQuery() != null) {
esQuery = nativeQuery.getQuery();
} else if (nativeQuery.getSpringDataQuery() != null) {
esQuery = getEsQuery(nativeQuery.getSpringDataQuery(), queryConverter);
}
} else {
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
}
if (query instanceof CriteriaQuery criteriaQuery) {
esQuery = CriteriaQueryProcessor.createQuery(criteriaQuery.getCriteria());
} else if (query instanceof StringQuery stringQuery) {
esQuery = Queries.wrapperQueryAsQuery(stringQuery.getSource());
} else if (query instanceof NativeQuery nativeQuery) {
if (nativeQuery.getQuery() != null) {
esQuery = nativeQuery.getQuery();
} else if (nativeQuery.getSpringDataQuery() != null) {
esQuery = getEsQuery(nativeQuery.getSpringDataQuery(), queryConverter);
}
} else {
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
}
return esQuery;
}
return esQuery;
}
}

View File

@ -16,7 +16,7 @@
package org.springframework.data.elasticsearch.client.elc;
import static org.springframework.data.elasticsearch.client.elc.Queries.*;
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.scoreMode;
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
import static org.springframework.util.StringUtils.*;
import co.elastic.clients.elasticsearch._types.FieldValue;
@ -32,12 +32,10 @@ import java.util.List;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.Field;
import org.springframework.data.elasticsearch.core.query.HasChildQuery;
import org.springframework.data.elasticsearch.core.query.HasParentQuery;
import org.springframework.data.elasticsearch.core.query.InnerHitsQuery;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@ -357,27 +355,25 @@ class CriteriaQueryProcessor extends AbstractQueryProcessor {
.query(getEsQuery(query.getQuery(), null))
.innerHits(getInnerHits(query.getInnerHitsQuery()))
.ignoreUnmapped(query.getIgnoreUnmapped())
.minChildren(query.getMinChildren())
.maxChildren(query.getMaxChildren())
.scoreMode(scoreMode(query.getScoreMode()))
);
.minChildren(query.getMinChildren())
.maxChildren(query.getMaxChildren())
.scoreMode(scoreMode(query.getScoreMode())));
} else {
throw new CriteriaQueryException("value for " + fieldName + " is not a has_child query");
}
break;
case HAS_PARENT:
if (value instanceof HasParentQuery query) {
break;
case HAS_PARENT:
if (value instanceof HasParentQuery query) {
queryBuilder.hasParent(hpb -> hpb
.parentType(query.getParentType())
.query(getEsQuery(query.getQuery(), null))
.innerHits(getInnerHits(query.getInnerHitsQuery()))
.ignoreUnmapped(query.getIgnoreUnmapped())
.score(query.getScore())
);
} else {
throw new CriteriaQueryException("value for " + fieldName + " is not a has_parent query");
}
break;
.score(query.getScore()));
} else {
throw new CriteriaQueryException("value for " + fieldName + " is not a has_parent query");
}
break;
default:
throw new CriteriaQueryException("Could not build query for " + entry);
}
@ -432,19 +428,19 @@ class CriteriaQueryProcessor extends AbstractQueryProcessor {
return sb.toString();
}
/**
* Convert a spring-data-elasticsearch {@literal inner_hits} to an Elasticsearch {@literal inner_hits} query.
*
* @param query spring-data-elasticsearch {@literal inner_hits}.
* @return an Elasticsearch {@literal inner_hits} query.
*/
@Nullable
private static InnerHits getInnerHits(@Nullable InnerHitsQuery query) {
if (query == null) {
return null;
}
/**
* Convert a spring-data-elasticsearch {@literal inner_hits} to an Elasticsearch {@literal inner_hits} query.
*
* @param query spring-data-elasticsearch {@literal inner_hits}.
* @return an Elasticsearch {@literal inner_hits} query.
*/
@Nullable
private static InnerHits getInnerHits(@Nullable InnerHitsQuery query) {
if (query == null) {
return null;
}
return InnerHits.of(iqb -> iqb.from(query.getFrom()).size(query.getSize()).name(query.getName()));
}
return InnerHits.of(iqb -> iqb.from(query.getFrom()).size(query.getSize()).name(query.getName()));
}
}

View File

@ -1007,8 +1007,7 @@ public class Criteria {
/**
* @since 5.3
*/
HAS_CHILD,
HAS_PARENT;
HAS_CHILD, HAS_PARENT;
/**
* @return true if this key does not have an associated value

View File

@ -22,182 +22,183 @@ import org.springframework.util.Assert;
* Defines a has_child request.
*
* @author Aouichaoui Youssef
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html">docs</a>
* @see <a href=
* "https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html">docs</a>
* @since 5.3
*/
public class HasChildQuery {
/**
* Name of the child relationship mapped for the join field.
*/
private final String type;
/**
* Name of the child relationship mapped for the join field.
*/
private final String type;
/**
* Query that specifies the documents to run on child documents of the {@link #type} field.
*/
private final Query query;
/**
* Query that specifies the documents to run on child documents of the {@link #type} field.
*/
private final Query query;
/**
* Indicates whether to ignore an unmapped {@link #type} and not return any documents instead of an error.
* Default, this is set to {@code false}.
*/
@Nullable private final Boolean ignoreUnmapped;
/**
* Indicates whether to ignore an unmapped {@link #type} and not return any documents instead of an error. Default,
* this is set to {@code false}.
*/
@Nullable private final Boolean ignoreUnmapped;
/**
* The Maximum number of child documents that match the {@link #query} allowed for a returned parent document.
* If the parent document exceeds this limit, it is excluded from the search results.
*/
@Nullable private final Integer maxChildren;
/**
* The Maximum number of child documents that match the {@link #query} allowed for a returned parent document. If the
* parent document exceeds this limit, it is excluded from the search results.
*/
@Nullable private final Integer maxChildren;
/**
* Minimum number of child documents that match the query required to match the {@link #query} for a returned parent document.
* If the parent document does not meet this limit, it is excluded from the search results.
*/
@Nullable private final Integer minChildren;
/**
* Minimum number of child documents that match the query required to match the {@link #query} for a returned parent
* document. If the parent document does not meet this limit, it is excluded from the search results.
*/
@Nullable private final Integer minChildren;
/**
* Indicates how scores for matching child documents affect the root parent documents relevance score.
*/
@Nullable private final ScoreMode scoreMode;
/**
* Indicates how scores for matching child documents affect the root parent documents relevance score.
*/
@Nullable private final ScoreMode scoreMode;
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
@Nullable private final InnerHitsQuery innerHitsQuery;
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
@Nullable private final InnerHitsQuery innerHitsQuery;
public static Builder builder(String type) {
return new Builder(type);
}
public static Builder builder(String type) {
return new Builder(type);
}
private HasChildQuery(Builder builder) {
this.type = builder.type;
this.query = builder.query;
this.innerHitsQuery = builder.innerHitsQuery;
private HasChildQuery(Builder builder) {
this.type = builder.type;
this.query = builder.query;
this.innerHitsQuery = builder.innerHitsQuery;
this.ignoreUnmapped = builder.ignoreUnmapped;
this.ignoreUnmapped = builder.ignoreUnmapped;
this.maxChildren = builder.maxChildren;
this.minChildren = builder.minChildren;
this.maxChildren = builder.maxChildren;
this.minChildren = builder.minChildren;
this.scoreMode = builder.scoreMode;
}
this.scoreMode = builder.scoreMode;
}
public String getType() {
return type;
}
public String getType() {
return type;
}
public Query getQuery() {
return query;
}
public Query getQuery() {
return query;
}
@Nullable
public Boolean getIgnoreUnmapped() {
return ignoreUnmapped;
}
@Nullable
public Boolean getIgnoreUnmapped() {
return ignoreUnmapped;
}
@Nullable
public Integer getMaxChildren() {
return maxChildren;
}
@Nullable
public Integer getMaxChildren() {
return maxChildren;
}
@Nullable
public Integer getMinChildren() {
return minChildren;
}
@Nullable
public Integer getMinChildren() {
return minChildren;
}
@Nullable
public ScoreMode getScoreMode() {
return scoreMode;
}
@Nullable
public ScoreMode getScoreMode() {
return scoreMode;
}
@Nullable
public InnerHitsQuery getInnerHitsQuery() {
return innerHitsQuery;
}
@Nullable
public InnerHitsQuery getInnerHitsQuery() {
return innerHitsQuery;
}
public enum ScoreMode {
Default, Avg, Max, Min, Sum
}
public enum ScoreMode {
Default, Avg, Max, Min, Sum
}
public static final class Builder {
private final String type;
private Query query;
public static final class Builder {
private final String type;
private Query query;
@Nullable private Boolean ignoreUnmapped;
@Nullable private Boolean ignoreUnmapped;
@Nullable private Integer maxChildren;
@Nullable private Integer minChildren;
@Nullable private Integer maxChildren;
@Nullable private Integer minChildren;
@Nullable private ScoreMode scoreMode;
@Nullable private ScoreMode scoreMode;
@Nullable private InnerHitsQuery innerHitsQuery;
@Nullable private InnerHitsQuery innerHitsQuery;
private Builder(String type) {
Assert.notNull(type, "type must not be null");
private Builder(String type) {
Assert.notNull(type, "type must not be null");
this.type = type;
}
this.type = type;
}
/**
* Query that specifies the documents to run on child documents of the {@link #type} field.
*/
public Builder withQuery(Query query) {
this.query = query;
/**
* Query that specifies the documents to run on child documents of the {@link #type} field.
*/
public Builder withQuery(Query query) {
this.query = query;
return this;
}
return this;
}
/**
* Indicates whether to ignore an unmapped {@link #type} and not return any documents instead of an error.
* Default, this is set to {@code false}.
*/
public Builder withIgnoreUnmapped(@Nullable Boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
/**
* Indicates whether to ignore an unmapped {@link #type} and not return any documents instead of an error. Default,
* this is set to {@code false}.
*/
public Builder withIgnoreUnmapped(@Nullable Boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
return this;
}
return this;
}
/**
* The Maximum number of child documents that match the {@link #query} allowed for a returned parent document.
* If the parent document exceeds this limit, it is excluded from the search results.
*/
public Builder withMaxChildren(@Nullable Integer maxChildren) {
this.maxChildren = maxChildren;
/**
* The Maximum number of child documents that match the {@link #query} allowed for a returned parent document. If
* the parent document exceeds this limit, it is excluded from the search results.
*/
public Builder withMaxChildren(@Nullable Integer maxChildren) {
this.maxChildren = maxChildren;
return this;
}
return this;
}
/**
* Minimum number of child documents that match the query required to match the {@link #query} for a returned parent document.
* If the parent document does not meet this limit, it is excluded from the search results.
*/
public Builder withMinChildren(@Nullable Integer minChildren) {
this.minChildren = minChildren;
/**
* Minimum number of child documents that match the query required to match the {@link #query} for a returned parent
* document. If the parent document does not meet this limit, it is excluded from the search results.
*/
public Builder withMinChildren(@Nullable Integer minChildren) {
this.minChildren = minChildren;
return this;
}
return this;
}
/**
* Indicates how scores for matching child documents affect the root parent documents relevance score.
*/
public Builder withScoreMode(@Nullable ScoreMode scoreMode) {
this.scoreMode = scoreMode;
/**
* Indicates how scores for matching child documents affect the root parent documents relevance score.
*/
public Builder withScoreMode(@Nullable ScoreMode scoreMode) {
this.scoreMode = scoreMode;
return this;
}
return this;
}
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
public Builder withInnerHitsQuery(@Nullable InnerHitsQuery innerHitsQuery) {
this.innerHitsQuery = innerHitsQuery;
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
public Builder withInnerHitsQuery(@Nullable InnerHitsQuery innerHitsQuery) {
this.innerHitsQuery = innerHitsQuery;
return this;
}
return this;
}
public HasChildQuery build() {
Assert.notNull(query, "query must not be null.");
public HasChildQuery build() {
Assert.notNull(query, "query must not be null.");
return new HasChildQuery(this);
}
}
return new HasChildQuery(this);
}
}
}

View File

@ -22,119 +22,120 @@ import org.springframework.util.Assert;
* Defines a has_parent request.
*
* @author Aouichaoui Youssef
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html">docs</a>
* @see <a href=
* "https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html">docs</a>
* @since 5.3
*/
public class HasParentQuery {
/**
* Name of the parent relationship mapped for the join field.
*/
private final String parentType;
/**
* Name of the parent relationship mapped for the join field.
*/
private final String parentType;
/**
* Query that specifies the documents to run on parent documents of the {@link #parentType} field.
*/
private final Query query;
/**
* Query that specifies the documents to run on parent documents of the {@link #parentType} field.
*/
private final Query query;
/**
* Indicates whether the relevance score of a matching parent document is aggregated into its child documents.
* Default, this is set to {@code false}.
*/
@Nullable private final Boolean score;
/**
* Indicates whether the relevance score of a matching parent document is aggregated into its child documents.
* Default, this is set to {@code false}.
*/
@Nullable private final Boolean score;
/**
* Indicates whether to ignore an unmapped {@link #parentType} and not return any documents instead of an error.
* Default, this is set to {@code false}.
*/
@Nullable private final Boolean ignoreUnmapped;
/**
* Indicates whether to ignore an unmapped {@link #parentType} and not return any documents instead of an error.
* Default, this is set to {@code false}.
*/
@Nullable private final Boolean ignoreUnmapped;
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
@Nullable private final InnerHitsQuery innerHitsQuery;
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
@Nullable private final InnerHitsQuery innerHitsQuery;
public static Builder builder(String parentType) {
return new Builder(parentType);
}
public static Builder builder(String parentType) {
return new Builder(parentType);
}
private HasParentQuery(Builder builder) {
this.parentType = builder.parentType;
this.query = builder.query;
this.innerHitsQuery = builder.innerHitsQuery;
private HasParentQuery(Builder builder) {
this.parentType = builder.parentType;
this.query = builder.query;
this.innerHitsQuery = builder.innerHitsQuery;
this.score = builder.score;
this.ignoreUnmapped = builder.ignoreUnmapped;
}
this.score = builder.score;
this.ignoreUnmapped = builder.ignoreUnmapped;
}
public String getParentType() {
return parentType;
}
public String getParentType() {
return parentType;
}
public Query getQuery() {
return query;
}
public Query getQuery() {
return query;
}
@Nullable
public Boolean getScore() {
return score;
}
@Nullable
public Boolean getScore() {
return score;
}
@Nullable
public Boolean getIgnoreUnmapped() {
return ignoreUnmapped;
}
@Nullable
public Boolean getIgnoreUnmapped() {
return ignoreUnmapped;
}
@Nullable
public InnerHitsQuery getInnerHitsQuery() {
return innerHitsQuery;
}
@Nullable
public InnerHitsQuery getInnerHitsQuery() {
return innerHitsQuery;
}
public static class Builder {
private final String parentType;
private Query query;
public static class Builder {
private final String parentType;
private Query query;
@Nullable private Boolean score;
@Nullable private Boolean ignoreUnmapped;
@Nullable private Boolean score;
@Nullable private Boolean ignoreUnmapped;
@Nullable private InnerHitsQuery innerHitsQuery;
@Nullable private InnerHitsQuery innerHitsQuery;
private Builder(String parentType) {
Assert.notNull(parentType, "parent_type must not be null.");
private Builder(String parentType) {
Assert.notNull(parentType, "parent_type must not be null.");
this.parentType = parentType;
}
this.parentType = parentType;
}
public Builder withQuery(Query query) {
this.query = query;
public Builder withQuery(Query query) {
this.query = query;
return this;
}
return this;
}
public Builder withScore(@Nullable Boolean score) {
this.score = score;
public Builder withScore(@Nullable Boolean score) {
this.score = score;
return this;
}
return this;
}
public Builder withIgnoreUnmapped(@Nullable Boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
public Builder withIgnoreUnmapped(@Nullable Boolean ignoreUnmapped) {
this.ignoreUnmapped = ignoreUnmapped;
return this;
}
return this;
}
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
public Builder withInnerHitsQuery(@Nullable InnerHitsQuery innerHitsQuery) {
this.innerHitsQuery = innerHitsQuery;
/**
* Obtaining nested objects and documents that have a parent-child relationship.
*/
public Builder withInnerHitsQuery(@Nullable InnerHitsQuery innerHitsQuery) {
this.innerHitsQuery = innerHitsQuery;
return this;
}
return this;
}
public HasParentQuery build() {
Assert.notNull(query, "query must not be null.");
public HasParentQuery build() {
Assert.notNull(query, "query must not be null.");
return new HasParentQuery(this);
}
}
return new HasParentQuery(this);
}
}
}

View File

@ -25,84 +25,83 @@ import org.springframework.lang.Nullable;
* @since 5.3
*/
public class InnerHitsQuery {
/**
* The name to be used for the particular inner hit definition in the response.
*/
@Nullable private final String name;
/**
* The name to be used for the particular inner hit definition in the response.
*/
@Nullable private final String name;
/**
* The maximum number of hits to return.
*/
@Nullable private final Integer size;
/**
* The maximum number of hits to return.
*/
@Nullable private final Integer size;
/**
* The offset from where the first hit to fetch.
*/
@Nullable private final Integer from;
/**
* The offset from where the first hit to fetch.
*/
@Nullable private final Integer from;
public static Builder builder() {
return new Builder();
}
public static Builder builder() {
return new Builder();
}
private InnerHitsQuery(Builder builder) {
this.name = builder.name;
private InnerHitsQuery(Builder builder) {
this.name = builder.name;
this.from = builder.from;
this.size = builder.size;
}
this.from = builder.from;
this.size = builder.size;
}
@Nullable
public String getName() {
return name;
}
@Nullable
public String getName() {
return name;
}
@Nullable
public Integer getSize() {
return size;
}
@Nullable
public Integer getSize() {
return size;
}
@Nullable
public Integer getFrom() {
return from;
}
@Nullable
public Integer getFrom() {
return from;
}
public static final class Builder {
@Nullable private String name;
@Nullable private Integer size;
@Nullable private Integer from;
public static final class Builder {
@Nullable private String name;
@Nullable private Integer size;
@Nullable private Integer from;
private Builder() {
}
private Builder() {}
/**
* The name to be used for the particular inner hit definition in the response.
*/
public Builder withName(@Nullable String name) {
this.name = name;
/**
* The name to be used for the particular inner hit definition in the response.
*/
public Builder withName(@Nullable String name) {
this.name = name;
return this;
}
return this;
}
/**
* The maximum number of hits to return.
*/
public Builder withSize(@Nullable Integer size) {
this.size = size;
/**
* The maximum number of hits to return.
*/
public Builder withSize(@Nullable Integer size) {
this.size = size;
return this;
}
return this;
}
/**
* The offset from where the first hit to fetch.
*/
public Builder withFrom(@Nullable Integer from) {
this.from = from;
/**
* The offset from where the first hit to fetch.
*/
public Builder withFrom(@Nullable Integer from) {
this.from = from;
return this;
}
return this;
}
public InnerHitsQuery build() {
return new InnerHitsQuery(this);
}
}
public InnerHitsQuery build() {
return new InnerHitsQuery(this);
}
}
}

View File

@ -5013,21 +5013,18 @@ public abstract class ElasticsearchIntegrationTests {
@Document(indexName = "#{@indexNameProvider.indexName()}-join")
private static class RootEntity {
@Id
private String id;
@Id private String id;
@Field(type = FieldType.Object)
private Child child;
@Field(type = FieldType.Object) private Child child;
@Field(type = FieldType.Object)
private Parent parent;
@Field(type = FieldType.Object) private Parent parent;
@JoinTypeRelations(relations = {
@JoinTypeRelation(parent = "parent", children = {"child"})
})
private JoinField<String> relation = new JoinField<>("parent");
@JoinTypeRelation(parent = "parent", children = { "child" })
}) private JoinField<String> relation = new JoinField<>("parent");
private static final class Child {}
private static final class Parent {}
public static Builder builder() {