Take a look at the ignore_unavableable parameter, which is part of the multi index syntax. This has been available since at least version 1.3 and allows you to ignore missing or closed indexes when performing a search (among other operations with multiple indexes).
It is displayed in the Java API IndicesOptions
. Looking through the source code, I found that there is a setIndicesOptions()
method on the SearchRequestBuilder used in the example. You need to pass it an instance of IndicesOptions
.
In the IndicesOptions class, there are various static factory methods for instantiating with your desired parameters. It will probably be more profitable for you to use the more convenient lenientExpandOpen()
factory method (or the legacy version of lenient()
, depending on your version), which sets ignore_unavailable = true, allow_no_indices = true and expand_wildcards = open.
Here is a modified version of the sample request that should provide the behavior you are looking for:
SearchRequestBuilder builder = elasticsearchClient.getClient().prepareSearch(indices) .setQuery(Query.buildQueryFrom(term1, term2)) .addAggregation(AggregationBuilders.terms('term') .field('field') .shardSize(shardSize) .size(size) .minDocCount(minCount)) .setIndicesOptions(IndicesOptions.lenientExpandOpen());
source share