一:先看一下es的语句以及查询结果:
我这边使用的条件是is_device要么是工控要么是资产
二:java代码部分
关于es的操作,java里面不需要添加mapper层,只要在service以及controller层里面操作就OK了
1:在service里面添加接口
1 //首页的工控资产详情在es里面查询结果 2 List<Object> getBaseCurrencyByEs();
2:在实现类里面实现逻辑以及步骤,es查询基本就那7步
1 @Override 2 public List<Object> getBaseCurrencyByEs() { 3 //存放最终数据 4 ArrayList<Object> list = new ArrayList<>(); 5 //1.创建搜索请求对象 6 SearchRequest searchRequest = new SearchRequest("mid_assets_shanxi").types("mid_assets_shanxi"); 7 //2.设置搜索源配置 8 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 9 //3.构建QueryBuilder对象指定查询方式和查询条件 10 QueryBuilder queryBuilder = QueryBuilders.termQuery("is_device.keyword", "通用设备"); 11 //4,将QuseryBuilder对象设置到SearchSourceBuilder对象中 12 searchSourceBuilder.query(queryBuilder); 13 //5,将SearchSourceBuilder设置到SearchRequest中 14 searchRequest.source(searchSourceBuilder); 15 try { 16 //6,调用方法查询数据 17 SearchResponse searchResponse = restHighLevelClient.search(searchRequest); 18 System.err.println("es语句:" + searchRequest); 19 //7,解析返回结果 20 SearchHits hits = searchResponse.getHits(); 21 //得到数据 22 SearchHit[] searchHits = hits.getHits(); 23 for (SearchHit hit : searchHits){ 24 Map<String, Object> result = hit.getSourceAsMap(); 25 list.add(result); 26 } 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } 30 return list; 31 }
3:在controller层里面调用接口
1 @GetMapping("getBaseCurrencyByEs") 2 @ApiOperation("通用资产详情的数据") 3 public Result getBaseCurrencyByEs(){ 4 List<Object> baseIndustrial=bigscreenStatisticsAllService.getBaseCurrencyByEs(); 5 return Result.ok(baseIndustrial); 6 }
三:测试接口是否查询到了数据: