Display other books from the same series on the metadata viewer page (#151)
* Display other books from the same series on the metadata viewer page * feat(recommendations): filter out books from the same series as the target book --------- Co-authored-by: adityachandelgit <>
This commit is contained in:
co-authored by
adityachandelgit
parent
fd4b3c2bee
commit
b8a493d264
+14
-1
@@ -6,6 +6,7 @@ import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookRecommendation;
|
||||
import com.adityachandel.booklore.model.entity.AuthorEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -30,8 +31,20 @@ public class BookRecommendationService {
|
||||
|
||||
List<BookEntity> candidates = bookRepository.findAll();
|
||||
|
||||
String targetSeriesName = Optional.ofNullable(target.getMetadata())
|
||||
.map(BookMetadataEntity::getSeriesName)
|
||||
.map(String::toLowerCase)
|
||||
.orElse(null);
|
||||
|
||||
List<SimpleEntry<BookEntity, Double>> scored = candidates.stream()
|
||||
.filter(candidate -> !candidate.getId().equals(bookId))
|
||||
.filter(candidate -> !candidate.getId().equals(bookId)) // exclude self
|
||||
.filter(candidate -> {
|
||||
String candidateSeriesName = Optional.ofNullable(candidate.getMetadata())
|
||||
.map(BookMetadataEntity::getSeriesName)
|
||||
.map(String::toLowerCase)
|
||||
.orElse(null);
|
||||
return targetSeriesName == null || !targetSeriesName.equals(candidateSeriesName);
|
||||
})
|
||||
.map(candidate -> new SimpleEntry<>(candidate, similarityService.calculateSimilarity(target, candidate)))
|
||||
.filter(entry -> entry.getValue() > 0.0)
|
||||
.sorted(Map.Entry.<BookEntity, Double>comparingByValue().reversed())
|
||||
|
||||
Reference in New Issue
Block a user