Skip to content

Commit

Permalink
Fix an issue with the postgres indices as they weren't being used pro…
Browse files Browse the repository at this point in the history
…perly
  • Loading branch information
bjpirt committed Feb 27, 2024
1 parent 06bc5fd commit e20b480
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class PostgresIndexQueryBuilder {
"task_def_name",
"update_time",
"json_data",
"to_tsvector(json_data::text)"
"jsonb_to_tsvector('english', json_data, '[\"all\"]')"
};

private static final String[] VALID_SORT_ORDER = {"ASC", "DESC"};
Expand Down Expand Up @@ -76,7 +76,7 @@ public String getQueryFragment() {
if (operator.equals("IN")) {
return attribute + " = ANY(?)";
} else if (operator.equals("@@")) {
return attribute + " @@ to_tsquery(?)";
return attribute + " @@ ?";
} else if (operator.equals("@>")) {
return attribute + " @> ?::JSONB";
} else {
Expand Down Expand Up @@ -186,7 +186,7 @@ private void parseFreeText(String freeText) {
conditions.add(cond);
} else {
Condition cond = new Condition();
cond.setAttribute("to_tsvector(json_data::text)");
cond.setAttribute("jsonb_to_tsvector('english', json_data, '[\"all\"]')");
cond.setOperator("@@");
String[] values = {freeText};
cond.setValues(Arrays.asList(values));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Drop the unused text index on the json_data column
DROP INDEX CONCURRENTLY IF EXISTS workflow_index_json_data_text_idx;
-- Create a new index to enable querying the json by attribute and value
CREATE INDEX CONCURRENTLY IF NOT EXISTS workflow_index_json_data_gin_idx ON workflow_index USING GIN (json_data jsonb_path_ops);

-- Drop the incorrectly created indices on the workflow_index that should be on the task_index table
DROP INDEX CONCURRENTLY IF EXISTS task_index_json_data_json_idx;
DROP INDEX CONCURRENTLY IF EXISTS task_index_json_data_text_idx;
-- Create the full text index on the json_data column of the task_index table
CREATE INDEX CONCURRENTLY IF NOT EXISTS task_index_json_data_json_idx ON task_index USING GIN (jsonb_to_tsvector('english', json_data, '["all"]'));
-- Create a new index to enable querying the json by attribute and value
CREATE INDEX CONCURRENTLY IF NOT EXISTS task_index_json_data_gin_idx ON task_index USING GIN (json_data jsonb_path_ops);
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void shouldAllowFullTextSearch() throws SQLException {
new PostgresIndexQueryBuilder(
"table_name", "", freeText, 0, 15, Arrays.asList(query));
String expectedQuery =
"SELECT json_data::TEXT FROM table_name WHERE to_tsvector(json_data::text) @@ to_tsquery(?) LIMIT ? OFFSET ?";
"SELECT json_data::TEXT FROM table_name WHERE jsonb_to_tsvector('english', json_data, '[\"all\"]') @@ ? LIMIT ? OFFSET ?";
assertEquals(expectedQuery, builder.getQuery());
}

Expand Down

0 comments on commit e20b480

Please sign in to comment.