赞
踩
是一个分页查询
/** * 查询 * @param keyword * @param pageNo * @param pageSize * @return * @throws IOException */ public List<Map<String ,Object>> searchPage(String keyword, int pageNo, int pageSize) throws IOException { // 指定要查询的索引 SearchRequest searchRequest = new SearchRequest("phone_info"); // 设置分页条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); pageNo = pageNo == 0 ? 1 : pageNo; pageSize = pageSize == 0 ? 10 : pageSize; searchSourceBuilder.from(pageNo); searchSourceBuilder.size(pageSize); // 输入的关键字匹配的字段 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title.keyword", keyword); searchSourceBuilder.query(termQueryBuilder); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); // 执行 searchRequest.source(searchSourceBuilder); SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); ArrayList<Map<String,Object>> list = new ArrayList<>(); if (search.getHits().getHits().length!=0){ for (SearchHit documentFields : search.getHits().getHits()) { list.add(documentFields.getSourceAsMap()); } return list; }else { HashMap<String, Object> map = new HashMap<>(); map.put("code",404); map.put("msg","没有相关数据"); list.add(map); return list; } } }
其实前面的都差不多,最大的不同的地方在QueryBuilders
调用的方法。
termQuery(字段.keyword,"value")
:单条件精确查询。termsQuery(字段.keyword,"value1","value2","...")
:多条件精确查询,取并集。rangeQuery("字段").from("start").to("end");
范围查询,查询指定字段处于start到end范围的值,闭区间(不包括start、end)。rangeQuery("字段").from("start", false).to("end", false);
开区间范围查询。rangeQuery("字段").lt("value");
小于value的值,小于等于是lte
、大于是gt
、大于等于是gte
。must
必须、mustNot
必须不、should
类似于or进行连接。wildcardQuery("字段","*value*")
:模糊查询,支持通配符。queryStringQuery("value").field("字段");
不使用通配符的模糊查询,左右匹配。multiMatchQuery("字段","value1","value2")
:多字段模糊查询赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。