From 822a35b9d577539a63984ca81d6668ca3b8435fd Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 28 Aug 2023 10:31:51 +0200 Subject: [PATCH] Fix not being able to negate prefix clauses in search (#26672) --- app/lib/search_query_parser.rb | 2 +- app/lib/search_query_transformer.rb | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/lib/search_query_parser.rb b/app/lib/search_query_parser.rb index 15956d4cf..5d6ffbf29 100644 --- a/app/lib/search_query_parser.rb +++ b/app/lib/search_query_parser.rb @@ -9,7 +9,7 @@ class SearchQueryParser < Parslet::Parser rule(:prefix) { (term >> colon).as(:prefix) } rule(:shortcode) { (colon >> term >> colon.maybe).as(:shortcode) } rule(:phrase) { (quote >> (term >> space.maybe).repeat >> quote).as(:phrase) } - rule(:clause) { (prefix.maybe >> operator.maybe >> (phrase | term | shortcode)).as(:clause) } + rule(:clause) { (operator.maybe >> prefix.maybe >> (phrase | term | shortcode)).as(:clause) } rule(:query) { (clause >> space.maybe).repeat.as(:query) } root(:query) end diff --git a/app/lib/search_query_transformer.rb b/app/lib/search_query_transformer.rb index dad99cbd2..915f9c331 100644 --- a/app/lib/search_query_transformer.rb +++ b/app/lib/search_query_transformer.rb @@ -36,7 +36,11 @@ class SearchQueryTransformer < Parslet::Transform def clause_to_filter(clause) case clause when PrefixClause - { clause.type => { clause.filter => clause.term } } + if clause.negated? + { bool: { must_not: { clause.type => { clause.filter => clause.term } } } } + else + { clause.type => { clause.filter => clause.term } } + end else raise "Unexpected clause type: #{clause}" end @@ -81,7 +85,8 @@ class SearchQueryTransformer < Parslet::Transform class PrefixClause attr_reader :type, :filter, :operator, :term - def initialize(prefix, term) + def initialize(prefix, operator, term) + @negated = operator == '-' @operator = :filter case prefix @@ -114,6 +119,10 @@ class SearchQueryTransformer < Parslet::Transform end end + def negated? + @negated + end + private def account_id_from_term(term) @@ -132,7 +141,7 @@ class SearchQueryTransformer < Parslet::Transform operator = clause[:operator]&.to_s if clause[:prefix] - PrefixClause.new(prefix, clause[:term].to_s) + PrefixClause.new(prefix, operator, clause[:term].to_s) elsif clause[:term] TermClause.new(prefix, operator, clause[:term].to_s) elsif clause[:shortcode]