-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #615 from codeforjapan/search-with-pg_bigm
pg_bigmを使った検索を有効にする
- Loading branch information
Showing
8 changed files
with
128 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pg_search" | ||
|
||
module Decidim | ||
# A Searchable Resource. | ||
# This is a model to a PgSearch table that indexes all searchable resources. | ||
# This table is used to perform textual searches. | ||
# | ||
# Main attributes are: | ||
# - locale: One entry per locale is required, so each resource will be indexed once per locale. | ||
# - content_a: The most relevant textual content. | ||
# - content_b: The second most relevant textual content. | ||
# - content_c: The third most relevant textual content. | ||
# - content_d: The less relevant textual content. | ||
# - datetime: The timestamp that places this resource in the line of time. Used as second criteria (first is text relevance) for sorting. | ||
# | ||
class SearchableResource < ApplicationRecord | ||
include PgSearch::Model | ||
|
||
belongs_to :organization, | ||
foreign_key: "decidim_organization_id", | ||
class_name: "Decidim::Organization" | ||
belongs_to :scope, | ||
foreign_key: "decidim_scope_id", | ||
class_name: "Decidim::Scope", | ||
optional: true | ||
belongs_to :resource, polymorphic: true | ||
belongs_to :decidim_participatory_space, polymorphic: true, optional: true | ||
|
||
validates :locale, uniqueness: { scope: [:decidim_organization_id, :resource_type, :resource_id] } # rubocop:disable Rails/UniqueValidationWithoutIndex | ||
|
||
pg_search_scope :global_search, | ||
against: { content_a: "A", content_b: "B", content_c: "C", content_d: "D" }, | ||
ranked_by: ":bigram", | ||
using: :bigram | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pg_search" | ||
|
||
Rails.application.config.to_prepare do | ||
## define `PgSearch::Features::Bigram` to use pg_bigm extension | ||
module PgSearch | ||
module Features | ||
class Bigram < Feature | ||
def conditions | ||
if options[:threshold] | ||
Arel::Nodes::Grouping.new( | ||
similarity.gteq(options[:threshold]) | ||
) | ||
else | ||
Arel::Nodes::Grouping.new( | ||
Arel::Nodes::InfixOperation.new( | ||
infix_operator, | ||
normalized_document, | ||
normalized_query_for_like | ||
) | ||
) | ||
end | ||
end | ||
|
||
def rank | ||
Arel::Nodes::Grouping.new(similarity) | ||
end | ||
|
||
private | ||
|
||
def similarity_function | ||
"bigm_similarity" | ||
end | ||
|
||
def infix_operator | ||
"like" | ||
end | ||
|
||
def similarity | ||
Arel::Nodes::NamedFunction.new( | ||
similarity_function, | ||
[ | ||
normalized_query, | ||
normalized_document | ||
] | ||
) | ||
end | ||
|
||
def normalized_document | ||
Arel::Nodes::Grouping.new(Arel.sql(normalize(document))) | ||
end | ||
|
||
def normalized_query_for_like | ||
sanitized_query = connection.quote(query) | ||
Arel.sql("likequery(#{normalize(sanitized_query)})") | ||
end | ||
|
||
def normalized_query | ||
sanitized_query = connection.quote(query) | ||
Arel.sql(normalize(sanitized_query)) | ||
end | ||
end | ||
end | ||
end | ||
|
||
## override `PgSearch::ScopeOptions::FEATURE_CLASSES` | ||
PgSearch::ScopeOptions::FEATURE_CLASSES = { | ||
dmetaphone: PgSearch::Features::DMetaphone, | ||
tsearch: PgSearch::Features::TSearch, | ||
trigram: PgSearch::Features::Trigram, | ||
bigram: PgSearch::Features::Bigram | ||
}.freeze | ||
end |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class EnablePgBigmExtension < ActiveRecord::Migration[6.1] | ||
def up | ||
enable_extension 'pg_bigm' | ||
end | ||
|
||
def down | ||
disable_extension 'pg_bigm' | ||
end | ||
end |
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