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



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

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

#add_conjunctionObject

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_disjunctionObject

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

#negateObject

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



112
113
114
115
116
117
118
# File 'lib/sunspot/query/connective.rb', line 112

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)


105
106
107
# File 'lib/sunspot/query/connective.rb', line 105

def negated?
  @negated
end

#to_boolean_phraseObject

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
95
96
97
98
# 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.each do |component|
          boolean_phrase = component.to_boolean_phrase
          component_phrases << boolean_phrase if boolean_phrase
        end
        unless component_phrases.empty?
          "(#{component_phrases.join(" #{connector} ")})"
        end
      end
    if negated?
      "-#{phrase}"
    else
      phrase
    end
  end
end