-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
347 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../bio/terra/pearl/core/service/participant/search/facets/sql/KeywordFacetSqlGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package bio.terra.pearl.core.service.participant.search.facets.sql; | ||
|
||
import bio.terra.pearl.core.dao.BaseJdbiDao; | ||
import bio.terra.pearl.core.service.participant.search.EnrolleeSearchUtils; | ||
import bio.terra.pearl.core.service.participant.search.facets.StringFacetValue; | ||
import org.jdbi.v3.core.statement.Query; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class KeywordFacetSqlGenerator implements FacetSqlGenerator<StringFacetValue> { | ||
|
||
public KeywordFacetSqlGenerator() {} | ||
|
||
@Override | ||
public String getTableName() { | ||
// this facet is only tied to enrollee and Profile tables that are already joined | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getJoinQuery() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getSelectQuery(StringFacetValue facetValue) { | ||
return null; // already included in base query | ||
} | ||
|
||
@Override | ||
public String getWhereClause(StringFacetValue facetValue, int facetIndex) { | ||
if (facetValue.getValues().isEmpty()) { | ||
return " 1 = 1"; | ||
} | ||
return IntStream.range(0, facetValue.getValues().size()) | ||
.mapToObj(index -> { | ||
String paramName = EnrolleeSearchUtils.getSqlParamName("keyword", facetValue.getKeyName(), index); | ||
return """ | ||
(profile.given_name ilike :%1$s | ||
OR profile.family_name ilike :%1$s | ||
OR profile.contact_email ilike :%1$s | ||
OR enrollee.shortcode ilike :%1$s) | ||
""" | ||
.formatted(paramName); | ||
}) | ||
.collect(Collectors.joining(" AND")); | ||
} | ||
|
||
@Override | ||
public String getCombinedWhereClause(List<StringFacetValue> facetValues) { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public void bindSqlParameters(StringFacetValue facetValue, int facetIndex, Query query) { | ||
for(int i = 0; i < facetValue.getValues().size(); i++) { | ||
query.bind(EnrolleeSearchUtils.getSqlParamName("keyword", | ||
facetValue.getKeyName(), i), "%" + facetValue.getValues().get(i) + "%"); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
core/src/main/resources/db/changelog/changesets/2023_09_14_add_profile_gin_indexes.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: profile_gin_indexes | ||
author: dbush | ||
changes: | ||
- sql: | ||
sql: CREATE EXTENSION IF NOT EXISTS pg_trgm with schema pg_catalog; | ||
- sql: | ||
sql: CREATE INDEX profile_given_name_gindx ON profile USING gin (given_name gin_trgm_ops); | ||
- sql: | ||
sql: CREATE INDEX profile_family_name_gindx ON profile USING gin (family_name gin_trgm_ops); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,8 @@ | |
import bio.terra.pearl.core.service.participant.search.facets.IntRangeFacetValue; | ||
import bio.terra.pearl.core.service.participant.search.facets.StableIdStringFacetValue; | ||
import bio.terra.pearl.core.service.participant.search.facets.StringFacetValue; | ||
import bio.terra.pearl.core.service.participant.search.facets.sql.ParticipantTaskFacetSqlGenerator; | ||
import bio.terra.pearl.core.service.participant.search.facets.sql.ProfileAgeFacetSqlGenerator; | ||
import bio.terra.pearl.core.service.participant.search.facets.sql.ProfileFacetSqlGenerator; | ||
import bio.terra.pearl.core.service.participant.search.facets.sql.SqlSearchableFacet; | ||
import bio.terra.pearl.core.service.participant.search.facets.sql.*; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
@@ -96,6 +94,58 @@ public void testProfileSearch() { | |
assertThat(result.get(0).getProfile().getSexAtBirth(), equalTo("male")); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void testKeywordSearchGivenFamilyName() { | ||
StudyEnvironment studyEnv = studyEnvironmentFactory.buildPersisted("testKeywordSearch"); | ||
|
||
Profile profile = Profile.builder().givenName("mark").familyName("stewart").build(); | ||
Enrollee markGivenNameEnrollee = enrolleeFactory.buildPersisted("testKeywordSearch", studyEnv, profile); | ||
Profile profile2 = Profile.builder().givenName("matt").familyName("stover").build(); | ||
Enrollee mattGivenNameEnrollee = enrolleeFactory.buildPersisted("testKeywordSearch", studyEnv, profile2); | ||
Profile profile3 = Profile.builder().givenName("steve").familyName("mallory").build(); | ||
Enrollee steveGivenNameEnrollee = enrolleeFactory.buildPersisted("testKeywordSearch", studyEnv, profile3); | ||
|
||
SqlSearchableFacet facet = new SqlSearchableFacet(new StringFacetValue( | ||
"keyword", List.of("mark")), new KeywordFacetSqlGenerator()); | ||
var result = enrolleeSearchDao.search(studyEnv.getId(), List.of(facet)); | ||
assertThat(result, hasSize(1)); | ||
assertThat(result.get(0).getEnrollee().getShortcode(), equalTo(markGivenNameEnrollee.getShortcode())); | ||
|
||
facet = new SqlSearchableFacet(new StringFacetValue( | ||
"keyword", List.of("ma")), new KeywordFacetSqlGenerator()); | ||
result = enrolleeSearchDao.search(studyEnv.getId(), List.of(facet)); | ||
assertThat(result, hasSize(3)); | ||
|
||
facet = new SqlSearchableFacet(new StringFacetValue( | ||
"keyword", List.of("allo")), new KeywordFacetSqlGenerator()); | ||
result = enrolleeSearchDao.search(studyEnv.getId(), List.of(facet)); | ||
assertThat(result, hasSize(1)); | ||
assertThat(result.get(0).getEnrollee().getShortcode(), equalTo(steveGivenNameEnrollee.getShortcode())); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void testKeywordSearchEmailShortcode() { | ||
StudyEnvironment studyEnv = studyEnvironmentFactory.buildPersisted("testKeywordSearch"); | ||
|
||
Profile profile = Profile.builder().contactEmail("[email protected]").build(); | ||
Enrollee maEmail = enrolleeFactory.buildPersisted("testKeywordSearch", studyEnv, profile); | ||
Profile profile2 = Profile.builder().contactEmail("[email protected]").familyName("stover").build(); | ||
Enrollee fooEmail = enrolleeFactory.buildPersisted("testKeywordSearch", studyEnv, profile2); | ||
|
||
SqlSearchableFacet facet = new SqlSearchableFacet(new StringFacetValue( | ||
"keyword", List.of(maEmail.getShortcode())), new KeywordFacetSqlGenerator()); | ||
var result = enrolleeSearchDao.search(studyEnv.getId(), List.of(facet)); | ||
assertThat(result, hasSize(1)); | ||
assertThat(result.get(0).getEnrollee().getShortcode(), equalTo(maEmail.getShortcode())); | ||
|
||
facet = new SqlSearchableFacet(new StringFacetValue( | ||
"keyword", List.of("a.com")), new KeywordFacetSqlGenerator()); | ||
result = enrolleeSearchDao.search(studyEnv.getId(), List.of(facet)); | ||
assertThat(result, hasSize(2)); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void testProfileAgeSearch() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.