Skip to content

Commit

Permalink
add fts to simple collection suggestions #652
Browse files Browse the repository at this point in the history
  • Loading branch information
ahakanzn committed Jan 26, 2025
1 parent 9691e31 commit c0de954
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ public void testSuggest() {
assertEquals(2, collectionService.suggest("CC").size());
assertEquals(1, collectionService.suggest("CC2").size());
assertEquals(1, collectionService.suggest("name2").size());
assertDoesNotThrow(() -> collectionService.suggest(""));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<sql splitStatements="false" stripComments="false">
<![CDATA[
CREATE INDEX idx_institution_fulltext_search ON institution USING gin (to_tsvector('english', code || ' ' || name));
CREATE INDEX idx_collection_fulltext_search ON collection USING gin (to_tsvector('english', code || ' ' || name));
]]>
</sql>
</changeSet>
Expand Down
4 changes: 2 additions & 2 deletions registry-persistence/src/main/resources/liquibase/master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,6 @@
<include file="liquibase/148-primary-identifier-grscicoll.xml" />
<include file="liquibase/149-taxon-fields-collection-descriptors.xml" />
<include file="liquibase/150-similar-name-list-function.xml" />
<include file="liquibase/151-institution-suggest-fts.xml" />
<include file="liquibase/152-indexes-collections-contacts.xml" />
<include file="liquibase/151-indexes-collections-contacts.xml" />
<include file="liquibase/152-collections-suggest-fts.xml" />
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,24 @@

<!-- Simple suggest -->
<select id="suggest" resultType="org.gbif.api.model.registry.search.collections.KeyCodeNameResult">
SELECT key,code,name FROM (
(SELECT key,code,name FROM collection WHERE code ilike #{q}||'%' AND deleted IS NULL ORDER BY code LIMIT 20)
UNION ALL
(SELECT key,code,name FROM collection WHERE name ilike #{q}||'%' AND deleted IS NULL ORDER BY name LIMIT 20)
UNION ALL
(SELECT key,code,name FROM collection WHERE code ilike '%'||#{q}||'%' AND NOT code ilike #{q}||'%' AND deleted IS NULL ORDER BY code LIMIT 20)
UNION ALL
(SELECT key,code,name FROM collection WHERE name ilike '%'||#{q}||'%' AND NOT name ilike #{q}||'%' AND deleted IS NULL ORDER BY name LIMIT 20)
) t1 LIMIT 20
<if test="q != null and !q.isEmpty()">
SELECT key, code, name
FROM (
SELECT key, code, name,
to_tsvector('english', code || ' ' || name) AS document,
to_tsquery('english', unaccent(regexp_replace(#{q} || ':*', ' ', '&amp;', 'g'))) AS query
FROM collection
WHERE deleted IS NULL
) AS subquery
WHERE document @@ query
ORDER BY ts_rank(document, query) DESC
LIMIT 20;
</if>
<if test="q == null or q.isEmpty()">
SELECT key, code, name FROM collection
WHERE deleted IS NULL
LIMIT 20;
</if>
</select>

<!-- COMMENTS -->
Expand Down

0 comments on commit c0de954

Please sign in to comment.