Class: Flare::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/flare/session.rb

Constant Summary collapse

PER_PAGE =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Session

Returns a new instance of Session.



5
6
7
# File 'lib/flare/session.rb', line 5

def initialize(url)
  @connection = RSolr.connect(:url => url)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/flare/session.rb', line 9

def connection
  @connection
end

Instance Method Details

#count(*args) ⇒ Object



27
28
29
# File 'lib/flare/session.rb', line 27

def count(*args)
  execute(*args)[:response][:numFound]
end

#index(*objects) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/flare/session.rb', line 31

def index(*objects)
  objects = ensure_searchable(objects)
  objects.collect(&:to_solr_doc).each do |doc|
    # connection.update(RSolr::Message::Generator.new.add(doc[:fields], doc[:attributes]))
    connection.update(RSolr::Message::Builder.new.add(doc[:fields], doc[:attributes]))
    # connection.add(doc[:fields], doc[:attributes])
  end
end

#index!(*objects) ⇒ Object



40
41
42
43
# File 'lib/flare/session.rb', line 40

def index!(*objects)
  index(objects)
  commit
end

#remove(*objects) ⇒ Object



45
46
47
48
# File 'lib/flare/session.rb', line 45

def remove(*objects)
  objects = ensure_searchable(objects)
  connection.delete_by_id(objects.collect(&:solr_document_id))
end

#remove!(*objects) ⇒ Object



50
51
52
53
# File 'lib/flare/session.rb', line 50

def remove!(*objects)
  remove(objects)
  commit
end

#remove_all(*types) ⇒ Object



55
56
57
58
# File 'lib/flare/session.rb', line 55

def remove_all(*types)
  types = Array(types).flatten.select { |type| type.searchable? }
  connection.delete_by_query(types.map { |type| "type:#{type.name}" }.join(' OR '))
end

#remove_all!(*types) ⇒ Object



60
61
62
63
64
# File 'lib/flare/session.rb', line 60

def remove_all!(*types)
  remove_all(types)
  commit
  optimize
end

#search(*args) ⇒ Object



20
21
22
23
24
25
# File 'lib/flare/session.rb', line 20

def search(*args)
  options = args.extract_options!
  ar_options = { :include => options.delete(:include) }
  response = execute(options)
  Flare::Collection.create_from_response(response, response[:request][:page], response[:request][:per_page], ar_options)
end

#search_for_ids(*args) ⇒ Object



13
14
15
16
17
18
# File 'lib/flare/session.rb', line 13

def search_for_ids(*args)
  options = args.extract_options!
  ar_options = { :include => options.delete(:include) }
  response = execute(*args)
  Flare::Collection.ids_from_response(response, response[:request][:page], response[:request][:per_page], response[:request])
end