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

"Advanced" search #1218

Conversation

patrick-gleeson
Copy link

@patrick-gleeson patrick-gleeson commented Sep 19, 2018

Forgive a rather presumptuous PR. I know that #157 is open and there has not been a general agreement on exactly what smart searching would look like.

I just needed more advanced search functionality for a site I'm building, and so I built what I need, and I want to see if anyone else would find it useful.

How this works:

Suppose you have a User class with the following in its Administrate dashboard

ATTRIBUTE_TYPES = {
  name: Field::String,
  email: Field::String,
  activated: Field::Boolean
}

Search term: name:Steve

  • This will search for 'steve' in the name field

Search term: name:Steve example

  • This will search for 'steve' in the name field and 'example' in everything searchable except the name field

Search term: name:'Steve Jones'

  • This will search for 'steve jones' in the name field

Search term: activated:true

  • This will search for records with activated set to true

Search term: email:example.com name:"Steve Jones" activated:false hoopla

  • This will search for records where email matches 'example.com', name matches 'Steve Jones', activated is set to true, and it will ignore 'hoopla' because there are no fields left for it to search.

I look forward to any thoughts and feedback!

spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
spec/lib/administrate/search_spec.rb Outdated Show resolved Hide resolved
@conradbeach
Copy link
Contributor

@patrick-gleeson Does this allow filtering/searching on associations? For example, could I use it to view all records that belong to a certain parent record?

Thanks for taking the time to implement this. We recently added Administrate to our project and advanced searching is something we were planning on adding ourselves.

@patrick-gleeson
Copy link
Author

@conradbeach In its current form it doesn't allow specifying associations to search. It might be relatively straightforward to implement that, though - I just haven't done much with associations yet so didn't think to try to support it.

@nickcharlton nickcharlton added feature new functionality that’s not yet implemented search finding things through our models labels Jan 2, 2020
@mmrobins
Copy link

mmrobins commented Apr 2, 2021

Any word on getting something like this merged? This would be awesome to have

@pablobm
Copy link
Collaborator

pablobm commented Apr 16, 2021

Search in Administrate could definitely get some improvement, but perhaps not as super-smart default option, but rather by providing an easy way to provide your own.

The problem with implementing anything is that different people expect search to work differently in different situations. Take this PR as an example. When you search for name:Steve example, it "will search for 'steve' in the name field and 'example' in everything searchable except the name field". That's not what I would expect: I would expect a search for steve in the name field and for example across all fields with no exception.

Therefore I hesitate to accept any specific changes.

In my mind, search should be something for which Administrate provides a bare minimum default (eg: all words across all searchable fields, perhaps with quoted phrases), and users would provide their own custom algorithm by overriding controllers.

Currently, search is implemented in Administrate::ApplicationController#index. This is what this action looks like at the moment:

def index
authorize_resource(resource_class)
search_term = params[:search].to_s.strip
resources = Administrate::Search.new(scoped_resource,
dashboard_class,
search_term).run
resources = apply_collection_includes(resources)
resources = order.apply(resources)
resources = resources.page(params[:_page]).per(records_per_page)
page = Administrate::Page::Collection.new(dashboard, order: order)
render locals: {
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?,
}
end

Line 8 is the key. It will take the search query and return some results based on it. If you override this, you can implement whichever search you wish.

A simple first approach would be to move this line to a separate method in the same controller. This new method can read the query params (line 7) and perform the search (line 8), returning the results. Once a separate method, it should be more readily available for users to override.

Would anyone be up for providing a PR that does just that?

(Meanwhile, I'm afraid I'm going to close this PR for the reasons I explain above).

@pablobm pablobm closed this Apr 16, 2021
@pablobm pablobm mentioned this pull request Apr 22, 2021
@pldavid2
Copy link

Hello @pablobm , I'm rather new on administrate and was looking to have some kind of elasticsearch/searchkick integration on it. I understand your solution could help with that?
My first try was looking on the administrate plugin list but unfortunately it seems there is none for elasticsearch/searchkick, only for ransack.
Thanks

@pablobm
Copy link
Collaborator

pablobm commented Jun 10, 2021

I don't know anything about searchkick, but I'm going to guess that yes, it should work.

To override Administrate as I explain above, you can edit Admin::ApplicationController and provide your own version of index (or whatever method you want to change). You probably want to copy the original method and then tweak it to suit your needs.

However bear in mind that you'll have to double check that things still work whenever you upgrade Administrate. We change the implementation of controllers, templates, etc every now and then, and this may cause an incompatibility with your overrides, which will need to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature new functionality that’s not yet implemented search finding things through our models
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants