Skip to content

Improve NativeSearchQueryBuilder by adding convenience methods and modifying existing ones. #1855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.elasticsearch.action.search.SearchType;
Expand Down Expand Up @@ -58,16 +59,16 @@ public class NativeSearchQueryBuilder {
private final List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
private final List<PipelineAggregationBuilder> pipelineAggregationBuilders = new ArrayList<>();
@Nullable private HighlightBuilder highlightBuilder;
@Nullable private HighlightBuilder.Field[] highlightFields;
@Nullable private List<HighlightBuilder.Field> highlightFields = new ArrayList<>();
private Pageable pageable = Pageable.unpaged();
@Nullable private String[] fields;
@Nullable private List<String> fields = new ArrayList<>();
@Nullable private SourceFilter sourceFilter;
@Nullable private CollapseBuilder collapseBuilder;
@Nullable private List<IndexBoost> indicesBoost;
@Nullable private List<IndexBoost> indicesBoost = new ArrayList<>();
@Nullable private SearchTemplateRequestBuilder searchTemplateBuilder;
private float minScore;
private boolean trackScores;
@Nullable private Collection<String> ids;
@Nullable private List<String> ids = new ArrayList<>();
@Nullable private String route;
@Nullable private SearchType searchType;
@Nullable private IndicesOptions indicesOptions;
Expand All @@ -87,11 +88,31 @@ public NativeSearchQueryBuilder withFilter(QueryBuilder filterBuilder) {
return this;
}

/**
* @deprecated use {@link #withSorts(SortBuilder...)} instead.
*/
@Deprecated
public NativeSearchQueryBuilder withSort(SortBuilder<?> sortBuilder) {
this.sortBuilders.add(sortBuilder);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withSorts(Collection<SortBuilder<?>> sortBuilders) {
this.sortBuilders.addAll(sortBuilders);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withSorts(SortBuilder<?>... sortBuilders) {
Collections.addAll(this.sortBuilders, sortBuilders);
return this;
}

public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) {
this.scriptFields.add(scriptField);
return this;
Expand All @@ -110,6 +131,10 @@ public NativeSearchQueryBuilder withCollapseBuilder(@Nullable CollapseBuilder co
return this;
}

/**
* @deprecated use {@link #withAggregations(AbstractAggregationBuilder...)} instead.
*/
@Deprecated
public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder<?> aggregationBuilder) {
this.aggregationBuilders.add(aggregationBuilder);
return this;
Expand All @@ -118,23 +143,73 @@ public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder<?> agg
/**
* @since 4.3
*/
public NativeSearchQueryBuilder withAggregations(Collection<AbstractAggregationBuilder<?>> aggregationBuilders) {
this.aggregationBuilders.addAll(aggregationBuilders);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withAggregations(AbstractAggregationBuilder<?>... aggregationBuilders) {
Collections.addAll(this.aggregationBuilders, aggregationBuilders);
return this;
}

/**
* @deprecated use {@link #withPipelineAggregations(PipelineAggregationBuilder...)} instead.
*/
@Deprecated
public NativeSearchQueryBuilder addAggregation(PipelineAggregationBuilder pipelineAggregationBuilder) {
this.pipelineAggregationBuilders.add(pipelineAggregationBuilder);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withPipelineAggregations(
Collection<PipelineAggregationBuilder> pipelineAggregationBuilders) {
this.pipelineAggregationBuilders.addAll(pipelineAggregationBuilders);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withPipelineAggregations(PipelineAggregationBuilder... pipelineAggregationBuilders) {
Collections.addAll(this.pipelineAggregationBuilders, pipelineAggregationBuilders);
return this;
}

public NativeSearchQueryBuilder withHighlightBuilder(HighlightBuilder highlightBuilder) {
this.highlightBuilder = highlightBuilder;
return this;
}

public NativeSearchQueryBuilder withHighlightFields(HighlightBuilder.Field... highlightFields) {
this.highlightFields = highlightFields;
Collections.addAll(this.highlightFields, highlightFields);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withHighlightFields(Collection<HighlightBuilder.Field> highlightFields) {
this.highlightFields.addAll(highlightFields);
return this;
}

public NativeSearchQueryBuilder withIndicesBoost(List<IndexBoost> indicesBoost) {
this.indicesBoost = indicesBoost;
public NativeSearchQueryBuilder withIndicesBoost(Collection<IndexBoost> indicesBoost) {
this.indicesBoost.addAll(indicesBoost);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withIndicesBoost(IndexBoost... indicesBoost) {
Collections.addAll(this.indicesBoost, indicesBoost);
return this;
}

Expand All @@ -148,8 +223,16 @@ public NativeSearchQueryBuilder withPageable(Pageable pageable) {
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withFields(Collection<String> fields) {
this.fields.addAll(fields);
return this;
}

public NativeSearchQueryBuilder withFields(String... fields) {
this.fields = fields;
Collections.addAll(this.fields, fields);
return this;
}

Expand All @@ -174,7 +257,15 @@ public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
}

public NativeSearchQueryBuilder withIds(Collection<String> ids) {
this.ids = ids;
this.ids.addAll(ids);
return this;
}

/**
* @since 4.3
*/
public NativeSearchQueryBuilder withIds(String... ids) {
Collections.addAll(this.ids, ids);
return this;
}

Expand Down Expand Up @@ -223,14 +314,18 @@ public NativeSearchQueryBuilder withRescorerQuery(RescorerQuery rescorerQuery) {

public NativeSearchQuery build() {

NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders,
highlightBuilder, highlightFields);
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery( //
queryBuilder, //
filterBuilder, //
sortBuilders, //
highlightBuilder, //
highlightFields.toArray(new HighlightBuilder.Field[highlightFields.size()]));

nativeSearchQuery.setPageable(pageable);
nativeSearchQuery.setTrackScores(trackScores);

if (fields != null) {
nativeSearchQuery.addFields(fields);
nativeSearchQuery.setFields(fields);
}

if (sourceFilter != null) {
Expand Down