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.



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

def host
  @host
end

.portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



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

def filters
  @filters
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#add_filters!(filters) ⇒ Object



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

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



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

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

#countObject



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

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

#geo_anchor!(opts) ⇒ Object



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

def geo_anchor!(opts)
  @geo = opts
end

#id_fieldObject



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

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

#idsObject



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

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

#indexObject



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

def index
  @index ||= '*'
end

#max_query_timeObject



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

def max_query_time
  @max_query_time || MAX_QUERY_TIME
end

#max_query_time!(seconds) ⇒ Object



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

def max_query_time!(seconds)
  @max_query_time = seconds
end

#max_resultsObject



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

def max_results
  @max_results || MAX_RESULTS
end

#max_results!(max) ⇒ Object



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

def max_results!(max)
  @max_results = max
end

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



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

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



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

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



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

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

#sizeObject



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

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

#total_countObject



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

def total_count
  response['total_found']
end

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



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

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