Skip to content

Commit

Permalink
#394 - Configure searchstore in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsanbagwan authored and Brutus5000 committed Feb 20, 2021
1 parent 1996555 commit 5843ef9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ dependencies {
implementation("com.yahoo.elide:elide-core:${elideVersion}")
implementation("com.yahoo.elide:elide-swagger:${elideVersion}")
implementation("com.yahoo.elide:elide-datastore-hibernate5:${elideVersion}")
implementation("com.yahoo.elide:elide-datastore-search:${elideVersion}")

implementation("org.luaj:luaj-jse:${luajVersion}")
implementation("com.github.micheljung:nocatch:${nocatchVersion}")
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/faforever/api/config/elide/ElideConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.elide.Elide;
import com.yahoo.elide.ElideSettingsBuilder;
import com.yahoo.elide.core.DataStore;
import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.filter.dialect.CaseSensitivityStrategy;
import com.yahoo.elide.core.filter.dialect.RSQLFilterDialect;
import com.yahoo.elide.datastores.search.SearchDataStore;
import com.yahoo.elide.jsonapi.JsonApiMapper;
import com.yahoo.elide.security.checks.Check;
import com.yahoo.elide.utils.coerce.CoerceUtil;
Expand All @@ -41,6 +43,8 @@
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -130,4 +134,16 @@ public EntityDictionary entityDictionary() {

return new EntityDictionary(checks);
}

@Bean
public Elide initElideSearch(SpringHibernateDataStore springHibernateDataStore) {
DataStore store = springHibernateDataStore;

EntityManagerFactory emf = Persistence.createEntityManagerFactory("searchDataStore");

boolean indexOnStartup = true;
DataStore searchStore = new SearchDataStore(store, emf, indexOnStartup);

return new Elide(new ElideSettingsBuilder(searchStore).build());
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/faforever/api/data/domain/Avatar.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;
import org.apache.lucene.analysis.core.LowerCaseFilterFactory;
import org.apache.lucene.analysis.ngram.NGramTokenizerFactory;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Parameter;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;

import javax.persistence.CascadeType;
import javax.persistence.Column;
Expand All @@ -22,6 +34,16 @@
@Entity
@Table(name = "avatars_list")
@Include(rootLevel = true, type = Avatar.TYPE_NAME)
@Indexed
@AnalyzerDef(name = "case_insensitive",
tokenizer = @TokenizerDef(factory = NGramTokenizerFactory.class, params = {
@Parameter(name = "minGramSize", value = "3"),
@Parameter(name = "maxGramSize", value = "10")
}),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class)
}
)
@Setter
@Type(Avatar.TYPE_NAME)
@EntityListeners(AvatarEnricherListener.class)
Expand All @@ -42,10 +64,14 @@ public String getUrl() {

@Column(name = "filename")
@NotNull
@Field(index = Index.YES, analyze = Analyze.YES,
store = Store.NO, analyzer = @Analyzer(definition = "case_insensitive"))
public String getFilename() { return filename; }

@Column(name = "tooltip")
@UpdatePermission(expression = WriteAvatarCheck.EXPRESSION)
@Field(index = Index.YES, analyze = Analyze.YES,
store = Store.NO, analyzer = @Analyzer(definition = "case_insensitive"))
public String getTooltip() {
return tooltip;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/faforever/api/data/domain/AvatarAssignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;
import org.apache.lucene.analysis.core.LowerCaseFilterFactory;
import org.apache.lucene.analysis.ngram.NGramTokenizerFactory;
import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Parameter;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -26,6 +33,16 @@
@Entity
@Table(name = "avatars")
@Include(rootLevel = true, type = AvatarAssignment.TYPE_NAME)
@Indexed
@AnalyzerDef(name = "case_insensitive",
tokenizer = @TokenizerDef(factory = NGramTokenizerFactory.class, params = {
@Parameter(name = "minGramSize", value = "3"),
@Parameter(name = "maxGramSize", value = "10")
}),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class)
}
)
@CreatePermission(expression = WriteAvatarCheck.EXPRESSION)
@DeletePermission(expression = WriteAvatarCheck.EXPRESSION)
@Audit(action = Action.CREATE, logStatement = "Avatar ''{0}'' has been assigned to player ''{1}''", logExpressions = {"${avatarAssignment.avatar.id}", "${avatarAssignment.player.id}"})
Expand Down

0 comments on commit 5843ef9

Please sign in to comment.