Skip to content

Commit

Permalink
support multiple fulltext search implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
querwurzel committed May 10, 2024
1 parent 9223f8b commit 8dcf526
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,7 @@

@Profile("mysql")
@Component
@Order(0)
class MySqlFullTextSupport implements FullTextSearchSupport {

private final R2dbcEntityTemplate entityTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PasteRepositoryCustomImpl implements PasteRepositoryCustom {
private final List<FullTextSearchSupport> fullTextSearchSupport;

public PasteRepositoryCustomImpl(R2dbcEntityTemplate entityManager, List<FullTextSearchSupport> 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;
}
Expand Down Expand Up @@ -79,15 +79,19 @@ public Mono<Long> markExpiredPastesForDeletion() {

@Override
public Flux<Paste> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 8dcf526

Please sign in to comment.