Class: SolrLite::Solr

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(solr_url, logger = nil) ⇒ Solr

Creates an instance of the Solr class.

Parameters:

  • solr_url (String)

    URL to Solr, e.g. “localhost:8983/solr/bibdata

  • logger (Object) (defaults to: nil)

    An object that provides an ‘info` method to log information about the requests. It could be an instance of Rails::logger if using Rails or an instance of SolrLite::DefaultLogger that outputs to the console. Use `nil` to omit logging.



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_typeObject

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.

Returns:



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.

Parameters:

  • id (String)

    ID of the document to delete.

Returns:



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.

Parameters:

  • query (String)

    The query to pass to Solr.

Returns:



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.

Parameters:

  • id (String)

    ID of the document to fetch.

  • q_field (String) (defaults to: "q")

    Query field.

  • fl (String) (defaults to: "*")

    List of fields to fetch.

Returns:

  • (Hash)

    The document found or nil if no document was found. Raises an exception if more than one document was found.



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.

Parameters:

  • params (SolrLite::SearchParams)

    Search parameters.

  • extra_fqs (Array) (defaults to: [])

    Array of FilterQuery objects. This is used to add filters to the search that we don’t want to allow the user to override.

  • qf (String) (defaults to: nil)

    Use to override the server’s qf value.

  • mm (String) (defaults to: nil)

    Use to override the server’s mm value.

  • debug (Bool) (defaults to: false)

    Set to ‘true` to include `debugQuery` info in the response.

Returns:



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.

Parameters:

  • terms (String)

    the value to use as the query (q) in Solr.

Returns:



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.

Parameters:

  • page (Integer)

    Page number.

  • page_size (Integer)

    Number of documents per page.

Returns:

  • (Integer)

    The row number to pass to Solr to start at the given page.



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.

Parameters:

  • json (String)

    String the data in JSON format to sent to Solr. Usually in the form ‘“[{ f1:v1, f2:v2 }, { f1:v3, f2:v4 }]”`

Returns:



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