Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search handling missing indices

I would like to know if there is a way to specify to elastic search that I don't mind missing or erroneous indices on my search query. In other words I have a query which tries to query 7 different indices but one of them might be missing depending on the circumstances. What I want to know is that if there is a way to say, forget the broken one and get me the results of the other 6 indices?

    SearchRequestBuilder builder = elasticsearchClient.getClient().prepareSearch(indices)
            .setQuery(Query.buildQueryFrom(term1, term2))
            .addAggregation(AggregationBuilders.terms('term')
                                        .field('field')
                                        .shardSize(shardSize)
                                        .size(size)
                                        .minDocCount(minCount));

As an example query you can find the above one.

like image 805
ralzaul Avatar asked Dec 15 '14 10:12

ralzaul


1 Answers

Take a look at the ignore_unavailable option, 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 searches (among other multi index operations).

It is exposed in the Java API by IndicesOptions. Browsing through the source code, I found there is a setIndicesOptions() method on the SearchRequestBuilder used in the example. You need to pass it an instance of IndicesOptions.

There are various static factory methods on the IndicesOptions class for building an instance with your specific desired options. You would probably benefit from using the more convenient lenientExpandOpen() factory method (or the deprecated version, 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 example query which 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());
like image 157
BrookeB Avatar answered Oct 04 '22 13:10

BrookeB