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
|
* 分类ID
|
||||||
@ -34,12 +34,12 @@ public class BookSearchReqDto extends PageReqDto {
|
|||||||
/**
|
/**
|
||||||
* 是否收费,1:收费,0:免费
|
* 是否收费,1:收费,0:免费
|
||||||
*/
|
*/
|
||||||
private Byte isVip;
|
private Integer isVip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小说更新状态,0:连载中,1:已完结
|
* 小说更新状态,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);
|
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.ElasticsearchClient;
|
||||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
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.RangeQuery;
|
||||||
|
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
|
||||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||||
import co.elastic.clients.elasticsearch.core.search.Hit;
|
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());
|
SearchRequest.Builder searchBuilder = s.index(EsConsts.IndexEnum.BOOK.getName());
|
||||||
buildSearchCondition(condition, searchBuilder);
|
buildSearchCondition(condition, searchBuilder);
|
||||||
|
// 排序
|
||||||
searchBuilder.sort(o ->
|
if (!StringUtils.isBlank(condition.getSort())) {
|
||||||
o.field(f -> f.field(StringUtils
|
searchBuilder.sort(o ->
|
||||||
.underlineToCamel(condition.getSort().split(" ")[0]))
|
o.field(f -> f.field(StringUtils
|
||||||
.order(SortOrder.Desc))
|
.underlineToCamel(condition.getSort().split(" ")[0]))
|
||||||
)
|
.order(SortOrder.Desc))
|
||||||
.from((condition.getPageNum() - 1) * condition.getPageSize())
|
);
|
||||||
|
}
|
||||||
|
// 分页
|
||||||
|
searchBuilder.from((condition.getPageNum() - 1) * condition.getPageSize())
|
||||||
.size(condition.getPageSize());
|
.size(condition.getPageSize());
|
||||||
|
|
||||||
return searchBuilder;
|
return searchBuilder;
|
||||||
},
|
},
|
||||||
EsBookDto.class
|
EsBookDto.class
|
||||||
@ -85,57 +90,60 @@ public class EsSearchServiceImpl implements SearchService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void buildSearchCondition(BookSearchReqDto condition, SearchRequest.Builder searchBuilder) {
|
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())) {
|
BoolQuery boolQuery = BoolQuery.of(b -> {
|
||||||
searchBuilder.query(MatchQuery.of(m -> m
|
|
||||||
.field("workDirection")
|
|
||||||
.query(condition.getWorkDirection())
|
|
||||||
)._toQuery());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Objects.nonNull(condition.getCategoryId())) {
|
if (!StringUtils.isBlank(condition.getKeyword())) {
|
||||||
searchBuilder.query(MatchQuery.of(m -> m
|
// 关键词匹配
|
||||||
.field("categoryId")
|
b.must((q -> q.multiMatch(t -> t
|
||||||
.query(condition.getCategoryId())
|
.fields("bookName^2","authorName^1.8","bookDesc^0.1")
|
||||||
)._toQuery());
|
.query(condition.getKeyword())
|
||||||
}
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if (Objects.nonNull(condition.getWordCountMin())) {
|
// 精确查询
|
||||||
searchBuilder.query(RangeQuery.of(m -> m
|
if (Objects.nonNull(condition.getWorkDirection())) {
|
||||||
.field("wordCount")
|
b.must(TermQuery.of(m -> m
|
||||||
.gte(JsonData.of(condition.getWordCountMin()))
|
.field("workDirection")
|
||||||
)._toQuery());
|
.value(condition.getWorkDirection())
|
||||||
}
|
)._toQuery());
|
||||||
|
}
|
||||||
|
|
||||||
if (Objects.nonNull(condition.getWordCountMax())) {
|
if (Objects.nonNull(condition.getCategoryId())) {
|
||||||
searchBuilder.query(RangeQuery.of(m -> m
|
b.must(TermQuery.of(m -> m
|
||||||
.field("wordCount")
|
.field("categoryId")
|
||||||
.lt(JsonData.of(condition.getWordCountMax()))
|
.value(condition.getCategoryId())
|
||||||
)._toQuery());
|
)._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
|
id,category_id,category_name,book_name,author_id,author_name,word_count,last_chapter_name
|
||||||
from book_info where word_count > 0
|
from book_info where word_count > 0
|
||||||
<if test="condition.keyword != null and condition.keyword != ''">
|
<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>
|
||||||
<if test="condition.workDirection != null">
|
<if test="condition.workDirection != null">
|
||||||
and work_direction = #{condition.workDirection}
|
and work_direction = #{condition.workDirection}
|
||||||
@ -30,8 +31,9 @@
|
|||||||
<if test="condition.updateTimeMin != null">
|
<if test="condition.updateTimeMin != null">
|
||||||
and last_chapter_update_time >= #{condition.updateTimeMin}
|
and last_chapter_update_time >= #{condition.updateTimeMin}
|
||||||
</if>
|
</if>
|
||||||
|
<if test="condition.sort != null">
|
||||||
order by ${condition.sort}
|
order by ${condition.sort}
|
||||||
|
</if>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<update id="addVisitCount">
|
<update id="addVisitCount">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user