New search operator "not" for tags only

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

17 Likes

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.

3 Likes

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

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.

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.

4 Likes

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

6 Likes

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

1 Like

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

2 Likes

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

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 https://github.com/discourse/discourse/blob/87c334c06365f273f39038d91ab92488c4757041/app/assets/javascripts/discourse/widgets/search-menu.js.es6 the correct file to make changes in?

1 Like

The larger part of your changes will be in
https://github.com/discourse/discourse/blob/master/lib/search.rb

11 Likes

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: https://github.com/discourse/discourse/pull/5467

4 Likes

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

5 Likes

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

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

1 Like

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

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

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.

4 Likes

Should I change it into “!”, then?

2 Likes

I added a commit with the proper change: https://github.com/discourse/discourse/pull/5467/commits/d01f064cf5c0a167876c1f9bd4730a3af951795d

6 Likes