Class: Sunspot::Query::Connective::Abstract

Inherits:
Object
  • Object
show all
Includes:
Filter
Defined in:
lib/sunspot/query/connective.rb

Overview

Base class for connectives (conjunctions and disjunctions).

Direct Known Subclasses

Conjunction, Disjunction

Instance Method Summary collapse

Methods included from Filter

#tag, #to_filter_query

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



84
85
86
87
# File 'lib/sunspot/query/connective.rb', line 84

def add_component(component)
  @components << component
  component
end

#add_conjunctionObject

Add a new conjunction and return it.



69
70
71
# File 'lib/sunspot/query/connective.rb', line 69

def add_conjunction
  add_component(Conjunction.new)
end

#add_disjunctionObject

Add a new disjunction and return it.



76
77
78
# File 'lib/sunspot/query/connective.rb', line 76

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.



55
56
57
# File 'lib/sunspot/query/connective.rb', line 55

def add_negated_restriction(field, restriction_type, value)
  add_restriction(true, field, restriction_type, value)
end

#add_negated_shorthand_restriction(field, value) ⇒ Object

Add a negated shorthand restriction (see add_shorthand_restriction)



62
63
64
# File 'lib/sunspot/query/connective.rb', line 62

def add_negated_shorthand_restriction(field, value)
  add_shorthand_restriction(true, field, value)
end

#add_positive_restriction(field, restriction_type, value) ⇒ Object

Add a positive restriction. The restriction will match all documents who match the terms fo the restriction.



40
41
42
# File 'lib/sunspot/query/connective.rb', line 40

def add_positive_restriction(field, restriction_type, value)
  add_restriction(false, field, restriction_type, value)
end

#add_positive_shorthand_restriction(field, value) ⇒ Object

Add a positive shorthand restriction (see add_shorthand_restriction)



47
48
49
# File 'lib/sunspot/query/connective.rb', line 47

def add_positive_shorthand_restriction(field, value)
  add_shorthand_restriction(false, field, value)
end

#add_restriction(negated, field, restriction_type, *value) ⇒ Object

Add a restriction to the connective.



18
19
20
# File 'lib/sunspot/query/connective.rb', line 18

def add_restriction(negated, field, restriction_type, *value)
  add_component(restriction_type.new(negated, field, *value))
end

#add_shorthand_restriction(negated, field, value) ⇒ 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(negated, field, value)
  restriction_type =
    case value
    when Array then Restriction::AnyOf
    when Range then Restriction::Between
    else Restriction::EqualTo
    end
  add_restriction(negated, field, restriction_type, value)
end

#negateObject

Returns a new connective that’s a negated version of this one.



123
124
125
126
127
128
129
# File 'lib/sunspot/query/connective.rb', line 123

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.

Returns:

  • (Boolean)


116
117
118
# File 'lib/sunspot/query/connective.rb', line 116

def negated?
  @negated
end

#to_boolean_phraseObject

Express the connective as a Lucene boolean phrase.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sunspot/query/connective.rb', line 92

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