简体   繁体   中英

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.

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());

Have you tried using Index Aliases ?

Rather than referring to individual aliases you can specify a single index value. Behind this can be several indexes.

Here I'm adding two indexes to the alias and removing the missing / broken one:

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
   "actions" : [
      { "remove" : { "index" : "bad-index", "alias" : "alias-index" } },
      { "add" : { "index" : "good-index1", "alias" : "alias-index" } },
      { "add" : { "index" : "good-index2", "alias" : "alias-index" } }
   ]
}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM