2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-06 20:14:10 -04:00
|
|
|
module Gitlab
|
|
|
|
module Search
|
|
|
|
class ParsedQuery
|
2020-08-17 14:10:01 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-06-06 20:14:10 -04:00
|
|
|
attr_reader :term, :filters
|
|
|
|
|
|
|
|
def initialize(term, filters)
|
|
|
|
@term = term
|
|
|
|
@filters = filters
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_results(results)
|
2020-08-17 14:10:01 -04:00
|
|
|
with_matcher = ->(filter) { filter[:matcher].present? }
|
|
|
|
|
|
|
|
excluding = excluding_filters.select(&with_matcher)
|
|
|
|
including = including_filters.select(&with_matcher)
|
|
|
|
|
|
|
|
return unless excluding.any? || including.any?
|
|
|
|
|
|
|
|
results.select! do |result|
|
|
|
|
including.all? { |filter| filter[:matcher].call(filter, result) }
|
|
|
|
end
|
|
|
|
|
|
|
|
results.reject! do |result|
|
|
|
|
excluding.any? { |filter| filter[:matcher].call(filter, result) }
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def including_filters
|
|
|
|
processed_filters(:including)
|
|
|
|
end
|
|
|
|
|
|
|
|
def excluding_filters
|
|
|
|
processed_filters(:excluding)
|
|
|
|
end
|
|
|
|
|
|
|
|
def processed_filters(type)
|
|
|
|
excluding, including = strong_memoize(:processed_filters) do
|
|
|
|
filters.partition { |filter| filter[:negated] }
|
|
|
|
end
|
2018-06-06 20:14:10 -04:00
|
|
|
|
2020-08-17 14:10:01 -04:00
|
|
|
case type
|
|
|
|
when :including then including
|
|
|
|
when :excluding then excluding
|
|
|
|
else
|
|
|
|
raise ArgumentError.new(type)
|
2018-06-06 20:14:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 05:09:34 -04:00
|
|
|
|
|
|
|
Gitlab::Search::ParsedQuery.prepend_if_ee('EE::Gitlab::Search::ParsedQuery')
|