Skip to content

Add IndicesOptions to search request. #2642

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
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
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,10 +19,10 @@
import static org.springframework.util.CollectionUtils.*;

import co.elastic.clients.elasticsearch._types.Conflicts;
import co.elastic.clients.elasticsearch._types.ExpandWildcard;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.InlineScript;
import co.elastic.clients.elasticsearch._types.OpType;
import co.elastic.clients.elasticsearch._types.SearchType;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.VersionType;
Expand Down Expand Up @@ -64,12 +64,13 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.RefreshPolicy;
Expand Down Expand Up @@ -109,6 +110,8 @@
@SuppressWarnings("ClassCanBeRecord")
class RequestConverter {

private static final Log LOGGER = LogFactory.getLog(RequestConverter.class);

// the default max result window size of Elasticsearch
public static final Integer INDEX_MAX_RESULT_WINDOW = 10_000;

Expand Down Expand Up @@ -1342,7 +1345,7 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu
}

if (query.getIndicesOptions() != null) {
// new Elasticsearch client does not support the old Indices options, need to be adapted
addIndicesOptions(builder, query.getIndicesOptions());
}

if (query.isLimiting()) {
Expand Down Expand Up @@ -1429,6 +1432,34 @@ private <T> void prepareSearchRequest(Query query, @Nullable String routing, @Nu
}
}

private void addIndicesOptions(SearchRequest.Builder builder, IndicesOptions indicesOptions) {

indicesOptions.getOptions().forEach(option -> {
switch (option) {
case ALLOW_NO_INDICES -> builder.allowNoIndices(true);
case IGNORE_UNAVAILABLE -> builder.ignoreUnavailable(true);
case IGNORE_THROTTLED -> builder.ignoreThrottled(true);
// the following ones aren't supported by the builder
case FORBID_ALIASES_TO_MULTIPLE_INDICES, FORBID_CLOSED_INDICES, IGNORE_ALIASES -> {
if (LOGGER.isWarnEnabled()) {
LOGGER
.warn(String.format("indices option %s is not supported by the Elasticsearch client.", option.name()));
}
}
}
});

builder.expandWildcards(indicesOptions.getExpandWildcards().stream().map(wildcardStates -> {
return switch (wildcardStates) {
case OPEN -> ExpandWildcard.Open;
case CLOSED -> ExpandWildcard.Closed;
case HIDDEN -> ExpandWildcard.Hidden;
case ALL -> ExpandWildcard.All;
case NONE -> ExpandWildcard.None;
};
}).collect(Collectors.toList()));
}

private Rescore getRescore(RescorerQuery rescorerQuery) {

return Rescore.of(r -> r //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ public IndicesOptions(EnumSet<Option> options, EnumSet<WildcardStates> expandWil
this.expandWildcards = expandWildcards;
}

/**
* @since 5.2
*/
public static IndicesOptions ofOptions(EnumSet<Option> options) {
return of(options, EnumSet.noneOf(WildcardStates.class));
}

/**
* @since 5.2
*/
public static IndicesOptions oFExpandWildcards(EnumSet<WildcardStates> expandWildcards) {
return of(EnumSet.noneOf(Option.class), expandWildcards);
}

/**
* @since 5.2
*/
public static IndicesOptions of(EnumSet<Option> options, EnumSet<WildcardStates> expandWildcards) {
return new IndicesOptions(options, expandWildcards);
}

public EnumSet<Option> getOptions() {
return options;
}
Expand Down