Skip to content
Permalink
Browse files

Full text search using PGroonga extension.

* To use this feature, you have to install [PGroonga](https://pgroonga.github.io/install/).
* Execute `rails db:migrate`. ( It takes VERY VERY LONG TIME!! )
  • Loading branch information...
kyori19 committed May 4, 2019
1 parent c88d9e5 commit d2cfb97c02128c89ec6795fbed8682853450edee
@@ -33,28 +33,31 @@ def perform_accounts_search!
end

def perform_statuses_search!
definition = StatusesIndex.filter(term: { searchable_by: @account.id })
.query(multi_match: { type: 'most_fields', query: @query, operator: 'and', fields: %w(text text.stemmed) })
results = Status.where(visibility: :public)
.where("statuses.text &@~ ?", @query)
.limit(@limit)
.order("statuses.id DESC")

if @options[:account_id].present?
definition = definition.filter(term: { account_id: @options[:account_id] })
results = results
.where(account_id: @options[:account_id])
end

if @options[:min_id].present? || @options[:max_id].present?
range = {}
range[:gt] = @options[:min_id].to_i if @options[:min_id].present?
range[:lt] = @options[:max_id].to_i if @options[:max_id].present?
definition = definition.filter(range: { id: range })
if @options[:min_id].present?
results = results
.where("statuses.id > ?", @options[:min_id])
end

if @options[:max_id].present?
results = results
.where("statuses.id < ?", @options[:max_id])
end

results = definition.limit(@limit).offset(@offset).objects.compact
account_ids = results.map(&:account_id)
account_domains = results.map(&:account_domain)
preloaded_relations = relations_map_for_account(@account, account_ids, account_domains)

results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
rescue Faraday::ConnectionFailed
[]
end

def perform_hashtags_search!
@@ -86,8 +89,6 @@ def url_resource_symbol
end

def full_text_searchable?
return false unless Chewy.enabled?

statuses_search? && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
end

@@ -0,0 +1,5 @@
class AddPgroongaExtension < ActiveRecord::Migration[5.2]
def change
enable_extension "pgroonga"
end
end
@@ -0,0 +1,5 @@
class AddPgroongaIndex < ActiveRecord::Migration[5.2]
def change
safety_assured { add_index :statuses, :text, using: :pgroonga }
end
end
@@ -10,9 +10,10 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_04_20_025523) do
ActiveRecord::Schema.define(version: 2019_04_26_100334) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgroonga"
enable_extension "plpgsql"

create_table "account_conversations", force: :cascade do |t|
@@ -629,6 +630,7 @@
t.index ["in_reply_to_account_id"], name: "index_statuses_on_in_reply_to_account_id"
t.index ["in_reply_to_id"], name: "index_statuses_on_in_reply_to_id"
t.index ["reblog_of_id", "account_id"], name: "index_statuses_on_reblog_of_id_and_account_id"
t.index ["text"], name: "index_statuses_on_text", using: :pgroonga
t.index ["uri"], name: "index_statuses_on_uri", unique: true
end

0 comments on commit d2cfb97

Please sign in to comment.
You can’t perform that action at this time.