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개의 좋아요

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개의 좋아요

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개의 좋아요

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

6개의 좋아요

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개의 좋아요

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

2개의 좋아요

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 discourse/app/assets/javascripts/discourse/widgets/search-menu.js.es6 at 87c334c06365f273f39038d91ab92488c4757041 · discourse/discourse · GitHub the correct file to make changes in?

1개의 좋아요

The larger part of your changes will be in

11개의 좋아요

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

4개의 좋아요

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

5개의 좋아요

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.

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개의 좋아요

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개의 좋아요

Should I change it into “!”, then?

2개의 좋아요

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

6개의 좋아요