1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| package com.orchids.elasticsearch.web.controller;
import cn.hutool.core.collection.CollUtil; import cn.hutool.json.JSONUtil; import com.orchids.elasticsearch.web.po.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortOrder; import org.springframework.web.bind.annotation.*;
import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.Map;
@Slf4j @Api(tags = "Restapi") @RestController @RequestMapping("/elastic/rest") @RequiredArgsConstructor public class ElasticRestController { private final RestHighLevelClient client;
public void testAgg() throws IOException { SearchRequest request = new SearchRequest("items"); BoolQueryBuilder bool = QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("category", "手机")) .filter(QueryBuilders.rangeQuery("price").gte(300000)); request.source().query(bool).size(0); request.source().aggregation( AggregationBuilders.terms("brand_agg").field("brand").size(5) ); SearchResponse response = client.search(request, RequestOptions.DEFAULT); Aggregations aggregations = response.getAggregations(); Terms brandTerms = aggregations.get("brand_agg"); List<? extends Terms.Bucket> buckets = brandTerms.getBuckets(); for (Terms.Bucket bucket : buckets) { String brand = bucket.getKeyAsString(); System.out.print("brand = " + brand); long count = bucket.getDocCount(); System.out.println("; count = " + count); } } @ApiOperation("高亮查询") @GetMapping("/high") public List<User> HighlightQuery( @ApiParam(name = "index",value = "索引",required = true) @RequestParam("index")String index, @ApiParam(name = "high",value = "高亮条件",required = true)@RequestParam("high") String high ) throws IOException { SearchRequest request = new SearchRequest(index); request.source().query(QueryBuilders.matchQuery("name", high)); request.source().highlighter( SearchSourceBuilder.highlight() .field("name") .preTags("<em>") .postTags("</em>") ); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return hand(response); } private List<User> hand(SearchResponse response) { SearchHits searchHits = response.getHits(); long total = searchHits.getTotalHits().value; System.out.println("共搜索到" + total + "条数据"); SearchHit[] hits = searchHits.getHits(); List<User> result = new LinkedList<>(); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); User user = JSONUtil.toBean(source, User.class); Map<String, HighlightField> hfs = hit.getHighlightFields(); if (CollUtil.isNotEmpty(hfs)) { HighlightField hf = hfs.get("name"); if (hf != null) { String hfName = hf.getFragments()[0].string(); user.setName(hfName); } } result.add(user); } return result; } @ApiOperation("分页查询") @GetMapping("/page") public List <User> pageQuery( @ApiParam(value = "索引",required = true)@RequestParam("index")String index, @ApiParam(value = "字段条件",required = true)@RequestParam("field") String field, @ApiParam(value = "当前页",required = true)@RequestParam("current")String current, @ApiParam(value = "页大小",required = true)@RequestParam("size")String size) throws IOException {
SearchRequest request = new SearchRequest(index); request.source().query(QueryBuilders.matchQuery("name", field)); request.source().sort("age", SortOrder.ASC); request.source().from((Integer.valueOf(current) - 1) * Integer.valueOf(size)).size(Integer.valueOf(size)); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return handleResponse(response);
} private List <User> handleResponse(SearchResponse response) { SearchHits searchHits = response.getHits(); long total = searchHits.getTotalHits().value; System.out.println("共搜索到" + total + "条数据"); SearchHit[] hits = searchHits.getHits(); List<User> result = new LinkedList<>(); for (SearchHit hit : hits) { String source = hit.getSourceAsString(); User user = JSONUtil.toBean(source, User.class); result.add(user); } return result; } @ApiOperation("复合条件") @GetMapping("/compare") public List<User> compareQuery( @ApiParam(value = "索引",required = true)@RequestParam("index") String index, @ApiParam(value = "名称",required = true)@RequestParam ("name")String name, @ApiParam(value = "性别",required = true)@RequestParam("sex") String sex, @ApiParam(value = "年龄",required = true)@RequestParam("age") String age)throws IOException { SearchRequest request = new SearchRequest(index); BoolQueryBuilder bool = QueryBuilders.boolQuery(); if (name!=null){ bool.must(QueryBuilders.matchQuery("name", name)); } if (sex!=null){ bool.filter(QueryBuilders.termQuery("sex", sex)); } if (age!=null){ bool.filter(QueryBuilders.rangeQuery("age").gt(age)); } request.source().query(bool); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return handleResponse(response); } @ApiOperation("区间查询") @GetMapping("/term") public List<User> rangeQuery( @ApiParam(value = "索引",required = true)@RequestParam String index, @ApiParam(value = "左区间",required = true)@RequestParam("gt") String gt, @ApiParam(value = "右区间",required = true)@RequestParam("lt") String lt, @ApiParam(value = "支持排序的字段",required = true)@RequestParam("field") String field) throws IOException { SearchRequest request = new SearchRequest(index); request.source().query(QueryBuilders.rangeQuery(field).gte(gt).lte(lt)); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return handleResponse(response); } @ApiOperation("多条件查询") @GetMapping("/multi_match") public List<User> multi_matchQuery(@ApiParam(value = "包含条件1" ,required = true)@RequestParam("name1") String args1, @ApiParam(value = "包含条件2",required = true)@RequestParam("name2") String args2) throws IOException { SearchRequest request = new SearchRequest("user"); request.source().query(QueryBuilders.multiMatchQuery("name",args1,args2)); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return handleResponse(response); } @ApiOperation("match查询") @GetMapping("/match") public List<User> matchQuery( @ApiParam(value = "索引",name = "index",required = true) @RequestParam("index")String index, @ApiParam(value ="匹配字段",name = "field",required = true)@RequestParam("field") String field) throws IOException { SearchRequest request = new SearchRequest(index); request.source().query(QueryBuilders.matchQuery("name",field)); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return handleResponse(response); } @ApiOperation("match-all查询") @GetMapping("/match-all") public List<User> SearchMatchAll(@ApiParam(value = "索引",name = "index",required = true) @RequestParam("index")String index) throws IOException { SearchRequest request = new SearchRequest(index); request.source().query(QueryBuilders.matchAllQuery()); SearchResponse response = client.search(request, RequestOptions.DEFAULT); return handleResponse(response); } }
|