简体   繁体   English

弹性搜索处理丢失的索引

[英]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. 换句话说,我有一个查询,它试图查询7个不同的索引,但其中一个可能会丢失,具体取决于具体情况。 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? 我想知道的是,如果有办法说,忘记破碎的那个并得到其他6个指数的结果?

    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 . 看一下ignore_unavailable选项,它是多索引语法的一部分 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). 至少从版本1.3开始就可以使用它,并允许您在执行搜索时忽略丢失或关闭的索引(以及其他多索引操作)。

It is exposed in the Java API by IndicesOptions . 它由IndicesOptions在Java API中IndicesOptions Browsing through the source code, I found there is a setIndicesOptions() method on the SearchRequestBuilder used in the example. 浏览源代码,我发现在示例中使用的SearchRequestBuilder上有一个setIndicesOptions()方法。 You need to pass it an instance of IndicesOptions . 您需要传递一个IndicesOptions实例。

There are various static factory methods on the IndicesOptions class for building an instance with your specific desired options. IndicesOptions类上有各种静态工厂方法,用于使用您特定的所需选项构建实例。 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 . 你可能会使用更方便受益lenientExpandOpen()工厂方法(或已经过时的版本, lenient()这取决于您的版本),其中规定ignore_unavailable = TRUE,allow_no_indices = true,并且expand_wildcards =开放

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" } }
   ]
}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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