赞
踩
/** * 查询 * @param index 索引 * @param type 数据类型 * @param searchParam 搜索条件 * @return */ public <T> List<T> search(String index, Class<T> type, SearchParam searchParam) { String keyword = searchParam.getKeyword(); System.out.println("keyword = " + keyword); //构建搜索条件 SearchRequest request = new SearchRequest(index); SearchSourceBuilder builder = new SearchSourceBuilder(); // builder.query(QueryBuilders.termQuery("brand",keyword)); // builder.query(QueryBuilders.matchPhraseQuery ("name",keyword)); builder.query(QueryBuilders.multiMatchQuery(keyword,"tester","name","brand")); request.source(builder); System.out.println("request = " + request); List<T> entityList = new ArrayList<>(); try { SearchResponse response = client.search(request, ElasticConfig.COMMON_OPTIONS); System.out.println("response = " + response.toString()); SearchHits hits = response.getHits(); SearchHit[] searchHits = hits.getHits(); Arrays.stream(searchHits).forEach(hit -> { Map<String, Object> sourceAsMap = hit.getSourceAsMap(); T t = null; try { t = BeanUtil.fillBeanWithMap(sourceAsMap, type.newInstance(), false); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } entityList.add(t); }); } catch (IOException e) { e.printStackTrace(); } return entityList; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。