New search operator "not"

search
pr-welcome

(Joshua Rosenfeld) #1

Over at Stonehearth we’ve started to have the want to search for topics that don’t have a certain tag on them, or that aren’t by a certain user. To the best of my understanding, this isn’t currently possible. As such, we’d like to request a negation operator for search, like Google support -.


Is it possible to search for a topic NOT in a category?
Search - Excluding keywords
Does Discourse search support a specific syntax?
Hide posts by tag (filter *out* not just filter *to*)
How do I exclude a category from the advanced search?
(Joshua Rosenfeld) #2

So this just came up naturally from another user at Stonehearth:

Seems he was trying to search for “frog”, but we have a very active user named “froggy” so he had a hard time searching for posts that had frog but weren’t mentions.


(Christopher Stone) #3

I’m amazed this topic didn’t get any more traction…

As the Keyboard Maestro forum gains more and more threads it’s becoming increasingly difficult to find things efficiently.

A negation operator would be helpful.

-Chris


(Jeff Atwood) #4

You can use negation in Google searches with the site: operator, so try that. Visit the 404 page (type any random gobbledygook as a URL past the domain name) to get a pre filled google search form.


(Sam Saffron) #5

It is actually a pretty straight forward change (add support for -test) which would be only about 10-20 lines of code including tests (as long as its not in the advanced page with a fancy UI). Going to put a #pr-welcome on this, I don’t think it would do any harm.


(Jeff Atwood) #6

It is super easy to search using Google with negation right now, just visit any 404 page and type the terms


(David Taylor) #7

Using google wont work for login-only forums, or secure categories


(Jeff Atwood) #8

That does not apply to the forum in question at https://forum.keyboardmaestro.com

So they can easily kick off a search with negation by going to https://forum.keyboardmaestro.com/nf


(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.

Screenshot%20from%202018-02-22%2018-03-34


(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?