# Patching Discourse's search

**URL:** https://meta.discourse.org/t/patching-discourses-search/157156
**Category:** Development
**Created:** [July 8, 2020, 3:09pm UTC](https://meta.discourse.org/t/patching-discourses-search/157156 "2020-07-08T15:09:02Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![snoopdouglas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snoopdouglas/32/186414_2.png) [@snoopdouglas](https://meta.discourse.org/u/snoopdouglas)
#### Post date: [July 8, 2020, 3:09pm UTC](https://meta.discourse.org/t/patching-discourses-search/157156/1 "2020-07-08T15:09:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [July 8, 2020, 6:24pm UTC](https://meta.discourse.org/t/patching-discourses-search/157156/2 "2020-07-08T18:24:17Z")

</div>

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

```ruby
  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](https://github.com/discourse/discourse/blob/master/lib/search.rb#L359-L359) for an example of a search query that takes user data into account.

---

<div class="post-metadata">

### Author: ![snoopdouglas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snoopdouglas/32/186414_2.png) [@snoopdouglas](https://meta.discourse.org/u/snoopdouglas)
#### Post date: [July 8, 2020, 7:14pm UTC](https://meta.discourse.org/t/patching-discourses-search/157156/3 "2020-07-08T19:14:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [July 8, 2020, 7:20pm UTC](https://meta.discourse.org/t/patching-discourses-search/157156/4 "2020-07-08T19:20:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [July 9, 2020, 7:53am UTC](https://meta.discourse.org/t/patching-discourses-search/157156/5 "2020-07-09T07:53:23Z")

</div>

> [@pmusaraj](#):
>
> `if Search.respond_to? :advanced_filter`

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

---

<div class="post-metadata">

### Author: ![snoopdouglas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/snoopdouglas/32/186414_2.png) [@snoopdouglas](https://meta.discourse.org/u/snoopdouglas)
#### Post date: [July 9, 2020, 8:49am UTC](https://meta.discourse.org/t/patching-discourses-search/157156/6 "2020-07-09T08:49:19Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [July 9, 2020, 8:53am UTC](https://meta.discourse.org/t/patching-discourses-search/157156/7 "2020-07-09T08:53:21Z")

</div>

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