6

I have elastic cluster where my indexes contain the current date - e.g:

example-idex-2016-07-26 --> exists
example-idex-2016-07-25 --> exists
example-idex-2016-07-24 --> doesn't exist (weekend)
...

Is it possible to query across multiple indexes, ignoring ones that don't exist. For example this WORKS:

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

Whereas this throws back a 404:

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

I would expect the second example to return documents from 25th only.

SDekov
  • 9,276
  • 1
  • 20
  • 50

1 Answers1

8

If you use a wildcard like example-idex-2016-07-* you don't need to care about this and ES will figure out the matching indices.

If you really want to enumerate indices you can specify ignoreUnavailable: true in your search call:

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

Alternatively, you can also use index aliases and query only that alias. When creating a new index, you also add that alias to the index. The good thing about this is that your client code doesn't need to be changed and will always only query the alias, i.e. implicitly all indices having that alias.

Val
  • 207,596
  • 13
  • 358
  • 360
  • This won't work in the case if I want **last week** -e.g. 18th-24th, only the ones that exist. – SDekov Jul 26 '16 at 13:39
  • You can have multiple aliases, `last-month`, `last-week`, etc and query the ones you want. – Val Jul 26 '16 at 13:39
  • 2
    You can also specify [`ignoreUnavailable: true`](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html) in the query if you really want to. – Val Jul 26 '16 at 13:42
  • That is what I was looking for! – SDekov Jul 26 '16 at 13:48