在Elasticsearch中,一般的查询都支持多索引。
只有文档API或者别名等不支持多索引操作,因此本篇就翻译一下多索引相关的内容。

首先,先插入几条数据:

$ curl -XPOST localhost:9200/test1/test/1 -d '{"name":"test1"}'
$ curl -XPOST localhost:9200/test1/test/2 -d '{"name":"test1"}'
$ curl -XPOST localhost:9200/test2/test/1 -d '{"name":"test1"}'

这样,当前的ES中就存在两个索引、三条数据!

数组风格

最基本的就是这种数组的风格,比如使用逗号进行分隔:

$ curl -XPOST localhost:9200/test1,test2/_search?pretty -d '{"query":{"match_all":{}}}'
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test1",
      "_type" : "test",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"name":"test1"}
    }, {
      "_index" : "test2",
      "_type" : "test",
      "_id" : "1",
      "_score" : 1.0,
      "_source":{"name":"test1"}
    }, {
      "_index" : "test1",
      "_type" : "test",
      "_id" : "2",
      "_score" : 1.0,
      "_source":{"name":"test1"}
    } ]
  }
}

_all

也可以在索引部分直接使用_all关键字代表匹配所有的索引:

$ curl -XPOST localhost:9200/_all/_search?pretty -d '{"query":{"match_all":{}}}'

通配风格

elasticsearch还支持使用统配的风格,如使用*匹配任意字符:

$ curl -XPOST localhost:9200/test*/_search?pretty -d '{"query":{"match_all":{}}}'

数学表达式风格

最后可以通过add(+)添加一个索引,使用remove(-)去掉一个索引

$ curl -XPOST localhost:9200/-logstash*,+test*/_search?pretty -d '{"query":{"match_all":{}}}'

另外介绍几个文档中常用的参数:

1 ignore_unavailable

是否忽略不可用的索引

2 allow_no_indices

当没有可用的索引时,是否正常

3 expand_wildcards

统配的对象,是open的索引,还是closed的索引

这几个参数都可以在url参数中设置。

相关文章:

  • 2022-12-23
  • 2021-12-31
  • 2021-12-31
  • 2022-01-18
  • 2021-10-23
  • 2021-09-18
  • 2021-07-11
  • 2021-08-03
猜你喜欢
  • 2022-01-21
  • 2021-08-21
  • 2022-12-23
  • 2022-01-19
  • 2021-12-29
  • 2021-12-22
相关资源
相似解决方案