Skip to content

Commit

Permalink
4476 task revisit elasticsearchservice semantic (#4521)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvince2 authored Aug 26, 2024
1 parent 9ced443 commit aaf540f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public void indexProject(final Project project) throws IOException {
elasticService.indexWithRouting(getAlias(), project.getId().toString(), doc, routing);
}

public void forceESRefresh() throws IOException {
elasticService.refreshIndex(getIndex());
}

public void updateProject(final Project project) throws IOException {
final ProjectDocument doc = new ProjectDocument();
doc.setUserId(project.getUserId());
Expand All @@ -150,7 +154,7 @@ public void removeProject(final UUID id) throws IOException {
elasticService.delete(getAlias(), id.toString());
}

private String getPermissionId(final UUID projectId, final String userId) {
private static String getPermissionId(final UUID projectId, final String userId) {
return projectId.toString() + "_" + userId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,7 @@ public <T> void index(final String index, final String id, final T document) thr
try {
log.info("Indexing: {} into {}", id, index);

final IndexRequest<T> req = new IndexRequest.Builder<T>()
.index(index)
.id(id)
.document(document)
.refresh(Refresh.WaitFor)
.build();
final IndexRequest<T> req = new IndexRequest.Builder<T>().index(index).id(id).document(document).build();

client.index(req);
} catch (final ElasticsearchException e) {
Expand All @@ -428,7 +423,6 @@ public <T> void indexWithRouting(final String index, final String id, final T do
.index(index)
.id(id)
.document(document)
.refresh(Refresh.WaitFor)
.routing(routing)
.build();

Expand All @@ -448,7 +442,7 @@ public void delete(final String index, final String id) throws IOException {
try {
log.info("Deleting: {} from {}", id, index);

final DeleteRequest req = new DeleteRequest.Builder().index(index).id(id).refresh(Refresh.WaitFor).build();
final DeleteRequest req = new DeleteRequest.Builder().index(index).id(id).build();

client.delete(req);
} catch (final ElasticsearchException e) {
Expand All @@ -470,7 +464,6 @@ public <T, Partial> void update(final String index, final String id, final Parti
.index(index)
.id(id)
.doc(partial)
.refresh(Refresh.WaitFor)
.build();

client.update(req, Void.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void setup() throws IOException {
project = projectService.createProject(
(Project) new Project().setPublicAsset(true).setName("test-project-name").setDescription("my description")
);
modelService.setupIndexAndAliasAndEnsureEmpty();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public void testItCanSearchPublicProject() throws Exception {
project.setDescription("my description");

projectSearchService.indexProject(project);

projectSearchService.forceESRefresh();
final List<ProjectDocument> docs = projectSearchService.searchProjectsForUser(searcherId, 0, 10, null);

Assertions.assertEquals(1, docs.size());
}

Expand All @@ -73,8 +74,10 @@ public void testItCanSearchPrivateProjectAsReader() throws Exception {

projectSearchService.indexProject(project);
projectSearchService.addProjectPermission(project.getId(), searcherId);
projectSearchService.forceESRefresh();

final List<ProjectDocument> docs = projectSearchService.searchProjectsForUser(searcherId, 0, 10, null);

Assertions.assertEquals(1, docs.size());
}

Expand All @@ -90,8 +93,9 @@ public void testItCanSearchPrivateProjectAsOwner() throws Exception {
project.setDescription("my description");

projectSearchService.indexProject(project);

projectSearchService.forceESRefresh();
final List<ProjectDocument> docs = projectSearchService.searchProjectsForUser(ownerId, 0, 10, null);

Assertions.assertEquals(1, docs.size());
}

Expand Down Expand Up @@ -145,8 +149,10 @@ public void testPermissionsWithMultipleProjects() throws Exception {

projectSearchService.indexProject(project);
}
projectSearchService.forceESRefresh();

final List<ProjectDocument> docs = projectSearchService.searchProjectsForUser(readerId, 0, 100, null);

Assertions.assertEquals(NUM_VISIBLE_PROJECTS + NUM_PUBLIC_PROJECTS + NUM_OWNED_PROJECTS, docs.size());
}
}

0 comments on commit aaf540f

Please sign in to comment.