springdataelasticsearch多索引查询

编程

一次查询多个索引数据

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;

});

以上是 springdataelasticsearch多索引查询 的全部内容, 来源链接: utcz.com/z/517465.html

回到顶部