New search operator "not"


(Jeff Atwood) #8

That does not apply to the forum in question at

So they can easily kick off a search with negation by going to

(Katarzyna Bułat) #9

Hi, I am quite new to the project, but I can try to add this feature.

(Erlend Sogge Heggen) #10

That’s great @kasiabulat. Do you have a good idea of where to start with this or do you need some guidance?

(Katarzyna Bułat) #11

Thank you @erlend_sh for offering help! I think that the best and easiest way to do it would be to perform two searches:

  • Firstly, search for the term before the not part
  • Then, in results, search for the ones containing the unwanted phrase and filter them out

Is discourse/search-menu.js.es6 at 87c334c06365f273f39038d91ab92488c4757041 · discourse/discourse · GitHub the correct file to make changes in?

(cpradio) #12

The larger part of your changes will be in

(Katarzyna Bułat) #13

Thank you @cpradio for help with finding the correct file.

For the beginning, I added a simple version of search operator “not” (~) for tags. Currently it has the highest precedence and I implemented it as I described above. But I am wondering if it would be better for ‘+’ or ‘,’ to have higher precedence? And should it be possible to combine all operators? What way would be the best for users to construct their queries?

Pull request: Add search not operator by kasiabulat · Pull Request #5467 · discourse/discourse · GitHub

Filter topics without a specific tag
(Katarzyna Bułat) #14

I added some tests and comments about the performance issue to my pull request.

(Alan Tan) #15

I’m not sure about using ~ to represent NOT since it is usually used when matching against regular expressions. In Ruby, we use =~ /^some_regexp_pattern/ and it is similar in Postgres as well.


(Gerhard Schlager) #16

I agree that using ~ as operator probably isn’t a good idea.
@kasiabulat Why did you choose ~ instead of - as “not” operator?

I’d prefer tags:eggs-lunch+sandwiches or tags:eggs!lunch+sandwiches to tags:eggs~lunch+sandwiches

(Alan Tan) #17

Ah ha this was probably not chosen because - are allowed in tags.

(Gerhard Schlager) #18

Using quotes would help: tags:"boiled-eggs"-lunch+sandwiches

(Jeff Wong) #19

Probably not the best idea to require quotes as a naive search on tags:boiled-eggs would no longer do what you’d expect. Is there any conflict with !? That would be my preference here.

(Katarzyna Bułat) #20

Should I change it into “!”, then?

(Katarzyna Bułat) #21

I added a commit with the proper change: Add search not operator by kasiabulat · Pull Request #5467 · discourse/discourse · GitHub

(Katarzyna Bułat) #22

But it seems that after this change the code isn’t passing The Travis CI build (although it was passing it previously and I only changed the “~” symbol into “!” in few places)… Have I overlooked something? Had this job changed in the meantime maybe? Is “!” for sure a good symbol to use? Can pulling the repository change something as I haven’t updated it since the previous commit?

(Gerhard Schlager) #23

Thanks! Your changes look good to me.
We are having problems with Travis builds. The build failure is unrelated to your changes.

(Katarzyna Bułat) #24

What do you suggest I could work on now? Some time ago I added comments on the performance of my change on github. I wondered whether I should try to replace “NOT IN” with “NOT EXIST” (it would be faster then, but I found it not easy to do with active records), choose a completely different way of implementing the feature or leave it as it is. There’s also the new issue connected with search: Include translated posts in search?, so maybe I could take a look at it in the meantime?

(Alan Tan) #30

@kasiabulat Apologies for the lack of response from us as we’re trying to figure out internally what the search operator should look like. Do you think it’ll be hard to support sometime like tags:eggs -tags:lunch?

(Mittineague) #31

Do you know if there’s an easy way to swap the ispell / stemmer dictionaries used?

Even if so, would it not require more tables?

(Katarzyna Bułat) #32

@tgxworld Do you mean splitting on -tags: instead of -? So for more complex example: tags:eggs+beacon -tags:lunch,sandwiches it would search for posts with eggs and beacon tags, but without lunch and without sandwiches tags? Then, I think it would be a quick change.