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

Ability to override search #343

Closed
kristyan opened this issue Mar 22, 2011 · 8 comments
Closed

Ability to override search #343

kristyan opened this issue Mar 22, 2011 · 8 comments
Milestone

Comments

@kristyan
Copy link

It would be great if we could override the default search mechanic, to allow for a customized search on a per model bases. For example, one way to do this would be to have a config setting such as


config.model Cars do
  search_method :search
end

or globally for all models


RailsAdmin.search_with_method :search 

This would mean in the case of the Car model, searches would be performed by calling:

Car.search query, offset, limit 

dynamically in preference to the default search. This would of course return a list of Car models which would be understood by rails admin and rendered in the search results.

This would be a very powerful feature as it would allow users to customize searches and potentially use search indexers such as Sphinx/Solr etc.. to perform the actual search in the background.

I would like to hear other any other comments/suggestions ?

@ogennadi
Copy link

+1

This would also be helpful in large databases for searching by id.

@billmccord
Copy link

+1 - I'm already running out of memory for some large datasets on Heroku that are put into drop-down boxes by default.

@nthj
Copy link

nthj commented Feb 4, 2012

+1 — using rails_admin as a contacts database, and it's brilliant, right out of the box. Only searching across associations is slow.

@kkxlkkxllb
Copy link

need custom search to filter result from mongodb data type Array

@abevoelker
Copy link

Big +1 on this. I love that there is a search function out of the box for every model, but I already have ElasticSearch indexing a few of my models so for them I would prefer to search using the tire gem's functions.

@Hungor
Copy link

Hungor commented Oct 21, 2013

Big +1 on this too.

@a-vakulenko
Copy link

Big +1 too!

@willybaer
Copy link

Hi there,
had the same problem that I needed a custom search action, cause we have over 6 Mil. Entries and we needed a Sunspot:Solr search for that.

My solution:
Add to the bottom of rails_admin.rb

 # /config/initializers/rails_admin.rb

RailsAdmin::MainController.class_eval do

# Override the list_entries method of the RailsAdmin::MainController
  def list_entries(model_config = @model_config, auth_scope_key = :index, additional_scope = get_association_scope_from_params, pagination = !(params[:associated_collection] || params[:all] || params[:bulk_ids]))
    scope = model_config.abstract_model.scoped
    if auth_scope = @authorization_adapter && @authorization_adapter.query(auth_scope_key, model_config.abstract_model)
      scope = scope.merge(auth_scope)
    end
    scope = scope.instance_eval(&additional_scope) if additional_scope
    if params[:query].present? and model_config.abstract_model.is_a?(YOUR_MODEL).inspect
      solr_search(model_config, scope, pagination)
    else
      get_collection(model_config, scope, pagination)
    end
  end

#Custom query if params[:query] is set and the model ist YOUR MODEL
  def solr_search(model_config, scope, pagination)

    search_term = ""
    params[:query].gsub(/[_\?\*\~\"]/, '').split(' ').each do |word|
      search_term += word + " "
    end
    search_term

    search = YOUR_MODEL.search do
      fulltext search_term, :fields => [:your_field]
      paginate :page => 1, :per_page => (params[:per] || model_config.list.items_per_page)
    end
    results = []
    search.each_hit_with_result do |hit, YOUR_MODEL|
      results.push YOUR_MODEL
    end
    return results
  end

Hope it helps someone to boost the search action of rails_admin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants