一次查询多个索引数据
es里可以这样写
GET 索引1,索引2,索引3/_search
也可以这样
给索引创建别名,多个索引可以使用一个别名
POST /_aliases { "actions": [ { "add": { "index": "myindex2", "alias": "myalias" } },{ "add": { "index": "myindex3", "alias": "myalias" } } ] }
或者
PUT /myindex2/_alias/myalias
删除别名
POST /_aliases { "actions": [ { "remove": { "index": "myindex3", "alias": "my_index_alias" } } ] }
java查询多个索引
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; });
原文地址:https://www.cnblogs.com/double-yuan/p/9740494.html
时间: 2024-11-10 19:34:30