Patching Discourse's search

I’m writing a plugin which stores some extra data against each user (similar to discourse-user-notes, but not staff-only), and would like to be able to search against this.

Presumably this involves patching something in lib/search.rb. I’m aware the Algolia search plugin overrides what appears in the search box popup, but ideally, I’d like this to affect the actual results page too.

Does anyone have any suggestions on where to start?

3 Likes

You can extend search from your plugin, for example you could add an advanced filter like this:

  require_dependency 'search'
  if Search.respond_to? :advanced_filter
    Search.advanced_filter(/with:video/) do |posts|
      posts.where("posts.cooked LIKE '%<video %'")
    end
  end

The above will only display posts containing videos when the search box includes the with:video keyword. See also the badge advanced filter for an example of a search query that takes user data into account.

9 Likes

Nice. Does adding an advanced filter mean that these results will also appear in a general (non-advanced-filter) search?

Yes, you can use an advanced keyword in the search popup as well as the full page search.

2 Likes

We don’t really need to carry this protection anymore, that api has been in place for a real long time.

3 Likes

On the subject, will PluginStore scale well here? I imagine this’ll lead to a lot of full scans (in the absence of a LIKE-friendly index on keys), so would it be better to create a 1-1 table for the extra user data?

PluginStore is to be avoided, roll out tables for your plugin.

2 Likes