Skip to content

Commit 0c88c60

Browse files
author
Thomas Scherz
committed
Replaces read_timout with timout in Solr initializer.
1 parent 5ef61ae commit 0c88c60

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

lib/active_fedora/solr_service.rb

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require 'rsolr'
2+
3+
module ActiveFedora
4+
class SolrService
5+
attr_reader :options
6+
attr_writer :conn
7+
8+
MAX_ROWS = 10_000
9+
10+
def initialize(options = {})
11+
@options = { timeout: 120, open_timeout: 120, url: 'http://localhost:8080/solr' }.merge(options)
12+
end
13+
14+
def conn
15+
@conn ||= RSolr.connect @options
16+
end
17+
18+
class << self
19+
# @param [Hash] options
20+
def register(options = {})
21+
ActiveFedora::RuntimeRegistry.solr_service = new(options)
22+
end
23+
24+
def reset!
25+
ActiveFedora::RuntimeRegistry.solr_service = nil
26+
end
27+
28+
def select_path
29+
ActiveFedora.solr_config.fetch(:select_path, 'select')
30+
end
31+
32+
def instance
33+
# Register Solr
34+
35+
unless ActiveFedora::RuntimeRegistry.solr_service
36+
register(ActiveFedora.solr_config)
37+
end
38+
39+
ActiveFedora::RuntimeRegistry.solr_service
40+
end
41+
42+
def get(query, args = {})
43+
args = args.merge(q: query, qt: 'standard')
44+
SolrService.instance.conn.get(select_path, params: args)
45+
end
46+
47+
def post(query, args = {})
48+
args = args.merge(q: query, qt: 'standard')
49+
SolrService.instance.conn.post(select_path, data: args)
50+
end
51+
52+
def query(query, args = {})
53+
Base.logger.warn "Calling ActiveFedora::SolrService.get without passing an explicit value for ':rows' is not recommended. You will end up with
54+
Solr's default (usually set to 10)\nCalled by #{caller[0]}" unless args.key?(:rows)
55+
method = args.delete(:method) || :get
56+
57+
result = case method
58+
when :get
59+
get(query, args)
60+
when :post
61+
post(query, args)
62+
else
63+
raise "Unsupported HTTP method for querying SolrService (#{method.inspect})"
64+
end
65+
result['response']['docs'].map do |doc|
66+
ActiveFedora::SolrHit.new(doc)
67+
end
68+
end
69+
70+
def delete(id)
71+
SolrService.instance.conn.delete_by_id(id, params: { 'softCommit' => true })
72+
end
73+
74+
# Get the count of records that match the query
75+
# @param [String] query a solr query
76+
# @param [Hash] args arguments to pass through to `args' param of SolrService.query (note that :rows will be overwritten to 0)
77+
# @return [Integer] number of records matching
78+
def count(query, args = {})
79+
args = args.merge(rows: 0)
80+
SolrService.get(query, args)['response']['numFound'].to_i
81+
end
82+
83+
# @param [Hash] doc the document to index, or an array of docs
84+
# @param [Hash] params
85+
# :commit => commits immediately
86+
# :softCommit => commit to memory, but don't flush to disk
87+
def add(doc, params = {})
88+
SolrService.instance.conn.add(doc, params: params)
89+
end
90+
91+
def commit
92+
SolrService.instance.conn.commit
93+
end
94+
end
95+
end # SolrService
96+
end # ActiveFedora

0 commit comments

Comments
 (0)