一次查詢多個索引數據java
es裏能夠這樣寫elasticsearch
GET 索引1,索引2,索引3/_searchui
也能夠這樣spa
給索引建立別名,多個索引可使用一個別名code
POST /_aliases { "actions": [ { "add": { "index": "myindex2", "alias": "myalias" } },{ "add": { "index": "myindex3", "alias": "myalias" } } ] }
或者blog
PUT /myindex2/_alias/myalias
刪除別名索引
POST /_aliases { "actions": [ { "remove": { "index": "myindex3", "alias": "my_index_alias" } } ] }
java查詢多個索引rem
SearchQuery searchQuery = new NativeSearchQueryBuilder() .withIndices("myindex2","myindex4") //能夠直接使用別名 .withQuery(queryBuilder) //.addAggregation(sumBuilder) .build(); List<Map> map=elasticsearchTemplate.query(searchQuery, response -> { SearchHits hits = response.getHits(); List<Map> list=new ArrayList<>(); Arrays.stream(hits.getHits()).forEach(h -> { Map<String, Object> source = h.getSource(); System.out.println(JSONArray.toJSONString(source)); list.add(source); }); return list; });