Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some search functionality #1406

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 "meilisearch-rails", "~> 0.10.1"
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ GEM
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
meilisearch (0.25.1)
httparty (>= 0.17.1, < 0.22.0)
meilisearch-rails (0.10.1)
meilisearch (~> 0.25.0)
method_source (1.0.0)
mime-types (3.5.1)
mime-types-data (~> 3.2015)
Expand Down Expand Up @@ -535,6 +539,7 @@ DEPENDENCIES
letter_opener
lograge
mail (~> 2.7.1)
meilisearch-rails (~> 0.10.1)
mini_magick
net-imap
net-pop
Expand Down
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ web: unset PORT && bin/rails server
worker: bundle exec sidekiq
css: yarn build:css --watch
js: yarn build --watch
search: meilisearch --no-analytics --env development --db-path tmp/meilisearch/db --dump-dir tmp/meilisearch/dump --master-key justfordev42069e621
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/
35 changes: 35 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class SearchController < ApplicationController

Check notice on line 1 in app/controllers/search_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/search_controller.rb#L1 <Style/FrozenStringLiteralComment>

Missing frozen string literal comment.
Raw output
app/controllers/search_controller.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
def index
@results = []
@query = params[:q]
return if @query.blank?

@results = if params[:multi_search] == "1"
multi_search_experiment
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's the same thing (~2.7m documents), but combined in the same request:

19:52:44 search.1 | [2023-10-22T17:52:44Z INFO  actix_web::middleware::logger] ::1 "POST /multi-search HTTP/1.1" 200 2787 "-" "Meilisearch Rails (v0.10.1);Meilisearch Ruby (v0.25.1)" 0.002295

else
[*Answer.search(@query), *Question.search(@query)]
Copy link
Member Author

@nilsding nilsding Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't take too long tbh (~2.7m documents)

19:52:10 search.1 | [2023-10-22T17:52:10Z INFO  actix_web::middleware::logger] ::1 "POST /indexes/Answer/search HTTP/1.1" 200 959 "-" "Meilisearch Rails (v0.10.1);Meilisearch Ruby (v0.25.1)" 0.023072
19:52:10 search.1 | [2023-10-22T17:52:10Z INFO  actix_web::middleware::logger] ::1 "POST /indexes/Question/search HTTP/1.1" 200 933 "-" "Meilisearch Rails (v0.10.1);Meilisearch Ruby (v0.25.1)" 0.011066

end
end

private

def multi_search_experiment
MeiliSearch::Rails.client.multi_search(
[Answer, Question].map do |klass|
{
q: @query,
index_uid: klass.name.to_s,
show_ranking_score: true,
}
end

Check notice on line 24 in app/controllers/search_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/search_controller.rb#L24 <Style/TrailingCommaInArguments>

Put a comma after the last parameter of a multiline method call.
Raw output
app/controllers/search_controller.rb:24:7: C: Style/TrailingCommaInArguments: Put a comma after the last parameter of a multiline method call.
)["results"].flat_map do |h|
model = h["indexUid"].constantize # bad practice!
results = model.find(h["hits"].pluck("id")).map { |r| [r.id.to_s, r] }.to_h

Check notice on line 27 in app/controllers/search_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/search_controller.rb#L27 <Rails/IndexBy>

Prefer `index_by` over `map { ... }.to_h`.
Raw output
app/controllers/search_controller.rb:27:17: C: Rails/IndexBy: Prefer `index_by` over `map { ... }.to_h`.

Check notice on line 27 in app/controllers/search_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/search_controller.rb#L27 <Style/MapToHash>

Pass a block to `to_h` instead of calling `map.to_h`.
Raw output
app/controllers/search_controller.rb:27:51: C: Style/MapToHash: Pass a block to `to_h` instead of calling `map.to_h`.
h["hits"].map { |hit| [hit["_rankingScore"], results[hit["id"]]] }
end
.sort_by(&:first)
.reverse
.tap { |results| Rails.logger.debug(results) }

Check notice on line 32 in app/controllers/search_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/search_controller.rb#L29-L32 <Style/MultilineBlockChain>

Avoid multi-line chains of blocks.
Raw output
app/controllers/search_controller.rb:29:5: C: Style/MultilineBlockChain: Avoid multi-line chains of blocks.
.map(&:last)

Check notice on line 33 in app/controllers/search_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/controllers/search_controller.rb#L33 <Layout/MultilineMethodCallIndentation>

Align `.map` with `.client` on line 17.
Raw output
app/controllers/search_controller.rb:33:7: C: Layout/MultilineMethodCallIndentation: Align `.map` with `.client` on line 17.
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

Check notice on line 1 in app/helpers/search_helper.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] app/helpers/search_helper.rb#L1 <Style/FrozenStringLiteralComment>

Missing frozen string literal comment.
Raw output
app/helpers/search_helper.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
end
6 changes: 6 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
class Answer < ApplicationRecord
extend Answer::TimelineMethods

include MeiliSearch::Rails

meilisearch do
attribute :content
end

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

include MeiliSearch::Rails

meilisearch do
attribute :content
end

belongs_to :user, optional: true
has_many :answers, dependent: :destroy
has_many :inboxes, dependent: :destroy
Expand Down
24 changes: 24 additions & 0 deletions app/views/search/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.container-lg.container--main
.row
.col-sm-10.col-md-10.col-lg-9.mx-auto
.card
.card-body
= bootstrap_form_with url: search_path, layout: :inline, method: :get do |f|
= f.text_field :q, skip_label: true, append: f.primary("Search"), value: params[:q]
= f.check_box :multi_search, label: "Multisearch"
- 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
- when Answer
= render "answerbox", a: result, display_all: false, subscribed_answer_ids: []
- when Question
= render "shared/question", q: result, type: nil

= render 'shared/links'

:ruby
provide(:title, generate_title('Search'))
parent_layout 'base'
8 changes: 8 additions & 0 deletions config/initializers/meilisearch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

return unless ENV["SEARCH_ENABLED"] == "true"

MeiliSearch::Rails.configuration = {
meilisearch_url: ENV.fetch("MEILISEARCH_HOST", "http://localhost:7700"),
meilisearch_api_key: ENV.fetch("MEILISEARCH_API_KEY", "justfordev42069e621")
}
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
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'

Check notice on line 1 in spec/helpers/search_helper_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/helpers/search_helper_spec.rb#L1 <Style/FrozenStringLiteralComment>

Missing frozen string literal comment.
Raw output
spec/helpers/search_helper_spec.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.

Check notice on line 1 in spec/helpers/search_helper_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/helpers/search_helper_spec.rb#L1 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
spec/helpers/search_helper_spec.rb:1:9: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

# 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'

Check notice on line 1 in spec/requests/search_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/requests/search_spec.rb#L1 <Style/FrozenStringLiteralComment>

Missing frozen string literal comment.
Raw output
spec/requests/search_spec.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.

Check notice on line 1 in spec/requests/search_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/requests/search_spec.rb#L1 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
spec/requests/search_spec.rb:1:9: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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

Check notice on line 11 in spec/requests/search_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/requests/search_spec.rb#L10-L11 <Layout/EmptyLinesAroundBlockBody>

Extra empty line detected at block body end.
Raw output
spec/requests/search_spec.rb:10:1: C: Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body 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'

Check notice on line 1 in spec/views/search/index.html.erb_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/views/search/index.html.erb_spec.rb#L1 <Style/FrozenStringLiteralComment>

Missing frozen string literal comment.
Raw output
spec/views/search/index.html.erb_spec.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.

Check notice on line 1 in spec/views/search/index.html.erb_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/views/search/index.html.erb_spec.rb#L1 <Style/StringLiterals>

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Raw output
spec/views/search/index.html.erb_spec.rb:1:9: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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