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

feature: custom search drivers #3034

Merged
merged 5 commits into from
Jul 22, 2024
Merged
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
38 changes: 29 additions & 9 deletions app/controllers/avo/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ def search_resource(resource)
query: resource.query_scope
).handle

query = apply_scope(query) if should_apply_any_scope?

# Get the count
results_count = query.reselect(resource.model_class.primary_key).count

# Get the results
query = query.limit(search_results_count(resource))

results = apply_search_metadata(query, resource)
results_count, results = parse_results(query)

header = resource.plural_name

Expand Down Expand Up @@ -212,5 +204,33 @@ def search_results_count(resource)
Avo.configuration.search_results_count
end
end

def parse_results(query)
# When using custom search services query should return an array of hashes
if query.is_a?(Array)
# Apply highlight
query.map do |result|
result[:_label] = highlight(result[:_label].to_s, CGI.escapeHTML(params[:q] || ""))
end

# Force count to 0 until implement an API to pass the count
results_count = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this hidden to the user or can they see (0) in the search results?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is hidden. The count is added only when bigger than 0:

      if results_count > 0
        header = "#{header} (#{results_count})"
      end


# Apply the limit
results = query.first(search_results_count(resource))
else
query = apply_scope(query) if should_apply_any_scope?

# Get the count
results_count = query.reselect(resource.model_class.primary_key).count

# Get the results
query = query.limit(search_results_count(resource))

results = apply_search_metadata(query, resource)
end

[results_count, results]
end
end
end
52 changes: 52 additions & 0 deletions spec/features/avo/search_custom_results_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.feature Avo::SearchController, type: :controller do
let!(:project) { create :project }

it "active record search" do
get :show, params: {
resource_name: "projects"
}

expect(json["projects"]["results"].count).to eq 1
end

it "custom search" do
old_query = Avo::Resources::Project.search[:query]

limit = 8 # default
total_results = 9

Avo::Resources::Project.search[:query] = -> do
array = []

total_results.times do |iteration|
array << {
_id: iteration,
_label: "Label nr #{iteration}",
_url: "www.#{iteration}.ro"
}
end

array
end

get :show, params: {
resource_name: "projects"
}

expect(json["projects"]["results"].count).to eq limit

limit.times do |index|
expect(json["projects"]["results"][index]).to eq({
"_id" => index,
"_label" => "Label nr #{index}",
"_url" => "www.#{index}.ro"
})
end

Avo::Resources::Project.search[:query] = old_query
end
end
Loading