Class: DataMapper::Adapters::Sphinx::Query

Inherits:
Object
  • Object
show all
Includes:
Extlib::Assertions
Defined in:
lib/dm-sphinx-adapter/query.rb

Overview

Sphinx extended search query string from DataMapper query.

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Query

Initialize a new extended Sphinx query from a DataMapper::Query object.

If the query has no conditions an ” empty string will be generated possibly triggering Sphinx’s full scan mode.

See

Raises

NotImplementedError

DataMapper operators that can’t be expressed in the extended sphinx query syntax.

Parameters

query<DataMapper::Query>

DataMapper query object.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dm-sphinx-adapter/query.rb', line 24

def initialize(query)
  assert_kind_of 'query', query, DataMapper::Query
  @query  = []

  if query.conditions.empty?
    @query << ''
  else
    query.conditions.each do |operator, property, value|
      next if property.kind_of? Sphinx::Attribute # Filters are added elsewhere.
      normalized = normalize_value(value)
      field      = property.field(query.repository.name) unless operator == :raw
      @query << case operator
        when :eql, :like then '@%s "%s"'  % [field.to_s, normalized.join(' ')]
        when :not        then '@%s -"%s"' % [field.to_s, normalized.join(' ')]
        when :in         then '@%s (%s)'  % [field.to_s, normalized.map{|v| %{"#{v}"}}.join(' | ')]
        when :raw        then "#{property}"
        else raise NotImplementedError.new("Sphinx: Query fields do not support the #{operator} operator")
      end
    end
  end
end

Instance Method Details

#to_sObject

Returns

String

The extended sphinx query string.



48
49
50
# File 'lib/dm-sphinx-adapter/query.rb', line 48

def to_s
  @query.join(' ')
end