Elasticsearch——多索引的使用


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM