mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-01-18 20:26:01 +01:00
attempt to add pg_search
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]]
This commit is contained in:
parent
e37532e0eb
commit
c14bae74d5
14 changed files with 110 additions and 1 deletions
2
Gemfile
2
Gemfile
|
@ -118,3 +118,5 @@ gem "openssl", "~> 3.2"
|
|||
gem "mail", "~> 2.7.1"
|
||||
|
||||
gem "prometheus-client", "~> 4.2"
|
||||
|
||||
gem "pg_search", "~> 2.3"
|
||||
|
|
|
@ -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)
|
||||
|
@ -542,6 +545,7 @@ DEPENDENCIES
|
|||
oj
|
||||
openssl (~> 3.2)
|
||||
pg
|
||||
pg_search (~> 2.3)
|
||||
pghero
|
||||
prometheus-client (~> 4.2)
|
||||
puma
|
||||
|
|
3
app/assets/stylesheets/search.scss
Normal file
3
app/assets/stylesheets/search.scss
Normal file
|
@ -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
app/controllers/search_controller.rb
Normal file
9
app/controllers/search_controller.rb
Normal file
|
@ -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
app/helpers/search_helper.rb
Normal file
2
app/helpers/search_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module SearchHelper
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
23
app/views/search/index.haml
Normal file
23
app/views/search/index.haml
Normal file
|
@ -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'
|
|
@ -148,6 +148,8 @@ Rails.application.routes.draw do
|
|||
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
|
||||
|
|
17
db/migrate/20231022155815_create_pg_search_documents.rb
Normal file
17
db/migrate/20231022155815_create_pg_search_documents.rb
Normal file
|
@ -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
db/schema.rb
12
db/schema.rb
|
@ -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"
|
||||
|
@ -127,6 +127,15 @@ ActiveRecord::Schema.define(version: 2023_10_18_172518) do
|
|||
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"
|
||||
|
@ -388,5 +397,6 @@ ActiveRecord::Schema.define(version: 2023_10_18_172518) do
|
|||
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
spec/helpers/search_helper_spec.rb
Normal file
15
spec/helpers/search_helper_spec.rb
Normal file
|
@ -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
spec/requests/search_spec.rb
Normal file
11
spec/requests/search_spec.rb
Normal file
|
@ -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
spec/views/search/index.html.erb_spec.rb
Normal file
5
spec/views/search/index.html.erb_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue