Skip to content

Commit

Permalink
feat: 查询时支持根据部分属性冒号关键词的格式 (#724)
Browse files Browse the repository at this point in the history
* feat: 查询时支持根据部分属性冒号关键词的格式

* fix: 修正dockerfile里的联系人邮件地址

* fix: 修复初始化查询索引时无法初始化所有条目的问题
  • Loading branch information
chivehao authored Nov 10, 2024
1 parent 0196f52 commit e76debf
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

更新日志文档,版本顺序从新到旧,最新版本在最前(上)面。

# 0.19.3

## 优化

- 查询时支持根据部分属性冒号关键词的格式

## 问题修复

- 修正dockerfile里的联系人邮件地址
- 修复初始化查询索引时无法初始化所有条目的问题

# 0.19.2

## 优化
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN java -Djarmode=layertools -jar application.jar extract
###########################################################

FROM eclipse-temurin:17-jre
MAINTAINER li-guohao <git@liguohao.cn>
MAINTAINER chivehao <chivehao@ikaros.run>
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public record SubjectHint(
String summary,
Boolean nsfw,
SubjectType type,
Long airTime,
String cover
String airTime
) {
public static final String ID_FIELD = "subject";

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.19.2
version=0.19.3
23 changes: 23 additions & 0 deletions server/src/main/java/run/ikaros/server/infra/utils/TimeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package run.ikaros.server.infra.utils;


import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {
/**
* 格式化日期时间戳.
*/
public static String formatTimestamp(Long timestamp) {
return formatTimestamp(timestamp, "yyyy-MM-dd");
}

/**
* 格式化日期时间戳.
*/
public static String formatTimestamp(Long timestamp, String pattern) {
Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public IndicesServiceImpl(
public Mono<Void> rebuildSubjectIndices() {
return subjectRepository.findAll()
.flatMap(ReactiveSubjectDocConverter::fromEntity)
.limitRate(100)
.buffer(100)
.limitRate(10)
.buffer(50)
.handle((subjectDocs, sink) -> {
try {
subjectSearchService.rebuild(subjectDocs);
} catch (Exception e) {
sink.error(new RuntimeException(e));
log.error("Rebuild subject indices fail, msg: {}", e.getMessage(), e);
}
})
.then();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.apache.lucene.document.Field.Store.YES;
import static org.apache.lucene.index.IndexWriterConfig.OpenMode.CREATE_OR_APPEND;
import static run.ikaros.api.constant.StringConst.SPACE;
import static run.ikaros.server.infra.utils.TimeUtils.formatTimestamp;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -25,6 +26,7 @@
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
Expand Down Expand Up @@ -62,7 +64,8 @@ public class LuceneSubjectSearchService implements SubjectSearchService, Disposa
*/
public LuceneSubjectSearchService(IkarosProperties ikarosProperties) throws IOException {
analyzer = new IKAnalyzer(true);
var subjectIdxPath = ikarosProperties.getWorkDir().resolve("indices/subjects");
var subjectIdxPath = ikarosProperties.getWorkDir()
.resolve("indices").resolve("subjects");
subjectIndexDir = FSDirectory.open(subjectIdxPath);
}

Expand Down Expand Up @@ -179,6 +182,7 @@ private Document convert(SubjectDoc subjectDoc) {
doc.add(new StringField("name", subjectDoc.getName(), YES));
if (StringUtils.hasText(subjectDoc.getNameCn())) {
doc.add(new StringField("nameCn", subjectDoc.getNameCn(), YES));
doc.add(new StringField("namecn", subjectDoc.getNameCn(), YES));
}
if (StringUtils.hasText(subjectDoc.getInfobox())) {
doc.add(new TextField("infobox", subjectDoc.getInfobox(), YES));
Expand All @@ -188,18 +192,17 @@ private Document convert(SubjectDoc subjectDoc) {
}
doc.add(new StringField("nsfw", String.valueOf(subjectDoc.getNsfw()), YES));
doc.add(new StringField("type", String.valueOf(subjectDoc.getType()), YES));
doc.add(new StringField("airTime", String.valueOf(subjectDoc.getAirTime()), YES));
doc.add(new StringField("cover", String.valueOf(subjectDoc.getCover()), YES));
doc.add(new StringField("airTime", formatTimestamp(subjectDoc.getAirTime()), YES));
doc.add(new StringField("airtime", formatTimestamp(subjectDoc.getAirTime()), YES));
var content = Jsoup.clean(
stripToEmpty(String.valueOf(subjectDoc.getId())) + SPACE
+ stripToEmpty(subjectDoc.getName()) + SPACE
+ stripToEmpty(subjectDoc.getNameCn()) + SPACE
+ stripToEmpty(subjectDoc.getInfobox()) + SPACE
+ stripToEmpty(subjectDoc.getSummary()) + SPACE
+ subjectDoc.getNsfw() + SPACE
+ subjectDoc.getNsfw() + SPACE
+ subjectDoc.getType() + SPACE
+ subjectDoc.getAirTime() + SPACE,
+ formatTimestamp(subjectDoc.getAirTime()) + SPACE,
Safelist.none());
doc.add(new StoredField("content", content));
doc.add(new TextField("searchable", subjectDoc.getName() + content, NO));
Expand All @@ -214,12 +217,19 @@ private SubjectHint convert(Document doc, Highlighter highlighter) {
doc.get("infobox"), doc.get("summary"),
Boolean.valueOf(doc.get("nsfw")),
SubjectType.valueOf(doc.get("type")),
Long.parseLong(doc.get("airTime")),
doc.get("cover")
doc.get("airTime")
);
}

private Query buildQuery(String keyword) throws ParseException {
if (keyword.contains(":")) {
String[] split = keyword.split(":");
if (split.length == 2) {
String field = split[0].trim().toLowerCase();
String value = split[1].trim();
return new TermQuery(new Term(field, value));
}
}
return buildQuery("searchable", keyword);
}

Expand Down

0 comments on commit e76debf

Please sign in to comment.