Class: Sunspot::Query::Connective::Abstract
- Inherits:
-
Object
- Object
- Sunspot::Query::Connective::Abstract
- Includes:
- Filter
- Defined in:
- lib/sunspot/query/connective.rb
Overview
Base class for connectives (conjunctions and disjunctions).
Direct Known Subclasses
Instance Method Summary collapse
-
#add_component(component) ⇒ Object
Add an arbitrary component to the conjunction, and return it.
-
#add_conjunction ⇒ Object
Add a new conjunction and return it.
-
#add_disjunction ⇒ Object
Add a new disjunction and return it.
-
#add_negated_restriction(field, restriction_type, value) ⇒ Object
Add a negated restriction.
-
#add_negated_shorthand_restriction(field, value) ⇒ Object
Add a negated shorthand restriction (see add_shorthand_restriction).
-
#add_restriction(field, restriction_type, value, negated = false) ⇒ Object
Add a restriction to the connective.
-
#add_shorthand_restriction(field, value, negated = false) ⇒ Object
Add a shorthand restriction; the restriction type is determined by the value.
-
#initialize(negated = false) ⇒ Abstract
constructor
:nodoc:.
-
#negate ⇒ Object
Returns a new connective that’s a negated version of this one.
-
#negated? ⇒ Boolean
Connectives can be negated during the process of denormalization that is performed when a disjunction contains a negated component.
-
#to_boolean_phrase ⇒ Object
Express the connective as a Lucene boolean phrase.
Methods included from Filter
Constructor Details
#initialize(negated = false) ⇒ Abstract
:nodoc:
10 11 12 13 |
# File 'lib/sunspot/query/connective.rb', line 10 def initialize(negated = false) #:nodoc: @negated = negated @components = [] end |
Instance Method Details
#add_component(component) ⇒ Object
Add an arbitrary component to the conjunction, and return it. The component must respond to #to_boolean_phrase
69 70 71 72 |
# File 'lib/sunspot/query/connective.rb', line 69 def add_component(component) @components << component component end |
#add_conjunction ⇒ Object
Add a new conjunction and return it.
54 55 56 |
# File 'lib/sunspot/query/connective.rb', line 54 def add_conjunction add_component(Conjunction.new) end |
#add_disjunction ⇒ Object
Add a new disjunction and return it.
61 62 63 |
# File 'lib/sunspot/query/connective.rb', line 61 def add_disjunction add_component(Disjunction.new) end |
#add_negated_restriction(field, restriction_type, value) ⇒ Object
Add a negated restriction. The added restriction will match all documents who do not match the terms of the restriction.
40 41 42 |
# File 'lib/sunspot/query/connective.rb', line 40 def add_negated_restriction(field, restriction_type, value) add_restriction(field, restriction_type, value, true) end |
#add_negated_shorthand_restriction(field, value) ⇒ Object
Add a negated shorthand restriction (see add_shorthand_restriction)
47 48 49 |
# File 'lib/sunspot/query/connective.rb', line 47 def add_negated_shorthand_restriction(field, value) add_shorthand_restriction(field, value, true) end |
#add_restriction(field, restriction_type, value, negated = false) ⇒ Object
Add a restriction to the connective.
18 19 20 |
# File 'lib/sunspot/query/connective.rb', line 18 def add_restriction(field, restriction_type, value, negated = false) add_component(restriction_type.new(field, value, negated)) end |
#add_shorthand_restriction(field, value, negated = false) ⇒ Object
Add a shorthand restriction; the restriction type is determined by the value.
26 27 28 29 30 31 32 33 34 |
# File 'lib/sunspot/query/connective.rb', line 26 def add_shorthand_restriction(field, value, negated = false) restriction_type = case value when Array then Restriction::AnyOf when Range then Restriction::Between else Restriction::EqualTo end add_restriction(field, restriction_type, value, negated) end |
#negate ⇒ Object
Returns a new connective that’s a negated version of this one.
108 109 110 111 112 113 114 |
# File 'lib/sunspot/query/connective.rb', line 108 def negate negated = self.class.new(!negated?) @components.each do |component| negated.add_component(component) end negated end |
#negated? ⇒ Boolean
Connectives can be negated during the process of denormalization that is performed when a disjunction contains a negated component. This method conforms to the duck type for all boolean query components.
101 102 103 |
# File 'lib/sunspot/query/connective.rb', line 101 def negated? @negated end |
#to_boolean_phrase ⇒ Object
Express the connective as a Lucene boolean phrase.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sunspot/query/connective.rb', line 77 def to_boolean_phrase #:nodoc: unless @components.empty? phrase = if @components.length == 1 @components.first.to_boolean_phrase else component_phrases = @components.map do |component| component.to_boolean_phrase end "(#{component_phrases.join(" #{connector} ")})" end if negated? "-#{phrase}" else phrase end end end |