Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: meilisearch poc #111

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5d90f6b
update ts
philmtd Mar 2, 2023
6cb24aa
ng15
philmtd Mar 2, 2023
cc21547
material15
philmtd Mar 2, 2023
0f2b182
update more deps
philmtd Mar 2, 2023
9fd91c9
update more deps
philmtd Mar 2, 2023
f31cb8d
update build image
philmtd Mar 2, 2023
60970ce
Merge remote-tracking branch 'origin/master' into update-frontend-dep…
philmtd Mar 2, 2023
c547505
first idea of a search field
philmtd Mar 2, 2023
2eca811
Merge branch 'master' into update-frontend-dependencies
philmtd Mar 3, 2023
009fffe
add meilisearch poc
philmtd Mar 3, 2023
579c659
Merge remote-tracking branch 'origin/update-frontend-dependencies' in…
philmtd Mar 3, 2023
efbf03f
implement search api and search result box
philmtd Mar 6, 2023
298af3b
change search endpoint, add action buttons to search result
philmtd Mar 7, 2023
fc88818
improve search box rendering when re-opening
philmtd Mar 7, 2023
240148f
use the caching project rest client more
philmtd Mar 7, 2023
6a827d3
prevent default brower shortcut
philmtd Mar 7, 2023
4f530bb
add multi deploy action buttons
philmtd Mar 8, 2023
174af93
Merge remote-tracking branch 'origin/implement-search-field' into mei…
philmtd Mar 8, 2023
013a8b3
adapt meilisearch service to changes introduced in search field branch
philmtd Mar 8, 2023
58575cf
Merge branch 'master' into implement-search-field
tom-schoener Mar 8, 2023
f566add
remove duplicate function
tom-schoener Mar 8, 2023
e2cf8e4
Merge branch 'master' into implement-search-field
philmtd Mar 8, 2023
cb2774a
implement changes requested in review
philmtd Mar 9, 2023
407946f
Merge remote-tracking branch 'origin/implement-search-field' into mei…
philmtd Mar 9, 2023
217c46d
change meilisearch service to implement internal method
philmtd Mar 9, 2023
e152378
Merge remote-tracking branch 'origin/master' into meilisearch-poc
philmtd Mar 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ services:
image: mongo:4
container_name: o-neko-mongodb
ports:
- "27017:27017"
- "27017:27017"

meilisearch:
image: getmeili/meilisearch:v1.0.2
ports:
- "7700:7700"
environment:
- MEILI_MASTER_KEY=ZWEZtwJKXZX3mQGA9hlqKr7THr2UTcHOKThuV8aWq3A
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
<artifactId>springdoc-openapi-security</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>com.meilisearch.sdk</groupId>
<artifactId>meilisearch-java</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/oneko/search/impl/DatabaseSearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import io.oneko.search.SearchResult;
import io.oneko.search.VersionSearchResultEntry;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnProperty(value = "o-neko.search.meilisearch.enabled", havingValue = "false", matchIfMissing = true)
public class DatabaseSearchService extends MeasuringSearchService {

private final ProjectRepository projectRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package io.oneko.search.impl.meilisearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.gson.Gson;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.GsonJsonHandler;
import com.meilisearch.sdk.model.Searchable;
import io.micrometer.core.instrument.MeterRegistry;
import io.oneko.event.EventDispatcher;
import io.oneko.project.ProjectRepository;
import io.oneko.project.ReadableProject;
import io.oneko.project.ReadableProjectVersion;
import io.oneko.project.event.ProjectDeletedEvent;
import io.oneko.project.event.ProjectSavedEvent;
import io.oneko.search.MeasuringSearchService;
import io.oneko.search.ProjectSearchResultEntry;
import io.oneko.search.SearchResult;
import io.oneko.search.VersionSearchResultEntry;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnProperty(value = "o-neko.search.meilisearch.enabled", havingValue = "true")
public class MeilisearchSearchService extends MeasuringSearchService {

private final ObjectMapper objectMapper;
private final Index projectIndex;
private final Index versionIndex;
private final Gson gson = new Gson();
private final Cache<String, SearchResult> queryResultCache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(512)
.build(); // TODO: Evaluate if cache is really useful in combination with Meilisearch

public MeilisearchSearchService(ProjectRepository projectRepository,
EventDispatcher eventDispatcher,
ObjectMapper objectMapper,
MeterRegistry meterRegistry) throws MeilisearchException {
super(meterRegistry);

this.objectMapper = objectMapper;

Client client = new Client(
new Config("http://localhost:7700", "ZWEZtwJKXZX3mQGA9hlqKr7THr2UTcHOKThuV8aWq3A", new GsonJsonHandler())); // TODO: Read values from config
projectIndex = client.index("oneko_projects");
versionIndex = client.index("oneko_versions");

boolean initIndexes = true; // TODO: Check if index exists or is empty

eventDispatcher.registerListener(event -> {
if (event instanceof ProjectSavedEvent pse) {
UUID projectId = pse.describeEntityChange()
.getId();
projectRepository.getById(projectId)
.ifPresent(project -> {
delete(project.getId()
.toString());
indexProject(project);
});
queryResultCache.invalidateAll();
} else if (event instanceof ProjectDeletedEvent pde) {
String deletedProjectId = pde.describeEntityChange()
.getId()
.toString();
delete(deletedProjectId);
queryResultCache.invalidateAll();
}
});

if (initIndexes) {
CompletableFuture.runAsync(() -> projectRepository.getAll()
.forEach(this::indexProject));
}
}

private void indexProject(ReadableProject project) {
ProjectMeili projectMeili = toProjectMeili(project);
try {
String json = gson.toJson(projectMeili);
projectIndex.addDocuments(json, "id");
} catch (MeilisearchException e) {
throw new RuntimeException(e);
}
project.getVersions()
.forEach(this::indexVersion);
}

private void indexVersion(ReadableProjectVersion version) {
VersionMeili versionMeili = toVersionMeili(version);
try {
String json = gson.toJson(versionMeili);
versionIndex.addDocuments(json, "id");
} catch (MeilisearchException e) {
throw new RuntimeException(e);
}
}

private ProjectMeili toProjectMeili(ReadableProject project) {
return ProjectMeili.builder()
.id(project.getId())
.name(project.getName())
.containerImage(project.getImageName())
.versionNames(project.getVersions()
.stream()
.map(ReadableProjectVersion::getName)
.toList())
.build();
}

private VersionMeili toVersionMeili(ReadableProjectVersion version) {
ReadableProject project = version.getProject();
return VersionMeili.builder()
.id(version.getId())
.name(version.getName())
.projectId(project.getId())
.projectName(project.getName())
.build();
}

private void delete(String projectId) {
try {
projectIndex.deleteDocument(projectId);
Searchable search = versionIndex.search(new SearchRequest("").setFilter(new String[]{"projectId = " + projectId}));
search.getHits()
.stream()
.map(h -> h.get("id"))
.forEach(id -> {
if (id instanceof String) {
try {
versionIndex.deleteDocument((String) id);
} catch (MeilisearchException e) {
throw new RuntimeException(e);
}
}
});
} catch (MeilisearchException e) {
throw new RuntimeException(e);
}
}

@Override
public SearchResult findProjectsAndVersionsInternal(String searchTerm) {
return queryResultCache.get(searchTerm, s -> {
try {
var versions = versionIndex.search(searchTerm)
.getHits()
.stream()
.map(res -> objectMapper.convertValue(res, VersionMeili.class))
.map(vm -> new VersionSearchResultEntry(vm.getName(), vm.getId(), vm.getProjectName(), vm.getProjectId()))
.toList();

var projects = projectIndex.search(searchTerm)
.getHits()
.stream()
.map(res -> objectMapper.convertValue(res, ProjectMeili.class))
.map(pm -> new ProjectSearchResultEntry(pm.getName(), pm.getId()))
.toList();

return SearchResult.builder()
.query(searchTerm)
.projects(projects)
.versions(versions)
.build();
} catch (MeilisearchException e) {
throw new RuntimeException(e);
}
});
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/oneko/search/impl/meilisearch/ProjectMeili.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.oneko.search.impl.meilisearch;

import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProjectMeili {

private UUID id;
private String name;
private String containerImage;
private List<String> versionNames;

}
20 changes: 20 additions & 0 deletions src/main/java/io/oneko/search/impl/meilisearch/VersionMeili.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.oneko.search.impl.meilisearch;

import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class VersionMeili {

private UUID id;
private String name;
private UUID projectId;

private String projectName;
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ logging:
o-neko:
security:
credentialsCoderKey: VJxDYI6zT9gLLfY9MyDGf2nxQ8mY7DcECxTDqKIV
search:
meilisearch:
enabled: true

kubernetes:
auth:
Expand Down