spring data elasticsearch多索引查询

2021-05-17 09:27

阅读:323

标签:hits   pre   add   agg   response   search   int   return   str   

一次查询多个索引数据

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;
        });

 

spring data elasticsearch多索引查询

标签:hits   pre   add   agg   response   search   int   return   str   

原文地址:https://www.cnblogs.com/double-yuan/p/9740494.html


评论


亲,登录后才可以留言!