Class: ModelSet::SphinxQuery

Inherits:
Query
  • Object
show all
Includes:
Conditioned
Defined in:
lib/model_set/sphinx_query.rb

Defined Under Namespace

Classes: SphinxError

Constant Summary collapse

MAX_RESULTS =
1000
MAX_QUERY_TIME =
5
SORT_MODES =
{
  :relevance  => Sphinx::Client::SPH_SORT_RELEVANCE,
  :descending => Sphinx::Client::SPH_SORT_ATTR_DESC,
  :ascending  => Sphinx::Client::SPH_SORT_ATTR_ASC,
  :time       => Sphinx::Client::SPH_SORT_TIME_SEGMENTS,
  :extended   => Sphinx::Client::SPH_SORT_EXTENDED,
  :expression => Sphinx::Client::SPH_SORT_EXPR,
}
RANKING_MODES =
{
  :proximity_bm25 => Sphinx::Client::SPH_RANK_PROXIMITY_BM25,
  :bm25           => Sphinx::Client::SPH_RANK_BM25,
  :none           => Sphinx::Client::SPH_RANK_NONE,
  :word_count     => Sphinx::Client::SPH_RANK_WORDCOUNT,
  :proximity      => Sphinx::Client::SPH_RANK_PROXIMITY,
  :fieldmask      => Sphinx::Client::SPH_RANK_FIELDMASK,
  :sph04          => Sphinx::Client::SPH_RANK_SPH04,
  :total          => Sphinx::Client::SPH_RANK_TOTAL,
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Conditioned

#conditions

Attributes inherited from Query

#limit, #set_class, #sort_order

Instance Method Summary collapse

Methods included from Conditioned

#add_conditions!, #invert!, #to_conditions

Methods inherited from Query

#after_query, after_query, #before_query, before_query, #clear_cache!, #clear_limited_cache!, #initialize, #limit!, #limit_enabled?, #model_class, #model_name, #offset, on_exception, #on_exception, #page, #page!, #pages, #table_name, #unlimited!, #unsorted!

Constructor Details

This class inherits a constructor from ModelSet::Query

Class Attribute Details

.hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/model_set/sphinx_query.rb', line 12

def host
  @host
end

.portObject

Returns the value of attribute port.



12
13
14
# File 'lib/model_set/sphinx_query.rb', line 12

def port
  @port
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



14
15
16
# File 'lib/model_set/sphinx_query.rb', line 14

def filters
  @filters
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/model_set/sphinx_query.rb', line 14

def response
  @response
end

Instance Method Details

#add_filters!(filters) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/model_set/sphinx_query.rb', line 36

def add_filters!(filters)
  @filters ||= []

  filters.each do |key, value|
    next if value.nil?
    @empty = true if value.kind_of?(Array) and value.empty?
    @filters << [key, value]
  end
  clear_cache!
end

#anchor!(query) ⇒ Object



32
33
34
# File 'lib/model_set/sphinx_query.rb', line 32

def anchor!(query)
  add_filters!( id_field => query.ids.to_a )
end

#countObject



112
113
114
115
# File 'lib/model_set/sphinx_query.rb', line 112

def count
  fetch_results if @count.nil?
  @count
end

#geo_anchor!(opts) ⇒ Object



47
48
49
# File 'lib/model_set/sphinx_query.rb', line 47

def geo_anchor!(opts)
  @geo = opts
end

#id_fieldObject



126
127
128
129
130
131
132
# File 'lib/model_set/sphinx_query.rb', line 126

def id_field
  if set_class.respond_to?(:sphinx_id_field)
    set_class.sphinx_id_field
  else
    'id'
  end
end

#idsObject



121
122
123
124
# File 'lib/model_set/sphinx_query.rb', line 121

def ids
  fetch_results if @ids.nil?
  @ids
end

#indexObject



51
52
53
# File 'lib/model_set/sphinx_query.rb', line 51

def index
  @index ||= '*'
end

#max_query_timeObject



16
17
18
# File 'lib/model_set/sphinx_query.rb', line 16

def max_query_time
  @max_query_time || MAX_QUERY_TIME
end

#max_query_time!(seconds) ⇒ Object



20
21
22
# File 'lib/model_set/sphinx_query.rb', line 20

def max_query_time!(seconds)
  @max_query_time = seconds
end

#max_resultsObject



24
25
26
# File 'lib/model_set/sphinx_query.rb', line 24

def max_results
  @max_results || MAX_RESULTS
end

#max_results!(max) ⇒ Object



28
29
30
# File 'lib/model_set/sphinx_query.rb', line 28

def max_results!(max)
  @max_results = max
end

#order_by!(field, mode = :ascending) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/model_set/sphinx_query.rb', line 76

def order_by!(field, mode = :ascending)
  if field == :relevance
    @sort_order = [SORT_MODES[:relevance]]
  else
    raise "invalid mode: :#{mode}" unless SORT_MODES[mode]
    @sort_order = [SORT_MODES[mode], field.to_s]
  end
  clear_cache!
end

#rank_using!(mode_or_expr) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/model_set/sphinx_query.rb', line 97

def rank_using!(mode_or_expr)
  if mode_or_expr.nil?
    @ranking = nil
  elsif mode = RANKING_MODES[mode_or_expr]
    @ranking = [mode]
  else
    @ranking = [Sphinx::Client::SPH_RANK_EXPR, mode_or_expr]
  end
end

#select_fields!(*fields) ⇒ Object



63
64
65
# File 'lib/model_set/sphinx_query.rb', line 63

def select_fields!(*fields)
  @select = fields.flatten
end

#sizeObject



107
108
109
110
# File 'lib/model_set/sphinx_query.rb', line 107

def size
  fetch_results if @size.nil?
  @size
end

#total_countObject



117
118
119
# File 'lib/model_set/sphinx_query.rb', line 117

def total_count
  response['total_found']
end

#use_index!(index, opts = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/model_set/sphinx_query.rb', line 55

def use_index!(index, opts = {})
  if opts[:delta]
    @index = "#{index} #{index}_delta"
  else
    @index = index
  end
end