mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 13:12:10 +00:00
Add IndicesOptions to search request.
Original Pull Request #2642 Closes #2641 Closes #2639
This commit is contained in:
parent
0028d7d550
commit
4e9bbe5045
@ -19,10 +19,10 @@ import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
|
|||||||
import static org.springframework.util.CollectionUtils.*;
|
import static org.springframework.util.CollectionUtils.*;
|
||||||
|
|
||||||
import co.elastic.clients.elasticsearch._types.Conflicts;
|
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.FieldValue;
|
||||||
import co.elastic.clients.elasticsearch._types.InlineScript;
|
import co.elastic.clients.elasticsearch._types.InlineScript;
|
||||||
import co.elastic.clients.elasticsearch._types.OpType;
|
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.SortOptions;
|
||||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||||
import co.elastic.clients.elasticsearch._types.VersionType;
|
import co.elastic.clients.elasticsearch._types.VersionType;
|
||||||
@ -64,12 +64,13 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
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.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
||||||
@ -109,6 +110,8 @@ import org.springframework.util.StringUtils;
|
|||||||
@SuppressWarnings("ClassCanBeRecord")
|
@SuppressWarnings("ClassCanBeRecord")
|
||||||
class RequestConverter {
|
class RequestConverter {
|
||||||
|
|
||||||
|
private static final Log LOGGER = LogFactory.getLog(RequestConverter.class);
|
||||||
|
|
||||||
// the default max result window size of Elasticsearch
|
// the default max result window size of Elasticsearch
|
||||||
public static final Integer INDEX_MAX_RESULT_WINDOW = 10_000;
|
public static final Integer INDEX_MAX_RESULT_WINDOW = 10_000;
|
||||||
|
|
||||||
@ -1342,7 +1345,7 @@ class RequestConverter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (query.getIndicesOptions() != null) {
|
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()) {
|
if (query.isLimiting()) {
|
||||||
@ -1429,6 +1432,34 @@ class RequestConverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
private Rescore getRescore(RescorerQuery rescorerQuery) {
|
||||||
|
|
||||||
return Rescore.of(r -> r //
|
return Rescore.of(r -> r //
|
||||||
|
@ -71,6 +71,27 @@ public class IndicesOptions {
|
|||||||
this.expandWildcards = expandWildcards;
|
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() {
|
public EnumSet<Option> getOptions() {
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user