From 23fa646cd6db88a0e90bb04c9f694e7326f410c6 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <773861846@qq.com> Date: Tue, 24 May 2022 17:00:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20ES=20=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=9A=84=E7=9B=B8=E5=85=B3=20BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../novel/dto/req/BookSearchReqDto.java | 8 +- .../xxyopen/novel/service/SearchService.java | 1 - .../service/impl/EsSearchServiceImpl.java | 118 ++++++++++-------- src/main/resources/mapper/BookInfoMapper.xml | 8 +- 4 files changed, 72 insertions(+), 63 deletions(-) diff --git a/src/main/java/io/github/xxyopen/novel/dto/req/BookSearchReqDto.java b/src/main/java/io/github/xxyopen/novel/dto/req/BookSearchReqDto.java index b9a6e09..0c34248 100644 --- a/src/main/java/io/github/xxyopen/novel/dto/req/BookSearchReqDto.java +++ b/src/main/java/io/github/xxyopen/novel/dto/req/BookSearchReqDto.java @@ -24,7 +24,7 @@ public class BookSearchReqDto extends PageReqDto { /** * 作品方向 */ - private Byte workDirection; + private Integer workDirection; /** * 分类ID @@ -34,12 +34,12 @@ public class BookSearchReqDto extends PageReqDto { /** * 是否收费,1:收费,0:免费 */ - private Byte isVip; + private Integer isVip; /** * 小说更新状态,0:连载中,1:已完结 */ - private Byte bookStatus; + private Integer bookStatus; /** * 字数最小值 @@ -64,5 +64,5 @@ public class BookSearchReqDto extends PageReqDto { /** * 排序字段 */ - private String sort = "last_chapter_update_time desc"; + private String sort; } diff --git a/src/main/java/io/github/xxyopen/novel/service/SearchService.java b/src/main/java/io/github/xxyopen/novel/service/SearchService.java index 6707ce8..bc1bf00 100644 --- a/src/main/java/io/github/xxyopen/novel/service/SearchService.java +++ b/src/main/java/io/github/xxyopen/novel/service/SearchService.java @@ -21,5 +21,4 @@ public interface SearchService { */ RestResp> searchBooks(BookSearchReqDto condition); - } diff --git a/src/main/java/io/github/xxyopen/novel/service/impl/EsSearchServiceImpl.java b/src/main/java/io/github/xxyopen/novel/service/impl/EsSearchServiceImpl.java index 3eec0d0..c0eb606 100644 --- a/src/main/java/io/github/xxyopen/novel/service/impl/EsSearchServiceImpl.java +++ b/src/main/java/io/github/xxyopen/novel/service/impl/EsSearchServiceImpl.java @@ -2,8 +2,9 @@ package io.github.xxyopen.novel.service.impl; import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.SortOrder; -import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery; +import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery; import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery; +import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery; import co.elastic.clients.elasticsearch.core.SearchRequest; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.search.Hit; @@ -49,14 +50,18 @@ public class EsSearchServiceImpl implements SearchService { SearchRequest.Builder searchBuilder = s.index(EsConsts.IndexEnum.BOOK.getName()); buildSearchCondition(condition, searchBuilder); - - searchBuilder.sort(o -> - o.field(f -> f.field(StringUtils - .underlineToCamel(condition.getSort().split(" ")[0])) - .order(SortOrder.Desc)) - ) - .from((condition.getPageNum() - 1) * condition.getPageSize()) + // 排序 + if (!StringUtils.isBlank(condition.getSort())) { + searchBuilder.sort(o -> + o.field(f -> f.field(StringUtils + .underlineToCamel(condition.getSort().split(" ")[0])) + .order(SortOrder.Desc)) + ); + } + // 分页 + searchBuilder.from((condition.getPageNum() - 1) * condition.getPageSize()) .size(condition.getPageSize()); + return searchBuilder; }, EsBookDto.class @@ -85,57 +90,60 @@ public class EsSearchServiceImpl implements SearchService { } private void buildSearchCondition(BookSearchReqDto condition, SearchRequest.Builder searchBuilder) { - if (!StringUtils.isBlank(condition.getKeyword())) { - searchBuilder.query(q -> q.match(t -> t - .field("bookName") - .query(condition.getKeyword()) - .boost(2.0f) - .field("authorName") - .query(condition.getKeyword()) - .boost(1.8f) - //.field("categoryName") - //.query(condition.getKeyword()) - //.boost(1.0f) - //.field("bookDesc") - //.query(condition.getKeyword()) - //.boost(0.1f) - ) - ); - } - if (Objects.nonNull(condition.getWorkDirection())) { - searchBuilder.query(MatchQuery.of(m -> m - .field("workDirection") - .query(condition.getWorkDirection()) - )._toQuery()); - } + BoolQuery boolQuery = BoolQuery.of(b -> { - if (Objects.nonNull(condition.getCategoryId())) { - searchBuilder.query(MatchQuery.of(m -> m - .field("categoryId") - .query(condition.getCategoryId()) - )._toQuery()); - } + if (!StringUtils.isBlank(condition.getKeyword())) { + // 关键词匹配 + b.must((q -> q.multiMatch(t -> t + .fields("bookName^2","authorName^1.8","bookDesc^0.1") + .query(condition.getKeyword()) + ) + )); + } - if (Objects.nonNull(condition.getWordCountMin())) { - searchBuilder.query(RangeQuery.of(m -> m - .field("wordCount") - .gte(JsonData.of(condition.getWordCountMin())) - )._toQuery()); - } + // 精确查询 + if (Objects.nonNull(condition.getWorkDirection())) { + b.must(TermQuery.of(m -> m + .field("workDirection") + .value(condition.getWorkDirection()) + )._toQuery()); + } - if (Objects.nonNull(condition.getWordCountMax())) { - searchBuilder.query(RangeQuery.of(m -> m - .field("wordCount") - .lt(JsonData.of(condition.getWordCountMax())) - )._toQuery()); - } + if (Objects.nonNull(condition.getCategoryId())) { + b.must(TermQuery.of(m -> m + .field("categoryId") + .value(condition.getCategoryId()) + )._toQuery()); + } + + // 范围查询 + if (Objects.nonNull(condition.getWordCountMin())) { + b.must(RangeQuery.of(m -> m + .field("wordCount") + .gte(JsonData.of(condition.getWordCountMin())) + )._toQuery()); + } + + if (Objects.nonNull(condition.getWordCountMax())) { + b.must(RangeQuery.of(m -> m + .field("wordCount") + .lt(JsonData.of(condition.getWordCountMax())) + )._toQuery()); + } + + if (Objects.nonNull(condition.getUpdateTimeMin())) { + b.must(RangeQuery.of(m -> m + .field("lastChapterUpdateTime") + .gte(JsonData.of(condition.getUpdateTimeMin().getTime())) + )._toQuery()); + } + + return b; + + }); + + searchBuilder.query(q -> q.bool(boolQuery)); - if (Objects.nonNull(condition.getUpdateTimeMin())) { - searchBuilder.query(RangeQuery.of(m -> m - .field("lastChapterUpdateTime") - .gte(JsonData.of(condition.getUpdateTimeMin().getTime())) - )._toQuery()); - } } } diff --git a/src/main/resources/mapper/BookInfoMapper.xml b/src/main/resources/mapper/BookInfoMapper.xml index 28d6d3c..b6dde7d 100644 --- a/src/main/resources/mapper/BookInfoMapper.xml +++ b/src/main/resources/mapper/BookInfoMapper.xml @@ -7,7 +7,8 @@ id,category_id,category_name,book_name,author_id,author_name,word_count,last_chapter_name from book_info where word_count > 0 - and (book_name like concat('%',#{condition.keyword},'%') or author_name like concat('%',#{condition.keyword},'%')) + and (book_name like concat('%',#{condition.keyword},'%') or author_name like + concat('%',#{condition.keyword},'%')) and work_direction = #{condition.workDirection} @@ -30,8 +31,9 @@ and last_chapter_update_time >= #{condition.updateTimeMin} - - order by ${condition.sort} + + order by ${condition.sort} +