mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-24 04:52:12 +00:00
Add missing properties to BaseQueryBuilder.
Original Pull Request #2251 Closes #2250
This commit is contained in:
parent
acf02a1dc9
commit
e71c66949a
@ -46,7 +46,6 @@ public class NativeQuery extends BaseQuery {
|
|||||||
@Nullable private Suggester suggester;
|
@Nullable private Suggester suggester;
|
||||||
@Nullable private FieldCollapse fieldCollapse;
|
@Nullable private FieldCollapse fieldCollapse;
|
||||||
private List<ScriptedField> scriptedFields = Collections.emptyList();
|
private List<ScriptedField> scriptedFields = Collections.emptyList();
|
||||||
private List<RescorerQuery> rescorerQueries = Collections.emptyList();
|
|
||||||
|
|
||||||
public NativeQuery(NativeQueryBuilder builder) {
|
public NativeQuery(NativeQueryBuilder builder) {
|
||||||
super(builder);
|
super(builder);
|
||||||
@ -56,7 +55,6 @@ public class NativeQuery extends BaseQuery {
|
|||||||
this.suggester = builder.getSuggester();
|
this.suggester = builder.getSuggester();
|
||||||
this.fieldCollapse = builder.getFieldCollapse();
|
this.fieldCollapse = builder.getFieldCollapse();
|
||||||
this.scriptedFields = builder.getScriptedFields();
|
this.scriptedFields = builder.getScriptedFields();
|
||||||
this.rescorerQueries = builder.getRescorerQueries();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public NativeQuery(@Nullable Query query) {
|
public NativeQuery(@Nullable Query query) {
|
||||||
@ -94,9 +92,4 @@ public class NativeQuery extends BaseQuery {
|
|||||||
public List<ScriptedField> getScriptedFields() {
|
public List<ScriptedField> getScriptedFields() {
|
||||||
return scriptedFields;
|
return scriptedFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<RescorerQuery> getRescorerQueries() {
|
|
||||||
return rescorerQueries;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,6 @@ public class NativeQueryBuilder extends BaseQueryBuilder<NativeQuery, NativeQuer
|
|||||||
@Nullable private Suggester suggester;
|
@Nullable private Suggester suggester;
|
||||||
@Nullable private FieldCollapse fieldCollapse;
|
@Nullable private FieldCollapse fieldCollapse;
|
||||||
private final List<ScriptedField> scriptedFields = new ArrayList<>();
|
private final List<ScriptedField> scriptedFields = new ArrayList<>();
|
||||||
private List<RescorerQuery> rescorerQueries = new ArrayList<>();
|
|
||||||
|
|
||||||
public NativeQueryBuilder() {}
|
public NativeQueryBuilder() {}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -77,10 +75,6 @@ public class NativeQueryBuilder extends BaseQueryBuilder<NativeQuery, NativeQuer
|
|||||||
return scriptedFields;
|
return scriptedFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<RescorerQuery> getRescorerQueries() {
|
|
||||||
return rescorerQueries;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NativeQueryBuilder withQuery(Query query) {
|
public NativeQueryBuilder withQuery(Query query) {
|
||||||
|
|
||||||
Assert.notNull(query, "query must not be null");
|
Assert.notNull(query, "query must not be null");
|
||||||
@ -135,14 +129,6 @@ public class NativeQueryBuilder extends BaseQueryBuilder<NativeQuery, NativeQuer
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NativeQueryBuilder withResorerQuery(RescorerQuery resorerQuery) {
|
|
||||||
|
|
||||||
Assert.notNull(resorerQuery, "resorerQuery must not be null");
|
|
||||||
|
|
||||||
this.rescorerQueries.add(resorerQuery);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NativeQuery build() {
|
public NativeQuery build() {
|
||||||
return new NativeQuery(this);
|
return new NativeQuery(this);
|
||||||
}
|
}
|
||||||
|
@ -47,8 +47,8 @@ import org.springframework.util.Assert;
|
|||||||
*/
|
*/
|
||||||
public class BaseQuery implements Query {
|
public class BaseQuery implements Query {
|
||||||
|
|
||||||
protected Pageable pageable = DEFAULT_PAGE;
|
|
||||||
@Nullable protected Sort sort;
|
@Nullable protected Sort sort;
|
||||||
|
protected Pageable pageable = DEFAULT_PAGE;
|
||||||
protected List<String> fields = new ArrayList<>();
|
protected List<String> fields = new ArrayList<>();
|
||||||
@Nullable protected List<String> storedFields;
|
@Nullable protected List<String> storedFields;
|
||||||
@Nullable protected SourceFilter sourceFilter;
|
@Nullable protected SourceFilter sourceFilter;
|
||||||
@ -67,11 +67,11 @@ public class BaseQuery implements Query {
|
|||||||
@Nullable protected Duration timeout;
|
@Nullable protected Duration timeout;
|
||||||
private boolean explain = false;
|
private boolean explain = false;
|
||||||
@Nullable protected List<Object> searchAfter;
|
@Nullable protected List<Object> searchAfter;
|
||||||
|
@Nullable protected List<IndexBoost> indicesBoost;
|
||||||
protected List<RescorerQuery> rescorerQueries = new ArrayList<>();
|
protected List<RescorerQuery> rescorerQueries = new ArrayList<>();
|
||||||
@Nullable protected Boolean requestCache;
|
@Nullable protected Boolean requestCache;
|
||||||
protected List<IdWithRouting> idsWithRouting = Collections.emptyList();
|
protected List<IdWithRouting> idsWithRouting = Collections.emptyList();
|
||||||
protected final List<RuntimeField> runtimeFields = new ArrayList<>();
|
protected final List<RuntimeField> runtimeFields = new ArrayList<>();
|
||||||
@Nullable protected List<IndexBoost> indicesBoost;
|
|
||||||
|
|
||||||
public BaseQuery() {}
|
public BaseQuery() {}
|
||||||
|
|
||||||
@ -79,17 +79,28 @@ public class BaseQuery implements Query {
|
|||||||
this.sort = builder.getSort();
|
this.sort = builder.getSort();
|
||||||
// do a setPageable after setting the sort, because the pageable may contain an additional sort
|
// 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.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.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.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.indicesBoost = builder.getIndicesBoost();
|
||||||
|
this.rescorerQueries = builder.getRescorerQueries();
|
||||||
|
this.requestCache = builder.getRequestCache();
|
||||||
|
this.idsWithRouting = builder.getIdsWithRouting();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core.query;
|
package org.springframework.data.elasticsearch.core.query;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -23,7 +24,9 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.data.elasticsearch.core.RuntimeField;
|
||||||
import org.springframework.lang.Nullable;
|
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
|
* 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>> {
|
public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQueryBuilder<Q, SELF>> {
|
||||||
|
|
||||||
@Nullable private Pageable pageable;
|
|
||||||
@Nullable private Sort sort;
|
@Nullable private Sort sort;
|
||||||
@Nullable private Integer maxResults;
|
@Nullable private Pageable pageable;
|
||||||
@Nullable private Collection<String> ids;
|
private final List<String> fields = new ArrayList<>();
|
||||||
private boolean trackScores;
|
@Nullable private List<String> storedFields;
|
||||||
@Nullable protected IndicesOptions indicesOptions;
|
|
||||||
private float minScore;
|
|
||||||
@Nullable private String preference;
|
|
||||||
@Nullable private SourceFilter sourceFilter;
|
@Nullable private SourceFilter sourceFilter;
|
||||||
private List<String> fields = new ArrayList<>();
|
private float minScore;
|
||||||
@Nullable protected HighlightQuery highlightQuery;
|
private final Collection<String> ids = new ArrayList<>();
|
||||||
@Nullable private String route;
|
@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;
|
@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
|
@Nullable
|
||||||
public Pageable getPageable() {
|
public Pageable getPageable() {
|
||||||
return pageable;
|
return pageable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getFields() {
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Sort getSort() {
|
public List<String> getStoredFields() {
|
||||||
return sort;
|
return storedFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -91,10 +117,6 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
|||||||
return sourceFilter;
|
return sourceFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getFields() {
|
|
||||||
return fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public HighlightQuery getHighlightQuery() {
|
public HighlightQuery getHighlightQuery() {
|
||||||
return highlightQuery;
|
return highlightQuery;
|
||||||
@ -110,6 +132,56 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
|||||||
return indicesBoost;
|
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) {
|
public SELF withPageable(Pageable pageable) {
|
||||||
this.pageable = pageable;
|
this.pageable = pageable;
|
||||||
return self();
|
return self();
|
||||||
@ -130,12 +202,18 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SELF withIds(String... ids) {
|
public SELF withIds(String... ids) {
|
||||||
this.ids = Arrays.asList(ids);
|
|
||||||
|
this.ids.clear();
|
||||||
|
this.ids.addAll(Arrays.asList(ids));
|
||||||
return self();
|
return self();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SELF withIds(Collection<String> ids) {
|
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();
|
return self();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,11 +243,17 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SELF withFields(String... fields) {
|
public SELF withFields(String... fields) {
|
||||||
|
|
||||||
|
this.fields.clear();
|
||||||
Collections.addAll(this.fields, fields);
|
Collections.addAll(this.fields, fields);
|
||||||
return self();
|
return self();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SELF withFields(Collection<String> fields) {
|
public SELF withFields(Collection<String> fields) {
|
||||||
|
|
||||||
|
Assert.notNull(fields, "fields must not be null");
|
||||||
|
|
||||||
|
this.fields.clear();
|
||||||
this.fields.addAll(fields);
|
this.fields.addAll(fields);
|
||||||
return self();
|
return self();
|
||||||
}
|
}
|
||||||
@ -184,16 +268,96 @@ public abstract class BaseQueryBuilder<Q extends BaseQuery, SELF extends BaseQue
|
|||||||
return self();
|
return self();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SELF withIndicesBoost(List<IndexBoost> indicesBoost) {
|
public SELF withIndicesBoost(@Nullable List<IndexBoost> indicesBoost) {
|
||||||
this.indicesBoost = indicesBoost;
|
this.indicesBoost = indicesBoost;
|
||||||
return self();
|
return self();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SELF withStoredFields(@Nullable List<String> storedFields) {
|
||||||
|
this.storedFields = storedFields;
|
||||||
|
return self();
|
||||||
|
}
|
||||||
|
|
||||||
public SELF withIndicesBoost(IndexBoost... indicesBoost) {
|
public SELF withIndicesBoost(IndexBoost... indicesBoost) {
|
||||||
this.indicesBoost = Arrays.asList(indicesBoost);
|
this.indicesBoost = Arrays.asList(indicesBoost);
|
||||||
return self();
|
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();
|
public abstract Q build();
|
||||||
|
|
||||||
private SELF self() {
|
private SELF self() {
|
||||||
|
@ -169,7 +169,7 @@ public class ElasticsearchELCIntegrationTests extends ElasticsearchIntegrationTe
|
|||||||
.filter(f -> f.exists(e -> e.field("rate"))) //
|
.filter(f -> f.exists(e -> e.field("rate"))) //
|
||||||
.should(s -> s.term(t -> t.field("message").value("message"))) //
|
.should(s -> s.term(t -> t.field("message").value("message"))) //
|
||||||
)) //
|
)) //
|
||||||
.withResorerQuery( //
|
.withRescorerQuery( //
|
||||||
new RescorerQuery(NativeQuery.builder() //
|
new RescorerQuery(NativeQuery.builder() //
|
||||||
.withQuery(q -> q //
|
.withQuery(q -> q //
|
||||||
.functionScore(fs -> fs //
|
.functionScore(fs -> fs //
|
||||||
|
Loading…
x
Reference in New Issue
Block a user