elasticsearch常用应用操作,会批量从mysql批量导入数据。
会解决一些mysql不太好写的sql语句,如搜索多模糊查询。
然后,重要特性可以提高查询效率,为搜索而生。 部分编辑
GET _search { "query": { "match_all": {} } } get goods/_search get goods/_search { "query": { "term": { "categoryName": { "value": "手机" } } } } get goods/_search { "query": { "match": { "title": "手机" } }, "size": 500 } get goods/_search { "query": { "wildcard": { "title": { "value": "手*" } } } } get goods/_search { "query": { "regexp": { "title": "(.)*" } } } get goods/_search { "query": { "prefix": { "brandName": { "value": "2dff" } } } } get goods/_search { "query": { "range": { "price": { "gte": 1000, "lte": 1000 } } }, "sort": [ { "price": { "order": "desc" } } ] } get goods/_search get goods/_search { "query": { "query_string": { "fields": ["title","categoryName","brandName"], "query": "手机", "default_operator": "AND" } } } get goods/_search { "query": { "bool": { "filter": [ { "term": { "brandName": { "value": "小米" } } } ] } } } get goods/_search { "query": { "match": { "title": "手机" } }, "highlight": { "fields": { "title": { "pre_tags": "<font color='red'>", "post_tags": "</font>" } } } }
- 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
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; 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.common.text.Text; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.*; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import javax.swing.text.Highlighter; import java.io.IOException; import java.util.*; /** * @author: samxie * @create: 2022/6/10 * @Description: * @FileName: ElasticsearchTest * @History: * @自定义内容: */ @RunWith(SpringRunner.class) @SpringBootTest public class ElasticsearchTest { @Resource private RestHighLevelClient client; private final String INDEX = "goods"; //导入 @Data class Goods { private Long id ; private String title; private Double price; private String stock; private Integer saleNum; private Date createTime; private String categoryName; private String brandName; private Map spec; @JSONField(serialize = false)//在转换JSON时,忽略该字段 private String specStr; public Goods(Long id, String title, Double price, String stock, Integer saleNum, Date createTime, String categoryName, String brandName, Map spec, String specStr) { this.id = id; this.title = title; this.price = price; this.stock = stock; this.saleNum = saleNum; this.createTime = createTime; this.categoryName = categoryName; this.brandName = brandName; this.spec = spec; this.specStr = specStr; } } @Test public void test9() throws IOException { List<Goods> goodsList = new Vector<>(); goodsList.add(new Goods(5L, "这家店", 10.00, null, 123, new Date(), "手机帝国1", "华为pro", null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}")); goodsList.add(new Goods(6L, "这家店2", 1102.00, null, 123, new Date(), "手机22哈", "华为时间皮卡手机pro", null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}")); goodsList.add(new Goods(7L, "这家店33", 1202.00, null, 123, new Date(), "2233", "华为pro", null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}")); goodsList.add(new Goods(8L, "这家店", 1023.00, null, 123, new Date(), "222", "2dffd", null, "{\"机身内存\":\"16G\",\"网络\":\"联通G\"}")); //bulk import BulkRequest bulkRequest = new BulkRequest(); for (Goods goods : goodsList) { String specStr = goods.getSpecStr(); Map map = JSON.parseObject(specStr, Map.class); goods.setSpec(map); String toJSONString = JSON.toJSONString(goods); IndexRequest indexRequest = new IndexRequest(INDEX).source(toJSONString, XContentType.JSON); System.out.println("=======" + JSON.toJSONString(indexRequest)); bulkRequest.add(indexRequest); } System.out.println("----" + bulkRequest); BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(response.status()); } @Test public void test34() throws IOException { //根据条件查询 SearchRequest searchRequest = new SearchRequest("goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); RangeQueryBuilder query = QueryBuilders.rangeQuery("price"); query.gte(200); query.lte(1002); sourceBuilder.query(query); sourceBuilder.sort("price", SortOrder.DESC); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); long value = hits.getTotalHits().value; System.out.println("总数:" + value); SearchHit[] hits1 = hits.getHits(); for (SearchHit searchHit : hits1) { String sourceAsString = searchHit.getSourceAsString(); System.out.println(sourceAsString); } } @Test public void test35() throws IOException { //布尔查询根据条件查询 SearchRequest searchRequest = new SearchRequest("goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米"); boolQuery.must(termQueryBuilder); MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("title", "手机"); boolQuery.filter(matchQuery); RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("price"); rangeQuery.gte(200); rangeQuery.lte(1002); boolQuery.filter(rangeQuery); sourceBuilder.query(rangeQuery); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); long value = hits.getTotalHits().value; System.out.println("总数:" + value); SearchHit[] hits1 = hits.getHits(); for (SearchHit searchHit : hits1) { String sourceAsString = searchHit.getSourceAsString(); System.out.println(sourceAsString); } } @Test public void test99() throws IOException { SearchRequest searchRequest = new SearchRequest("goods"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "手机"); sourceBuilder.query(queryBuilder); TermsAggregationBuilder aggregation = AggregationBuilders.terms("goods_brands").field("brandName").size(100); sourceBuilder.aggregation(aggregation); searchRequest.source(sourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); long total = hits.getTotalHits().value; System.out.println("总数:" + total); Aggregations aggregations = searchResponse.getAggregations(); Map<String, Aggregation> aggregationMap = aggregations.asMap(); Terms goods_brands = (Terms) aggregationMap.get("goods_brands"); List<? extends Terms.Bucket> buckets = goods_brands.getBuckets(); Map<String, Object> map = new HashMap<>(); for (Terms.Bucket bucket : buckets) { System.out.println(bucket.getKey()); map.put(bucket.getKeyAsString(), bucket.getDocCount()); } System.out.println(map); } }
- 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
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
本文使用 文章同步助手 同步
网友评论