Module: MongoMapper::Mixins::Indexer::ClassMethods

Defined in:
lib/mixins/indexer.rb

Instance Method Summary collapse

Instance Method Details

#by_fulltext_index(query, options = {}) ⇒ Object

Searches for an object of this model class (e.g. Post, Comment) and the requested query string. The query string may contain any query provided by Sphinx.

Call MongoMapper::Document.by_fulltext_index() to query without reducing to a single class type.

Parameters:

query

Query string like “foo @title bar”

options

Additional options to set

Options:

:match_mode

Optional Riddle match mode (defaults to :extended)

:limit

Optional Riddle limit (Riddle default)

:max_matches

Optional Riddle max_matches (Riddle default)

:sort_by

Optional Riddle sort order (also sets sort_mode to :extended)

:raw

Flag to return only IDs and do not lookup objects (defaults to false)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/mixins/indexer.rb', line 137

def by_fulltext_index(query, options = {})
  if self == Document
    client = Riddle::Client.new
  else
    client = Riddle::Client.new(fulltext_opts[:server],
             fulltext_opts[:port])

    query = query + " @classname #{self}"
  end

  client.match_mode = options[:match_mode] || :extended

  if (limit = options[:limit])
    client.limit = limit
  end

  if (max_matches = options[:max_matches])
    client.max_matches = matches
  end

  if (sort_by = options[:sort_by])
    client.sort_mode = :extended
    client.sort_by = sort_by
  end

  result = client.query(query)

  #TODO
  if result and result[:status] == 0 and (matches = result[:matches])
    classname = nil
    ids = matches.collect do |row|
      classname = MongoSphinx::MultiAttribute.decode(row[:attributes]['csphinx-class'])
      (classname + '-' + row[:doc].to_s) rescue nil
    end.compact

    return ids if options[:raw]
    return Object.const_get(classname).find(ids)
  else
    return []
  end
end

#fulltext_index(*keys) ⇒ Object

Method for enabling fulltext indexing and for defining the fields to include.

Parameters:

keys

Array of field keys to include plus options Hash

Options:

:server

Server name (defaults to localhost)

:port

Server port (defaults to 3312)

:idsize

Number of bits for the ID to generate (defaults to 32)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mixins/indexer.rb', line 95

def fulltext_index(*keys)
  opts = keys.pop if keys.last.is_a?(Hash)
  opts ||= {} # Handle some options: Future use... :-)

  # Save the keys to index and the options for later use in callback.
  # Helper method cattr_accessor is already bootstrapped by couchrest
  # gem. 

  cattr_accessor :fulltext_keys 
  cattr_accessor :fulltext_opts 

  self.fulltext_keys = keys
  self.fulltext_opts = opts

  # Overwrite setting of new ID to do something compatible with
  # Sphinx. If an ID already exists, we try to match it with our 
  # Schema and cowardly ignore if not.

  before_save :save_callback

end