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

Inherits:
Scope
  • Object
show all
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 inherited from Scope

#add_component, #add_conjunction, #add_disjunction, #add_negated_restriction, #add_negated_shorthand_restriction, #add_restriction, #add_shorthand_restriction, #dynamic_query, #exclude_instance

Constructor Details

#initialize(setup, negated = false) ⇒ Abstract

:nodoc:



8
9
10
11
# File 'lib/sunspot/query/connective.rb', line 8

def initialize(setup, negated = false) #:nodoc:
  super(setup)
  @negated = negated
end

Instance Method Details

#negateObject

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



58
59
60
61
62
63
64
# File 'lib/sunspot/query/connective.rb', line 58

def negate
  negated = self.class.new(@setup, !negated?)
  for component in @components
    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)


51
52
53
# File 'lib/sunspot/query/connective.rb', line 51

def negated?
  @negated
end

#to_boolean_phraseObject

Express the connective as a Lucene boolean phrase.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sunspot/query/connective.rb', line 27

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

#to_paramsObject

Connective as solr params.



16
17
18
19
20
21
22
# File 'lib/sunspot/query/connective.rb', line 16

def to_params #:nodoc:
  if boolean_phrase = to_boolean_phrase
    { :fq => to_boolean_phrase }
  else
    {}
  end
end