Class: SolrLite::Solr
- Inherits:
-
Object
- Object
- SolrLite::Solr
- Defined in:
- lib/solr.rb
Overview
Represents a Solr instance. This is the main public interface to submit commands (get, search, delete, update) to Solr.
Instance Attribute Summary collapse
-
#def_type ⇒ Object
- String
-
Set this value if you want to send a query parser (defType) attribute to Solr when submitting commands.
Instance Method Summary collapse
-
#delete_all! ⇒ SolrLite::Response
Deletes all documents in Solr.
-
#delete_by_id(id) ⇒ SolrLite::Response
Deletes a Solr document by id.
-
#delete_by_query(query) ⇒ SolrLite::Response
Deletes all documents that match a query in Solr.
-
#get(id, q_field = "q", fl = "*") ⇒ Hash
Fetches a Solr document by id.
- #get_many(ids, q_field = "q", fl = "*", batch_size = 20) ⇒ Object
-
#initialize(solr_url, logger = nil) ⇒ Solr
constructor
Creates an instance of the Solr class.
-
#search(params, extra_fqs = [], qf = nil, mm = nil, debug = false) ⇒ SolrLite::Response
Issues a search request to Solr.
- #search_group(params, extra_fqs = [], qf = nil, mm = nil, debug = false, group_field = nil, group_limit = nil, group_extra = nil) ⇒ Object
-
#search_text(terms) ⇒ SolrLite::Response
Shortcut for the ‘search` method.
-
#start_row(page, page_size) ⇒ Integer
Calculates the starting row for a given page and page size.
-
#update(json) ⇒ SolrLite::Response
Issues an update to Solr with the data provided.
Constructor Details
#initialize(solr_url, logger = nil) ⇒ Solr
Creates an instance of the Solr class.
30 31 32 33 34 35 |
# File 'lib/solr.rb', line 30 def initialize(solr_url, logger = nil) raise "No solr_url was indicated" if solr_url == nil @solr_url = solr_url @logger = logger @def_type = nil end |
Instance Attribute Details
#def_type ⇒ Object
- String
-
Set this value if you want to send a query parser (defType) attribute to Solr
when submitting commands. Leave as nil to use the value configured on the server.
21 22 23 |
# File 'lib/solr.rb', line 21 def def_type @def_type end |
Instance Method Details
#delete_all! ⇒ SolrLite::Response
Deletes all documents in Solr.
172 173 174 |
# File 'lib/solr.rb', line 172 def delete_all!() delete_by_query("*:*") end |
#delete_by_id(id) ⇒ SolrLite::Response
Deletes a Solr document by id.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/solr.rb', line 143 def delete_by_id(id) # Use XML format here because that's the only way I could get # the delete to recognize ids with a colon (e.g. bdr:123). # Using JSON caused the Solr parser to choke. # # Notice that they payload is XML but the response is JSON (wt=json) url = @solr_url + "/update?commit=true&wt=json" payload = "<delete><id>#{id}</id></delete>" http_response = http_post(url, payload, "text/xml") solr_response = Response.new(JSON.parse(http_response), nil) solr_response end |
#delete_by_query(query) ⇒ SolrLite::Response
Deletes all documents that match a query in Solr.
161 162 163 164 165 166 |
# File 'lib/solr.rb', line 161 def delete_by_query(query) url = @solr_url + "/update?commit=true" payload = '{ "delete" : { "query" : "' + query + '" } }' solr_response = http_post_json(url, payload) solr_response end |
#get(id, q_field = "q", fl = "*") ⇒ Hash
Fetches a Solr document by id.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/solr.rb', line 46 def get(id, q_field = "q", fl = "*") query_string = "#{q_field}=id%3A#{id}" # %3A => : query_string += "&fl=#{fl}" query_string += "&wt=json&indent=on" if @def_type != nil query_string += "&defType=#{@def_type}" end url = "#{@solr_url}/select?#{query_string}" solr_response = Response.new(http_get(url), nil) if solr_response.num_found > 1 raise "More than one record found for id #{id}" end solr_response.solr_docs.first end |
#get_many(ids, q_field = "q", fl = "*", batch_size = 20) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/solr.rb', line 61 def get_many(ids, q_field = "q", fl = "*", batch_size = 20) data = [] batches = to_batches(ids, batch_size) batches.each do |batch| ids_string = batch.join(" OR ") query_string = "#{q_field}=id%3A(#{ids_string})" # %3A => : query_string += "&fl=#{fl}" query_string += "&rows=#{batch_size}" query_string += "&wt=json&indent=on" if @def_type != nil query_string += "&defType=#{@def_type}" end url = "#{@solr_url}/select?#{query_string}" solr_response = Response.new(http_get(url), nil) data += solr_response.solr_docs end data end |
#search(params, extra_fqs = [], qf = nil, mm = nil, debug = false) ⇒ SolrLite::Response
Issues a search request to Solr.
92 93 94 95 96 |
# File 'lib/solr.rb', line 92 def search(params, extra_fqs = [], qf = nil, mm = nil, debug = false) http_response = search_core(params, extra_fqs, qf, mm, debug, nil, 0) response = Response.new(http_response, params) response end |
#search_group(params, extra_fqs = [], qf = nil, mm = nil, debug = false, group_field = nil, group_limit = nil, group_extra = nil) ⇒ Object
98 99 100 101 102 |
# File 'lib/solr.rb', line 98 def search_group(params, extra_fqs = [], qf = nil, mm = nil, debug = false, group_field = nil, group_limit = nil, group_extra = nil) http_response = search_core(params, extra_fqs, qf, mm, debug, group_field, group_limit, group_extra) response = Response.new(http_response, params) response end |
#search_text(terms) ⇒ SolrLite::Response
Shortcut for the ‘search` method.
109 110 111 112 |
# File 'lib/solr.rb', line 109 def search_text(terms) params = SearchParams.new(terms) search(params) end |
#start_row(page, page_size) ⇒ Integer
Calculates the starting row for a given page and page size.
121 122 123 |
# File 'lib/solr.rb', line 121 def start_row(page, page_size) (page - 1) * page_size end |
#update(json) ⇒ SolrLite::Response
Issues an update to Solr with the data provided.
131 132 133 134 135 |
# File 'lib/solr.rb', line 131 def update(json) url = @solr_url + "/update?commit=true" solr_response = http_post_json(url, json) solr_response end |