From 110c4b447214c7e774ce2bd820e6c7edf8e8f96c Mon Sep 17 00:00:00 2001 From: wayn <1669738430@qq.com> Date: Tue, 11 May 2021 20:10:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=95=86=E5=9F=8E):=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/readme.md b/readme.md index 9920188..4a209b8 100644 --- a/readme.md +++ b/readme.md @@ -49,6 +49,47 @@ threadPoolTaskExecutor.submit(bannerTask); # 4. 最后可以在外部通过FutureTask的get方法异步获取执行结果 List list = bannerTask.get() ``` +#### 3. `ElasticSearch`查询操作,查询包含搜索关键字并且是上架中的商品,在根据指定字段进行排序,最后分页返回 +```java +SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); +BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); +MatchQueryBuilder matchFiler = QueryBuilders.matchQuery("isOnSale", true); +MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", keyword); +MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("keyword", keyword); +boolQueryBuilder.filter(matchFiler).should(matchQuery).should(matchPhraseQueryBuilder).minimumShouldMatch(1); +searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS)); +// 按是否新品排序 +if (isNew) { + searchSourceBuilder.sort(new FieldSortBuilder("isNew").order(SortOrder.DESC)); +} +// 按是否热品排序 +if (isHot) { + searchSourceBuilder.sort(new FieldSortBuilder("isHot").order(SortOrder.DESC)); +} +// 按价格高低排序 +if (isPrice) { + searchSourceBuilder.sort(new FieldSortBuilder("retailPrice").order("asc".equals(orderBy) ? SortOrder.ASC : SortOrder.DESC)); +} +// 按销量排序 +if (isSales) { + searchSourceBuilder.sort(new FieldSortBuilder("sales").order(SortOrder.DESC)); +} +// 筛选新品 +if (filterNew) { + MatchQueryBuilder filterQuery = QueryBuilders.matchQuery("isNew", true); + boolQueryBuilder.filter(filterQuery); +} +// 筛选热品 +if (filterHot) { + MatchQueryBuilder filterQuery = QueryBuilders.matchQuery("isHot", true); + boolQueryBuilder.filter(filterQuery); +} + +searchSourceBuilder.query(boolQueryBuilder); +searchSourceBuilder.from((int) (page.getCurrent() - 1) * (int) page.getSize()); +searchSourceBuilder.size((int) page.getSize()); +List list = elasticDocument.search("goods", searchSourceBuilder, JSONObject.class); +``` - todo ## 文件目录