spring data elasticsearch多索引查询

一次查询多个索引数据

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=elasticsearchTemplate.query(searchQuery, response -> {
            SearchHits hits = response.getHits();
            List list=new ArrayList<>();
            Arrays.stream(hits.getHits()).forEach(h -> {
                Map source = h.getSource();
                System.out.println(JSONArray.toJSONString(source));
                list.add(source);
            });
            return list;
        });

 

转载于:https://www.cnblogs.com/double-yuan/p/9740494.html

你可能感兴趣的:(spring data elasticsearch多索引查询)