Module: Dnif

Extended by:
Dnif
Included in:
Dnif
Defined in:
lib/dnif/multi_attribute.rb,
lib/dnif.rb,
lib/dnif/index.rb,
lib/dnif/schema.rb,
lib/dnif/search.rb,
lib/dnif/indexer.rb,
lib/dnif/railtie.rb,
lib/dnif/version.rb,
lib/dnif/document.rb,
lib/dnif/configuration.rb

Overview

Defined Under Namespace

Modules: Indexer, MultiAttribute, Search, Version Classes: Configuration, Document, Index, Railtie, Schema

Instance Method Summary collapse

Instance Method Details

#clientObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dnif.rb', line 80

def client
  searchd = Dnif::Configuration.options_for("searchd", config_path)
  if searchd["listen"]
    address, port = searchd["listen"].split(":")
  else
    address = searchd["address"] || "127.0.0.1"
    port = searchd["port"] || 3313
  end

  @client ||= Riddle::Client.new(address, port)
end

#config_pathObject



92
93
94
95
# File 'lib/dnif.rb', line 92

def config_path
  Dnif.root_path ||= File.expand_path(File.dirname("."))
  File.join(Dnif.root_path, "config/sphinx", Dnif.environment + ".erb")
end

#environmentObject Also known as: env



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

def environment
  @environment || 'development'
end

#environment=(value) ⇒ Object Also known as: env=



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

def environment=(value)
  @environment = value
end

#load_modelsObject



41
42
43
44
45
46
47
# File 'lib/dnif.rb', line 41

def load_models
  models = Dir["#{self.models_path}/*.rb"]
  models.map! do |filename|
    filename = File.basename(filename, '.rb')
    filename.classify.constantize
  end
end

#models_pathObject



33
34
35
# File 'lib/dnif.rb', line 33

def models_path
  @models_path
end

#models_path=(value) ⇒ Object



37
38
39
# File 'lib/dnif.rb', line 37

def models_path=(value)
  @models_path = value
end

#root_pathObject



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

def root_path
  @root_path
end

#root_path=(value) ⇒ Object



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

def root_path=(value)
  @root_path = value
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dnif.rb', line 49

def search(query, options = {})
  options.reverse_merge!(:index => '*')

  if options[:classes]
    ids = if options[:classes].is_a?(Array)
      options[:classes].map { |name| ActiveRecord::Base.indexes.keys.index(name) }
    else
      [ActiveRecord::Base.indexes.keys.index(options[:classes])]
    end

    client.filters = [Riddle::Client::Filter.new("class_id", ids)]
  end

  results = client.query(query, options[:index])
  raise results[:error] if results[:error]

  models = {}
  results[:matches].each do |match|
    encoded_class_name = match[:attributes]["class_name"].split(',').flatten
    class_name = Dnif::MultiAttribute.decode(encoded_class_name)

    models[class_name] ||= []
    models[class_name] << (match[:doc] - encoded_class_name.sum { |c| c.to_i })
  end

  models.map do |class_name, ids|
    klass = class_name.constantize
    class_name.constantize.where("#{klass.primary_key} IN (?)", ids)
  end.flatten
end