mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 22:41:05 +08:00
feat: opensearch init
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package com.openisle.search;
|
||||
|
||||
import com.openisle.model.Category;
|
||||
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;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class SearchDocumentFactory {
|
||||
|
||||
private SearchDocumentFactory() {}
|
||||
|
||||
public static SearchDocument fromPost(Post post) {
|
||||
if (post == null || post.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> tags = post.getTags() == null
|
||||
? Collections.emptyList()
|
||||
: post
|
||||
.getTags()
|
||||
.stream()
|
||||
.map(Tag::getName)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
return new SearchDocument(
|
||||
"post",
|
||||
post.getId(),
|
||||
post.getTitle(),
|
||||
post.getContent(),
|
||||
post.getAuthor() != null ? post.getAuthor().getUsername() : null,
|
||||
post.getCategory() != null ? post.getCategory().getName() : null,
|
||||
tags,
|
||||
post.getId(),
|
||||
toInstant(post.getCreatedAt())
|
||||
);
|
||||
}
|
||||
|
||||
public static SearchDocument fromComment(Comment comment) {
|
||||
if (comment == null || comment.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
Post post = comment.getPost();
|
||||
List<String> tags = post == null || post.getTags() == null
|
||||
? Collections.emptyList()
|
||||
: post
|
||||
.getTags()
|
||||
.stream()
|
||||
.map(Tag::getName)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
return new SearchDocument(
|
||||
"comment",
|
||||
comment.getId(),
|
||||
post != null ? post.getTitle() : null,
|
||||
comment.getContent(),
|
||||
comment.getAuthor() != null ? comment.getAuthor().getUsername() : null,
|
||||
post != null && post.getCategory() != null ? post.getCategory().getName() : null,
|
||||
tags,
|
||||
post != null ? post.getId() : null,
|
||||
toInstant(comment.getCreatedAt())
|
||||
);
|
||||
}
|
||||
|
||||
public static SearchDocument fromUser(User user) {
|
||||
if (user == null || user.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
return new SearchDocument(
|
||||
"user",
|
||||
user.getId(),
|
||||
user.getUsername(),
|
||||
user.getIntroduction(),
|
||||
null,
|
||||
null,
|
||||
Collections.emptyList(),
|
||||
null,
|
||||
toInstant(user.getCreatedAt())
|
||||
);
|
||||
}
|
||||
|
||||
public static SearchDocument fromCategory(Category category) {
|
||||
if (category == null || category.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
return new SearchDocument(
|
||||
"category",
|
||||
category.getId(),
|
||||
category.getName(),
|
||||
category.getDescription(),
|
||||
null,
|
||||
null,
|
||||
Collections.emptyList(),
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
public static SearchDocument fromTag(Tag tag) {
|
||||
if (tag == null || tag.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
return new SearchDocument(
|
||||
"tag",
|
||||
tag.getId(),
|
||||
tag.getName(),
|
||||
tag.getDescription(),
|
||||
null,
|
||||
null,
|
||||
Collections.emptyList(),
|
||||
null,
|
||||
toInstant(tag.getCreatedAt())
|
||||
);
|
||||
}
|
||||
|
||||
private static Instant toInstant(LocalDateTime time) {
|
||||
return time == null ? null : time.atZone(ZoneId.systemDefault()).toInstant();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user