-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
nilsding
wants to merge
3
commits into
main
Choose a base branch
from
wir-suchen-dich
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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/ |
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,35 @@ | ||
class SearchController < ApplicationController | ||
def index | ||
@results = [] | ||
@query = params[:q] | ||
return if @query.blank? | ||
|
||
@results = if params[:multi_search] == "1" | ||
multi_search_experiment | ||
else | ||
[*Answer.search(@query), *Question.search(@query)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't take too long tbh (~2.7m documents)
|
||
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 | ||
)["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 | ||
h["hits"].map { |hit| [hit["_rankingScore"], results[hit["id"]]] } | ||
end | ||
.sort_by(&:first) | ||
.reverse | ||
.tap { |results| Rails.logger.debug(results) } | ||
.map(&:last) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module SearchHelper | ||
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
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,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' |
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,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") | ||
} |
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,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 |
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,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 |
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,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "search/index.html.erb", type: :view do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: