跨多个索引的Elasticsearch搜索-忽略不存在的索引

SDe*_*kov 2 elasticsearch angularjs

我有弹性簇,其中我的索引包含当前日期-例如:

example-idex-2016-07-26 --> exists
example-idex-2016-07-25 --> exists
example-idex-2016-07-24 --> doesn't exist (weekend)
...
Run Code Online (Sandbox Code Playgroud)

是否可以跨多个索引查询而忽略不存在的索引。例如,这WORKS

return elastic.search({
        index: [
            "example-idex-2016-07-26",
            "example-idex-2016-07-25"],
        ],
        ...
});
Run Code Online (Sandbox Code Playgroud)

而这会返回404

return elastic.search({
        index: [
            "example-idex-2016-07-25",
            "example-idex-2016-07-24"], //this doesn't exist
        ],
        ...
});
Run Code Online (Sandbox Code Playgroud)

我希望第二个示例仅从25日起返回文档。

Val*_*Val 5

如果您使用通配符example-idex-2016-07-*,则无需在意,ES会找出匹配的索引。

如果您真的想枚举索引,可以ignoreUnavailable: truesearch调用中指定:

return elastic.search({
        index: [
            "example-idex-2016-07-25",
            "example-idex-2016-07-24"], //this doesn't exist
        ],
        ignoreUnavailable: true,
        ...
});
Run Code Online (Sandbox Code Playgroud)

或者,您也可以使用索引别名并仅查询该别名。在创建新索引时,您还将该别名添加到索引中。这样做的好处是您的客户端代码不需要更改,并且始终仅查询别名,即隐式地具有该别名的所有索引。

  • 如果您真的愿意,您还可以在查询中指定 [`ignoreUnavailable: true`](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html)。 (2认同)