Add missing properties to BaseQueryBuilder.

Original Pull Request #2251
Closes #2250
This commit is contained in:
Peter-Josef Meisch 2022-08-04 17:50:37 +02:00 committed by GitHub
parent acf02a1dc9
commit e71c66949a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 204 additions and 50 deletions

View File

@ -46,7 +46,6 @@ public class NativeQuery extends BaseQuery {
@Nullable private Suggester suggester;
@Nullable private FieldCollapse fieldCollapse;
private List<ScriptedField> scriptedFields = Collections.emptyList();
private List<RescorerQuery> rescorerQueries = Collections.emptyList();
public NativeQuery(NativeQueryBuilder builder) {
super(builder);
@ -56,7 +55,6 @@ public class NativeQuery extends BaseQuery {
this.suggester = builder.getSuggester();
this.fieldCollapse = builder.getFieldCollapse();
this.scriptedFields = builder.getScriptedFields();
this.rescorerQueries = builder.getRescorerQueries();
}
public NativeQuery(@Nullable Query query) {
@ -94,9 +92,4 @@ public class NativeQuery extends BaseQuery {
public List<ScriptedField> getScriptedFields() {
return scriptedFields;
}
@Override
public List<RescorerQuery> getRescorerQueries() {
return rescorerQueries;
}
}

View File

@ -45,8 +45,6 @@ public class NativeQueryBuilder extends BaseQueryBuilder<NativeQuery, NativeQuer
@Nullable private Suggester suggester;
@Nullable private FieldCollapse fieldCollapse;
private final List<ScriptedField> scriptedFields = new ArrayList<>();
private List<RescorerQuery> rescorerQueries = new ArrayList<>();
public NativeQueryBuilder() {}
@Nullable
@ -77,10 +75,6 @@ public class NativeQueryBuilder extends BaseQueryBuilder<NativeQuery, NativeQuer
return scriptedFields;
}
public List<RescorerQuery> getRescorerQueries() {
return rescorerQueries;
}
public NativeQueryBuilder withQuery(Query query) {
Assert.notNull(query, "query must not be null");
@ -135,14 +129,6 @@ public class NativeQueryBuilder extends BaseQueryBuilder<NativeQuery, NativeQuer
return this;
}
public NativeQueryBuilder withResorerQuery(RescorerQuery resorerQuery) {
Assert.notNull(resorerQuery, "resorerQuery must not be null");
this.rescorerQueries.add(resorerQuery);
return this;
}
public NativeQuery build() {
return new NativeQuery(this);
}

View File

@ -47,8 +47,8 @@ import org.springframework.util.Assert;
*/
public class BaseQuery implements Query {
protected Pageable pageable = DEFAULT_PAGE;
@Nullable protected Sort sort;
protected Pageable pageable = DEFAULT_PAGE;
protected List<String> fields = new ArrayList<>();
@Nullable protected List<String> storedFields;
@Nullable protected SourceFilter sourceFilter;
@ -67,11 +67,11 @@ public class BaseQuery implements Query {
@Nullable protected Duration timeout;
private boolean explain = false;
@Nullable protected List<Object> searchAfter;
@Nullable protected List<IndexBoost> indicesBoost;
protected List<RescorerQuery> rescorerQueries = new ArrayList<>();
@Nullable protected Boolean requestCache;
protected List<IdWithRouting> idsWithRouting = Collections.emptyList();
protected final List<RuntimeField> runtimeFields = new ArrayList<>();
@Nullable protected List<IndexBoost> indicesBoost;
public BaseQuery() {}
@ -79,17 +79,28 @@ public class BaseQuery implements Query {
this.sort = builder.getSort();
// do a setPageable after setting the sort, because the pageable may contain an additional sort
this.setPageable(builder.getPageable() != null ? builder.getPageable() : DEFAULT_PAGE);
this.ids = builder.getIds();
this.trackScores = builder.getTrackScores();
this.maxResults = builder.getMaxResults();
this.indicesOptions = builder.getIndicesOptions();
this.minScore = builder.getMinScore();
this.preference = builder.getPreference();
this.sourceFilter = builder.getSourceFilter();
this.fields = builder.getFields();
this.highlightQuery = builder.highlightQuery;
this.storedFields = builder.getStoredFields();
this.sourceFilter = builder.getSourceFilter();
this.minScore = builder.getMinScore();
this.ids = builder.getIds().isEmpty() ? null : builder.getIds();
this.route = builder.getRoute();
this.searchType = builder.getSearchType();
this.indicesOptions = builder.getIndicesOptions();
this.trackScores = builder.getTrackScores();
this.preference = builder.getPreference();
this.maxResults = builder.getMaxResults();
this.highlightQuery = builder.getHighlightQuery();
this.trackTotalHits = builder.getTrackTotalHits();
this.trackTotalHitsUpTo = builder.getTrackTotalHitsUpTo();
this.scrollTime = builder.getScrollTime();
this.timeout = builder.getTimeout();
this.explain = builder.getExplain();
this.searchAfter = builder.getSearchAfter();
this.indicesBoost = builder.getIndicesBoost();
this.rescorerQueries = builder.getRescorerQueries();
this.requestCache = builder.getRequestCache();
this.idsWithRouting = builder.getIdsWithRouting();
}
@Override

View File

@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.core.query;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -23,7 +24,9 @@ import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.RuntimeField;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* base class for query builders. The different implementations of {@link Query} should derive from this class and then
@ -34,28 +37,51 @@ import org.springframework.lang.Nullable;
*/
public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQueryBuilder<Q, SELF>> {
@Nullable private Pageable pageable;
@Nullable private Sort sort;
@Nullable private Integer maxResults;
@Nullable private Collection<String> ids;
private boolean trackScores;
@Nullable protected IndicesOptions indicesOptions;
private float minScore;
@Nullable private String preference;
@Nullable private Pageable pageable;
private final List<String> fields = new ArrayList<>();
@Nullable private List<String> storedFields;
@Nullable private SourceFilter sourceFilter;
private List<String> fields = new ArrayList<>();
@Nullable protected HighlightQuery highlightQuery;
private float minScore;
private final Collection<String> ids = new ArrayList<>();
@Nullable private String route;
protected Query.SearchType searchType = Query.SearchType.QUERY_THEN_FETCH;
@Nullable protected IndicesOptions indicesOptions;
private boolean trackScores;
@Nullable private String preference;
@Nullable private Integer maxResults;
@Nullable protected HighlightQuery highlightQuery;
@Nullable private Boolean trackTotalHits;
@Nullable protected Integer trackTotalHitsUpTo;
@Nullable protected Duration scrollTime;
@Nullable protected Duration timeout;
boolean explain = false;
@Nullable protected List<Object> searchAfter;
@Nullable private List<IndexBoost> indicesBoost;
protected final List<RescorerQuery> rescorerQueries = new ArrayList<>();
@Nullable protected Boolean requestCache;
protected final List<Query.IdWithRouting> idsWithRouting = new ArrayList<>();
protected final List<RuntimeField> runtimeFields = new ArrayList<>();
@Nullable
public Sort getSort() {
return sort;
}
@Nullable
public Pageable getPageable() {
return pageable;
}
public List<String> getFields() {
return fields;
}
@Nullable
public Sort getSort() {
return sort;
public List<String> getStoredFields() {
return storedFields;
}
@Nullable
@ -91,10 +117,6 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return sourceFilter;
}
public List<String> getFields() {
return fields;
}
@Nullable
public HighlightQuery getHighlightQuery() {
return highlightQuery;
@ -110,6 +132,56 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return indicesBoost;
}
public Query.SearchType getSearchType() {
return searchType;
}
@Nullable
public Boolean getTrackTotalHits() {
return trackTotalHits;
}
@Nullable
public Integer getTrackTotalHitsUpTo() {
return trackTotalHitsUpTo;
}
@Nullable
public Duration getScrollTime() {
return scrollTime;
}
@Nullable
public Duration getTimeout() {
return timeout;
}
public boolean getExplain() {
return explain;
}
@Nullable
public List<Object> getSearchAfter() {
return searchAfter;
}
@Nullable
public Boolean getRequestCache() {
return requestCache;
}
public List<Query.IdWithRouting> getIdsWithRouting() {
return idsWithRouting;
}
public List<RuntimeField> getRuntimeFields() {
return runtimeFields;
}
public List<RescorerQuery> getRescorerQueries() {
return rescorerQueries;
}
public SELF withPageable(Pageable pageable) {
this.pageable = pageable;
return self();
@ -130,12 +202,18 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
}
public SELF withIds(String... ids) {
this.ids = Arrays.asList(ids);
this.ids.clear();
this.ids.addAll(Arrays.asList(ids));
return self();
}
public SELF withIds(Collection<String> ids) {
this.ids = ids;
Assert.notNull(ids, "ids must not be null");
this.ids.clear();
this.ids.addAll(ids);
return self();
}
@ -165,11 +243,17 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
}
public SELF withFields(String... fields) {
this.fields.clear();
Collections.addAll(this.fields, fields);
return self();
}
public SELF withFields(Collection<String> fields) {
Assert.notNull(fields, "fields must not be null");
this.fields.clear();
this.fields.addAll(fields);
return self();
}
@ -184,16 +268,96 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
return self();
}
public SELF withIndicesBoost(List<IndexBoost> indicesBoost) {
public SELF withIndicesBoost(@Nullable List<IndexBoost> indicesBoost) {
this.indicesBoost = indicesBoost;
return self();
}
public SELF withStoredFields(@Nullable List<String> storedFields) {
this.storedFields = storedFields;
return self();
}
public SELF withIndicesBoost(IndexBoost... indicesBoost) {
this.indicesBoost = Arrays.asList(indicesBoost);
return self();
}
public SELF withSearchType(Query.SearchType searchType) {
this.searchType = searchType;
return self();
}
public SELF withTrackTotalHits(@Nullable Boolean trackTotalHits) {
this.trackTotalHits = trackTotalHits;
return self();
}
public SELF withTrackTotalHitsUpTo(@Nullable Integer trackTotalHitsUpTo) {
this.trackTotalHitsUpTo = trackTotalHitsUpTo;
return self();
}
public SELF withTimeout(@Nullable Duration timeout) {
this.timeout = timeout;
return self();
}
public SELF withScrollTime(@Nullable Duration scrollTime) {
this.scrollTime = scrollTime;
return self();
}
public SELF withExplain(boolean explain) {
this.explain = explain;
return self();
}
public SELF withSearchAfter(@Nullable List<Object> searchAfter) {
this.searchAfter = searchAfter;
return self();
}
public SELF withRequestCache(@Nullable Boolean requestCache) {
this.requestCache = requestCache;
return self();
}
public SELF withIdsWithRouting(List<Query.IdWithRouting> idsWithRouting) {
Assert.notNull(idsWithRouting, "idsWithRouting must not be null");
this.idsWithRouting.clear();
this.idsWithRouting.addAll(idsWithRouting);
return self();
}
public SELF withRuntimeFields(List<RuntimeField> runtimeFields) {
Assert.notNull(runtimeFields, "runtimeFields must not be null");
this.runtimeFields.clear();
this.runtimeFields.addAll(runtimeFields);
return self();
}
public SELF withRescorerQueries(List<RescorerQuery> rescorerQueries) {
Assert.notNull(rescorerQueries, "rescorerQueries must not be null");
this.rescorerQueries.clear();
this.rescorerQueries.addAll(rescorerQueries);
return self();
}
public SELF withRescorerQuery(RescorerQuery rescorerQuery) {
Assert.notNull(rescorerQuery, "rescorerQuery must not be null");
this.rescorerQueries.add(rescorerQuery);
return self();
}
public abstract Q build();
private SELF self() {

View File

@ -169,7 +169,7 @@ public class ElasticsearchELCIntegrationTests extends ElasticsearchIntegrationTe
.filter(f -> f.exists(e -> e.field("rate"))) //
.should(s -> s.term(t -> t.field("message").value("message"))) //
)) //
.withResorerQuery( //
.withRescorerQuery( //
new RescorerQuery(NativeQuery.builder() //
.withQuery(q -> q //
.functionScore(fs -> fs //