mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
fix: 修复 ES 搜索的相关 BUG
This commit is contained in:
parent
471a24a330
commit
23fa646cd6
@ -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;
|
||||
}
|
||||
|
@ -21,5 +21,4 @@ public interface SearchService {
|
||||
*/
|
||||
RestResp<PageRespDto<BookInfoRespDto>> searchBooks(BookSearchReqDto condition);
|
||||
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
<if test="condition.keyword != null and condition.keyword != ''">
|
||||
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},'%'))
|
||||
</if>
|
||||
<if test="condition.workDirection != null">
|
||||
and work_direction = #{condition.workDirection}
|
||||
@ -30,8 +31,9 @@
|
||||
<if test="condition.updateTimeMin != null">
|
||||
and last_chapter_update_time >= #{condition.updateTimeMin}
|
||||
</if>
|
||||
|
||||
order by ${condition.sort}
|
||||
<if test="condition.sort != null">
|
||||
order by ${condition.sort}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<update id="addVisitCount">
|
||||
|
Loading…
x
Reference in New Issue
Block a user