New search operator "not" for tags only

AI 生成的摘要

A discussion on implementing a “not” search operator for tags in Discourse, initiated by jomaxro to address the need for excluding topics with specific tags or users. ccstone and others supported the idea, noting its utility in growing forums. kasiabulat proposed a solution, initially using ~ as the negation operator, but later switched to ! after community feedback, as ~ is commonly used in regex. The implementation involved changes to the search logic in search.rb and search-menu.js.es6, with a pull request (#5467) that was eventually merged. The discussion also covered syntax improvements, such as supporting tags:eggs -tags:lunch for more complex queries, and briefly touched on related issues like search performance and translated posts. The feature was confirmed as implemented in the final commit.

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 个赞