Skip to content

Commit

Permalink
attempt to add pg_search
Browse files Browse the repository at this point in the history
this commit is an attempt at building a search functionality using only
the stuff provided by postgresql

it is, unfortunately, painfully slow:

    PgSearch::Document Load (7869.1ms)  SELECT "pg_search_documents".* FROM "pg_search_documents" INNER JOIN (SELECT "pg_search_documents"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))), (to_tsquery('simple', ''' ' || 'awoo' || ' ''')), 0)) AS rank FROM "pg_search_documents" WHERE ((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || 'awoo' || ' ''')))) AS pg_search_ce9b9dd18c5c0023f2116f ON "pg_search_documents"."id" = pg_search_ce9b9dd18c5c0023f2116f.pg_search_id ORDER BY pg_search_ce9b9dd18c5c0023f2116f.rank DESC, "pg_search_documents"."id" ASC LIMIT $1  [["LIMIT", 10]]
  • Loading branch information
nilsding committed Oct 22, 2023
1 parent e37532e commit c14bae7
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,5 @@ gem "openssl", "~> 3.2"
gem "mail", "~> 2.7.1"

gem "prometheus-client", "~> 4.2"

gem "pg_search", "~> 2.3"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.4)
pg_search (2.3.6)
activerecord (>= 5.2)
activesupport (>= 5.2)
pghero (3.3.4)
activerecord (>= 6)
prometheus-client (4.2.1)
Expand Down Expand Up @@ -542,6 +545,7 @@ DEPENDENCIES
oj
openssl (~> 3.2)
pg
pg_search (~> 2.3)
pghero
prometheus-client (~> 4.2)
puma
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/search.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
9 changes: 9 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SearchController < ApplicationController
def index
@results = []
query = params[:q]
return if query.blank?

@results = PgSearch.multisearch(query).limit(10)
end
end
2 changes: 2 additions & 0 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SearchHelper
end
3 changes: 3 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Answer < ApplicationRecord
extend Answer::TimelineMethods

include PgSearch::Model
multisearchable against: [:content]

belongs_to :user, counter_cache: :answered_count
belongs_to :question, counter_cache: :answer_count
has_many :comments, dependent: :destroy
Expand Down
3 changes: 3 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Question < ApplicationRecord
include Question::AnswerMethods

include PgSearch::Model
multisearchable against: [:content]

belongs_to :user, optional: true
has_many :answers, dependent: :destroy
has_many :inboxes, dependent: :destroy
Expand Down
23 changes: 23 additions & 0 deletions app/views/search/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.container-lg.container--main
.row
.col-sm-10.col-md-10.col-lg-9.mx-auto
.card
.card-body
= bootstrap_form_tag layout: :inline, method: :get do |f|
= f.text_field :q, skip_label: true, append: f.primary("Search")
- unless @results.blank?
.container-lg.container--main
.row
.col-sm-10.col-md-10.col-lg-9.mx-auto
- @results.each do |result|
- case result.searchable
- when Answer
= render "answerbox", a: result.searchable, display_all: false, subscribed_answer_ids: []
- when Question
= render "shared/question", q: result.searchable, type: nil

= render 'shared/links'

:ruby
provide(:title, generate_title('Search'))
parent_layout 'base'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
post "/inbox/create", to: "inbox#create", as: :inbox_create
get "/inbox", to: "inbox#show", as: :inbox

get "/search", to: "search#index"

get "/user/:username", to: "user#show"
get "/@:username", to: "user#show", as: :user
get "/@:username/a/:id", to: "answer#show", as: :answer
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20231022155815_create_pg_search_documents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreatePgSearchDocuments < ActiveRecord::Migration[6.1]
def up
say_with_time("Creating table for pg_search multisearch") do
create_table :pg_search_documents do |t|
t.text :content
t.belongs_to :searchable, polymorphic: true, index: true
t.timestamps null: false
end
end
end

def down
say_with_time("Dropping table for pg_search multisearch") do
drop_table :pg_search_documents
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_10_18_172518) do
ActiveRecord::Schema.define(version: 2023_10_22_155815) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -127,6 +127,15 @@
t.index ["type"], name: "index_notifications_on_type"
end

create_table "pg_search_documents", force: :cascade do |t|
t.text "content"
t.string "searchable_type"
t.bigint "searchable_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable"
end

create_table "profiles", force: :cascade do |t|
t.bigint "user_id"
t.string "display_name"
Expand Down Expand Up @@ -388,5 +397,6 @@
t.index ["user_id"], name: "index_web_push_subscriptions_on_user_id"
end

add_foreign_key "anonymous_blocks", "users", column: "target_user_id"
add_foreign_key "profiles", "users"
end
15 changes: 15 additions & 0 deletions spec/helpers/search_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the SearchHelper. For example:
#
# describe SearchHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe SearchHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
11 changes: 11 additions & 0 deletions spec/requests/search_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe "Searches", type: :request do
describe "GET /index" do
it "returns http success" do
get "/search/index"
expect(response).to have_http_status(:success)
end
end

end
5 changes: 5 additions & 0 deletions spec/views/search/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "search/index.html.erb", type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit c14bae7

Please sign in to comment.