Skip to content

Commit

Permalink
Metadata: Lower case index to support case insensitive searching (#806)
Browse files Browse the repository at this point in the history
* feat: update metadata index on attachments and samples to be on lowercase so that we can do case-insensitive searching

* chore: rename index so that it includes _ci suffix to denote it as case insensitive

* chore: add in test for case insensitive searching samples by metadata in graphql
  • Loading branch information
ericenns authored Oct 3, 2024
1 parent c75a5bd commit c21f739
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
12 changes: 10 additions & 2 deletions config/initializers/ransack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ module Predications # rubocop:disable Style/Documentation
def contains_key(other)
Arel::Nodes::InfixOperation.new(:'?', self, Arel::Nodes::Quoted.new(other))
end

# contains but case insensitive
def contains_ci(other)
Arel::Nodes::Contains.new(
Arel::Nodes::SqlLiteral.new("lower(\"#{relation.name}\".\"#{name}\"::text)::jsonb"),
Arel::Nodes.build_quoted(other, self)
)
end
end
end

Ransack.configure do |config|
config.add_predicate 'jcont', arel_predicate: 'contains', formatter: proc { |v| JSON.parse(v.to_s) }
config.add_predicate 'jcont_key', arel_predicate: 'contains_key'
config.add_predicate 'jcont', arel_predicate: 'contains_ci', formatter: proc { |v| JSON.parse(v.to_s.downcase) }
config.add_predicate 'jcont_key', arel_predicate: 'contains_key', formatter: proc { |v| v.to_s.downcase }
end
27 changes: 27 additions & 0 deletions db/migrate/20241003125314_update_metadata_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# Update metadata index for attachments and samples to be on lower case
class UpdateMetadataIndexes < ActiveRecord::Migration[7.2]
def change # rubocop:disable Metrics/MethodLength
reversible do |dir|
dir.up do
execute <<~SQL.squish
DROP INDEX index_attachments_on_metadata;
CREATE INDEX index_attachments_on_metadata_ci ON attachments USING GIN ((LOWER(metadata::text)::jsonb));
DROP INDEX index_samples_on_metadata;
CREATE INDEX index_samples_on_metadata_ci ON samples USING GIN ((LOWER(metadata::text)::jsonb));
SQL
end

dir.down do
execute <<~SQL.squish
DROP INDEX index_attachments_on_metadata_ci;
DROP INDEX index_samples_on_metadata_ci;
SQL
add_index :attachments, :metadata, using: :gin
add_index :samples, :metadata, using: :gin
end
end
end
end
6 changes: 3 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions test/graphql/samples_query_ransack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ def setup
assert_equal 4, data.count
end

test 'ransack samples query with metadata_jcont_key filter should work ignoring case' do
result = IridaSchema.execute(SAMPLES_RANSACK_QUERY,
context: { current_user: @user },
variables: { filter: { metadata_jcont_key: 'MetadataField1' } })

assert_nil result['errors'], 'should work and have no errors.'

data = result['data']['samples']['nodes']

assert_equal 4, data.count
end

test 'ransack samples query with metadata_jcont filter should work' do
result = IridaSchema.execute(SAMPLES_RANSACK_QUERY,
context: { current_user: @user },
Expand All @@ -204,6 +216,18 @@ def setup
assert_equal 4, data.count
end

test 'ransack samples query with metadata_jcont filter should work ignoring case' do
result = IridaSchema.execute(SAMPLES_RANSACK_QUERY,
context: { current_user: @user },
variables: { filter: { metadata_jcont: { MetadataField1: 'Value1' } } })

assert_nil result['errors'], 'should work and have no errors.'

data = result['data']['samples']['nodes']

assert_equal 4, data.count
end

test 'ransack samples query with metadata_jcont_key filter should work with non_existent key' do
result = IridaSchema.execute(SAMPLES_RANSACK_QUERY,
context: { current_user: @user },
Expand Down

0 comments on commit c21f739

Please sign in to comment.