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

首先,先插入几条数据:

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

这样,ES中就存在了两个索引,三条数据。

数组格式

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

 
  1. $ curl -XPOST 'http://localhost:9200/test*/_search?pretty' -d '{"query" : {"match_all" : {}}}'
  2. {
  3. "took" : 8,
  4. "timed_out" : false,
  5. "_shards" : {
  6. "total" : 10,
  7. "successful" : 10,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 3,
  12. "max_score" : 1.0,
  13. "hits" : [ {
  14. "_index" : "test1",
  15. "_type" : "test",
  16. "_id" : "2",
  17. "_score" : 1.0,
  18. "_source" : {
  19. "name" : "test1"
  20. }
  21. }, {
  22. "_index" : "test1",
  23. "_type" : "test",
  24. "_id" : "1",
  25. "_score" : 1.0,
  26. "_source" : {
  27. "name" : "test1"
  28. }
  29. }, {
  30. "_index" : "test2",
  31. "_type" : "test",
  32. "_id" : "1",
  33. "_score" : 1.0,
  34. "_source" : {
  35. "name" : "test1"
  36. }
  37. } ]
  38. }
  39. }

_all

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

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

通配风格

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

数学表达式风格

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

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

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

所有的多索引API支持以下url查询字符串参数

1 ignore_unavailable: true/false

控制是否忽略所指定的具体索引不可用,这包括所有不存在或关闭的指数。可以指定真或假。

假设我现在指定查询一个不存在的索引

true:

false:

2 allow_no_indices

当使用匹配表达式没有正确的索引时,是否正常

true:

false:

3 expand_wildcards:true/false

通配的索引时open还是closed

假设现在有logstash-narilog-2015.11.25索引处于关闭,logstash-narilog-2015.11.24索引处于open

open:

closed:

以上几个参数都可以在URI参数中设置,默认参数是根据所使用的API来决定。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐