在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參數中設置。