fix: 搜索main路径跑通

This commit is contained in:
tim
2025-09-26 18:03:25 +08:00
parent 0bc65077df
commit 44addd2a7b
5 changed files with 143 additions and 39 deletions

View File

@@ -24,14 +24,12 @@ public class OpenSearchIndexer implements SearchIndexer {
builder.index(index).id(document.entityId().toString()).document(document)
);
IndexResponse response = client.index(request);
if (log.isDebugEnabled()) {
log.debug(
"Indexed document {} into {} with result {}",
document.entityId(),
index,
response.result()
);
}
log.info(
"Indexed document {} into {} with result {}",
document.entityId(),
index,
response.result()
);
} catch (IOException e) {
log.warn("Failed to index document {} into {}", document.entityId(), index, e);
}

View File

@@ -1,6 +1,5 @@
package com.openisle.search;
import java.time.Instant;
import java.util.List;
public record SearchDocument(
@@ -12,5 +11,5 @@ public record SearchDocument(
String category,
List<String> tags,
Long postId,
Instant createdAt
Long createdAt
) {}

View File

@@ -5,7 +5,6 @@ import com.openisle.model.Comment;
import com.openisle.model.Post;
import com.openisle.model.Tag;
import com.openisle.model.User;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collections;
@@ -38,7 +37,7 @@ public final class SearchDocumentFactory {
post.getCategory() != null ? post.getCategory().getName() : null,
tags,
post.getId(),
toInstant(post.getCreatedAt())
toEpochMillis(post.getCreatedAt())
);
}
@@ -64,7 +63,7 @@ public final class SearchDocumentFactory {
post != null && post.getCategory() != null ? post.getCategory().getName() : null,
tags,
post != null ? post.getId() : null,
toInstant(comment.getCreatedAt())
toEpochMillis(comment.getCreatedAt())
);
}
@@ -81,7 +80,7 @@ public final class SearchDocumentFactory {
null,
Collections.emptyList(),
null,
toInstant(user.getCreatedAt())
toEpochMillis(user.getCreatedAt())
);
}
@@ -115,11 +114,14 @@ public final class SearchDocumentFactory {
null,
Collections.emptyList(),
null,
toInstant(tag.getCreatedAt())
toEpochMillis(tag.getCreatedAt())
);
}
private static Instant toInstant(LocalDateTime time) {
return time == null ? null : time.atZone(ZoneId.systemDefault()).toInstant();
private static Long toEpochMillis(LocalDateTime time) {
if (time == null) {
return null;
}
return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}

View File

@@ -53,7 +53,10 @@ public class SearchIndexInitializer {
.properties("category", Property.of(p -> p.keyword(k -> k)))
.properties("tags", Property.of(p -> p.keyword(k -> k)))
.properties("postId", Property.of(p -> p.long_(l -> l)))
.properties("createdAt", Property.of(p -> p.date(d -> d)))
.properties(
"createdAt",
Property.of(p -> p.date(d -> d.format("strict_date_optional_time||epoch_millis")))
)
);
}
@@ -67,7 +70,10 @@ public class SearchIndexInitializer {
.properties("category", Property.of(p -> p.keyword(k -> k)))
.properties("tags", Property.of(p -> p.keyword(k -> k)))
.properties("postId", Property.of(p -> p.long_(l -> l)))
.properties("createdAt", Property.of(p -> p.date(d -> d)))
.properties(
"createdAt",
Property.of(p -> p.date(d -> d.format("strict_date_optional_time||epoch_millis")))
)
);
}
@@ -77,7 +83,10 @@ public class SearchIndexInitializer {
.properties("type", Property.of(p -> p.keyword(k -> k)))
.properties("title", Property.of(p -> p.text(t -> t)))
.properties("content", Property.of(p -> p.text(t -> t)))
.properties("createdAt", Property.of(p -> p.date(d -> d)))
.properties(
"createdAt",
Property.of(p -> p.date(d -> d.format("strict_date_optional_time||epoch_millis")))
)
);
}
@@ -96,7 +105,10 @@ public class SearchIndexInitializer {
.properties("type", Property.of(p -> p.keyword(k -> k)))
.properties("title", Property.of(p -> p.text(t -> t)))
.properties("content", Property.of(p -> p.text(t -> t)))
.properties("createdAt", Property.of(p -> p.date(d -> d)))
.properties(
"createdAt",
Property.of(p -> p.date(d -> d.format("strict_date_optional_time||epoch_millis")))
)
);
}
}