From 8dcf526de10798ec37db28b0d370e270f16d5b63 Mon Sep 17 00:00:00 2001 From: querwurzel <> Date: Fri, 10 May 2024 19:14:57 +0200 Subject: [PATCH] support multiple fulltext search implementations --- .../paste/domain/MySqlFullTextSupport.java | 2 ++ .../domain/PasteRepositoryCustomImpl.java | 26 +++++++++++-------- .../paste/domain/SimpleFullTextSupport.java | 3 ++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/com/github/binpastes/paste/domain/MySqlFullTextSupport.java b/backend/src/main/java/com/github/binpastes/paste/domain/MySqlFullTextSupport.java index 361db388..fdbfad19 100644 --- a/backend/src/main/java/com/github/binpastes/paste/domain/MySqlFullTextSupport.java +++ b/backend/src/main/java/com/github/binpastes/paste/domain/MySqlFullTextSupport.java @@ -2,6 +2,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; +import org.springframework.core.annotation.Order; import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; @@ -14,6 +15,7 @@ @Profile("mysql") @Component +@Order(0) class MySqlFullTextSupport implements FullTextSearchSupport { private final R2dbcEntityTemplate entityTemplate; diff --git a/backend/src/main/java/com/github/binpastes/paste/domain/PasteRepositoryCustomImpl.java b/backend/src/main/java/com/github/binpastes/paste/domain/PasteRepositoryCustomImpl.java index 369fa694..96708c00 100644 --- a/backend/src/main/java/com/github/binpastes/paste/domain/PasteRepositoryCustomImpl.java +++ b/backend/src/main/java/com/github/binpastes/paste/domain/PasteRepositoryCustomImpl.java @@ -27,7 +27,7 @@ class PasteRepositoryCustomImpl implements PasteRepositoryCustom { private final List fullTextSearchSupport; public PasteRepositoryCustomImpl(R2dbcEntityTemplate entityManager, List fullTextSearchSupport) { - Assert.isTrue(fullTextSearchSupport.size() <= 2, "Expected at most two FullTextSearchSupport implementations"); // ugly, but works for now + Assert.notEmpty(fullTextSearchSupport, "Require at least one FullTextSearchSupport implementation"); this.entityTemplate = entityManager; this.fullTextSearchSupport = fullTextSearchSupport; } @@ -79,15 +79,19 @@ public Mono markExpiredPastesForDeletion() { @Override public Flux searchAllLegitByFullText(String text) { - var flux = fullTextSearchSupport.getFirst().searchByFullText(text); - - return fullTextSearchSupport.size() == 1 - ? flux - : flux.switchIfEmpty(subscriber -> { - log.warn("Fulltext search found nothing for: {}", text); - fullTextSearchSupport.getLast() - .searchByFullText(text) - .subscribe(subscriber); - }); + var result = fullTextSearchSupport.getFirst().searchByFullText(text); + + for (int idx = 1; idx < fullTextSearchSupport.size(); idx++) { + final var alternativeImplementation = fullTextSearchSupport.get(idx); + + result = result.switchIfEmpty(subscriber -> { + log.warn("Utilising alternative FullTextSearch implementation {} for: {}", alternativeImplementation.getClass().getSimpleName(), text); + alternativeImplementation + .searchByFullText(text) + .subscribe(subscriber); + }); + } + + return result; } } diff --git a/backend/src/main/java/com/github/binpastes/paste/domain/SimpleFullTextSupport.java b/backend/src/main/java/com/github/binpastes/paste/domain/SimpleFullTextSupport.java index 6df7b179..8bdd8e28 100644 --- a/backend/src/main/java/com/github/binpastes/paste/domain/SimpleFullTextSupport.java +++ b/backend/src/main/java/com/github/binpastes/paste/domain/SimpleFullTextSupport.java @@ -1,6 +1,7 @@ package com.github.binpastes.paste.domain; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.data.domain.Sort; import org.springframework.data.r2dbc.core.R2dbcEntityTemplate; @@ -15,7 +16,7 @@ import static com.github.binpastes.paste.domain.Paste.PasteSchema; @Component -@Order +@Order(Ordered.LOWEST_PRECEDENCE) class SimpleFullTextSupport implements FullTextSearchSupport { private final R2dbcEntityTemplate entityTemplate;