Skip to content

Commit

Permalink
add fts to simple collection/institution suggestions #652
Browse files Browse the repository at this point in the history
  • Loading branch information
ahakanzn committed Feb 3, 2025
1 parent 9635f14 commit 348aa5c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ public void testSuggest() {
assertEquals(2, institutionService.suggest("II").size());
assertEquals(1, institutionService.suggest("II2").size());
assertEquals(1, institutionService.suggest("name2").size());
assertEquals(1, institutionService.suggest("Institution name2").size());
assertDoesNotThrow(() -> institutionService.suggest(""));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

<changeSet id="153" author="auzun" runInTransaction="false">
<sql splitStatements="false" stripComments="false">
<![CDATA[
CREATE INDEX idx_institution_fulltext_search ON institution USING gin (to_tsvector('english', code || ' ' || name));
]]>
</sql>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,5 @@
<include file="liquibase/150-similar-name-list-function.xml" />
<include file="liquibase/151-indexes-collections-contacts.xml" />
<include file="liquibase/152-function-search-paths.xml" />
<include file="liquibase/153-institution-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
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,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 institution WHERE code ilike #{q}||'%' AND deleted IS NULL ORDER BY code LIMIT 20)
UNION ALL
(SELECT key,code,name FROM institution WHERE name ilike #{q}||'%' AND deleted IS NULL ORDER BY name LIMIT 20)
UNION ALL
(SELECT key,code,name FROM institution 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 institution 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 institution
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 institution
WHERE deleted IS NULL
LIMIT 20;
</if>
</select>

<!-- COMMENTS -->
Expand Down

0 comments on commit 348aa5c

Please sign in to comment.