solr_lite 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/search_params.rb +97 -12
  3. data/lib/solr.rb +70 -19
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4130e04f0ee49e7d45b2bc833a4e9bec1b404f5f
4
- data.tar.gz: 8a812c8c94f95756cd6ba9001b9ff921e676dac6
2
+ SHA256:
3
+ metadata.gz: acee17a7d6d3d09c7777502658cbd008768fc8c08c16a474b5880dfa1fee9e08
4
+ data.tar.gz: 56fb2f341bf5ea7ca3729c1baa9501450b4176388c89f7f484600d1b03edee60
5
5
  SHA512:
6
- metadata.gz: d55a65d9d56e681d7e92e2297d82a067a3e49a10007665b88961e511c74a2b9bb3d6661c674ec7e9e230f1c81d06c7bbeb694b75e792f6a9202940cc7c0f8c41
7
- data.tar.gz: 4bea5f25c2caf7d525c48a2cacd98d8d460aa53ee7e0071cdaf4277e6b5ead40277c5b6b2533d01ecee9a5f6ac4664fa3352b863c4b72a749f7715ad97cbed50
6
+ metadata.gz: dae1e89e0f1bba8c6fa59b2a982890cb5240837b5c9593f17996f4af1079dc26307700e90342c1def2c75053ad08c95d6599d1825a9164cc914ac413f5538f14
7
+ data.tar.gz: 229cb68d206b08835ca786c1477a2600fa43ee453165d18b016823a6cc3fc79146307d64b7c96cf1f7a2b6113b73fcdf3e21ea87659d32cca1518153c6ce1c66
data/lib/search_params.rb CHANGED
@@ -1,12 +1,54 @@
1
1
  require "filter_query.rb"
2
2
  require "facet_field.rb"
3
3
  module SolrLite
4
+
5
+ # Represents the parameters to send to Solr during a search.
4
6
  class SearchParams
5
- attr_accessor :a, :q, :fq, :facets, :page, :page_size, :fl, :sort, :facet_limit,
6
- :spellcheck, :hl, :hl_fl, :hl_snippets
7
+
8
+ # [String] The q value to pass to Solr.
9
+ attr_accessor :q
10
+
11
+ # [Array] Array of {SolrLite::FilterQuery} objects to pass to Solr.
12
+ attr_accessor :fq
13
+
14
+ # [Array] Array of {SolrLite::FacetField} objects to pass to Solr.
15
+ attr_accessor :facets
16
+
17
+ # [Integer] Page number to request from Solr.
18
+ attr_accessor :page
19
+
20
+ # [Integer] Number of documents per page to request from Solr.
21
+ attr_accessor :page_size
22
+
23
+ # [String] List of fields to request from Solr.
24
+ attr_accessor :fl
25
+
26
+ # [String] Sort string to pass to Solr.
27
+ attr_accessor :sort
28
+
29
+ # [Integer] Number of facet values to request from Solr.
30
+ attr_accessor :facet_limit
31
+
32
+ # [Bool] True to request Solr to use spellchecking (defaults to false).
33
+ attr_accessor :spellcheck
34
+
35
+ # [Bool] Set to true to request hit highlighting information from Solr.
36
+ attr_accessor :hl
37
+
38
+ # [String] Sets the highlight fields (hl.fl) to request from Solr.
39
+ attr_accessor :hl_fl
40
+
41
+ # [Integer] Sets the number of hit highlights to request from Solr.
42
+ attr_accessor :hl_snippets
7
43
 
8
44
  DEFAULT_PAGE_SIZE = 20
9
45
 
46
+ # Creates an instance of the SearchParams class.
47
+ #
48
+ # @param q [String] The q value to use.
49
+ # @param fq [Array] An array of {SolrLite::FilterQuery} objects to pass to Solr.
50
+ # @param facets [Array] An array of {SolrLite::FacetField} objects to pass to Solr.
51
+ #
10
52
  def initialize(q = "", fq = [], facets = [])
11
53
  @q = q
12
54
  @fq = fq # array of FilterQuery
@@ -23,10 +65,21 @@ module SolrLite
23
65
  @hl_snippets = 1
24
66
  end
25
67
 
68
+ # Returns facet information about a given field.
69
+ #
70
+ # @param field [String] Name of the field.
71
+ # @return [SolrLite::FacetField] An object with the facet information.
72
+ #
26
73
  def facet_for_field(field)
27
74
  @facets.find {|f| f.name == field}
28
75
  end
29
76
 
77
+ # Sets the `remove_url` value for the given facet and value.
78
+ #
79
+ # @param field [String] Name of facet field.
80
+ # @param value [String] Value of the facet field.
81
+ # @param url [String] URL to set.
82
+ #
30
83
  def set_facet_remove_url(field, value, url)
31
84
  facet = facet_for_field(field)
32
85
  if facet != nil
@@ -34,10 +87,15 @@ module SolrLite
34
87
  end
35
88
  end
36
89
 
90
+ # Calculates the starting row number.
91
+ #
92
+ # @return [Integer] The starting row number for the current page and page_size.
93
+ #
37
94
  def start_row()
38
95
  (@page - 1) * @page_size
39
96
  end
40
97
 
98
+ # Sets the starting row number and recalculates the current page based on the current page_size.
41
99
  def star_row=(start)
42
100
  # recalculate the page
43
101
  if @page_size == 0
@@ -48,12 +106,17 @@ module SolrLite
48
106
  nil
49
107
  end
50
108
 
51
- # Returns the string that we need render on the Browser to execute
109
+ # Calculates the query string that we need render on the browser to execute
52
110
  # a search with the current parameters.
53
111
  #
54
- # facet_to_ignore: a FilterQuery object with a value to ignore when
55
- # creating the query string.
56
- # q_override: a string with a Solr query to use instead of the current q value
112
+ # @param facet_to_ignore [SolrLite::FilterQuery] Object with a specific facet to ignore when
113
+ # creating the query string. This is used to create the "remove this facet from the search"
114
+ # links that are shown to the user.
115
+ # @param q_override [String] The q value to use if we want to use a different value
116
+ # from the current one.
117
+ #
118
+ # @return [String] The string calculated.
119
+ #
57
120
  def to_user_query_string(facet_to_ignore = nil, q_override = nil)
58
121
  qs = ""
59
122
  q_value = q_override != nil ? q_override : @q
@@ -75,12 +138,21 @@ module SolrLite
75
138
  qs
76
139
  end
77
140
 
141
+ # Calculates the query string that we need render on the browser to execute
142
+ # a search with the current parameters and NO q parameter.
143
+ #
144
+ # @return [String] The string calculated.
145
+ #
78
146
  def to_user_query_string_no_q()
79
147
  to_user_query_string(nil, '')
80
148
  end
81
149
 
82
- # Returns the string that we need to pass Solr to execute a search
83
- # with the current parameters.
150
+ # Calculates the query string that needs to be passed to Solr to issue a search
151
+ # with the current search parameters.
152
+ #
153
+ # @param extra_fqs [Array] Array of {SolrLite::FilterQuery} objects to use.
154
+ # @return [String] Query string to pass to Solr for the current parameters.
155
+ #
84
156
  def to_solr_query_string(extra_fqs = [])
85
157
  qs = ""
86
158
  if @q != ""
@@ -123,10 +195,12 @@ module SolrLite
123
195
  qs
124
196
  end
125
197
 
126
- # Returns an array of values that can be added to an HTML form
127
- # to represent the current search parameters. Notice that we do
128
- # NOT include the `q` parameter there is typically an explicit
129
- # HTML form value for it on the form.
198
+ # Returns an array of values that can be added to an HTML form to represent the current
199
+ # search parameters. Notice that we do NOT include the `q` parameter because there is
200
+ # typically an explicit HTML form value for it on the form.
201
+ #
202
+ # @return [Array] An array of Hash objects with the values for the current search.
203
+ #
130
204
  def to_form_values()
131
205
  values = []
132
206
 
@@ -144,10 +218,21 @@ module SolrLite
144
218
  values
145
219
  end
146
220
 
221
+ # Returns a friendly string version of this object.
222
+ #
223
+ # @return [String] With information about the current search parameters.
224
+ #
147
225
  def to_s()
148
226
  "q=#{@q}\nfq=#{@fq}"
149
227
  end
150
228
 
229
+ # Creates a SearchParams object with the values in a query string.
230
+ # This is the inverse of `to_user_query_string()`.
231
+ #
232
+ # @param qs [String] A query string with the search parameters to use.
233
+ # @param facet_fields [Array] An array of {SolrLite::FacetField} to set in the returned object.
234
+ # @return [SolrLite::SearchParams] An object prepopulated with the values indicated in the query string.
235
+ #
151
236
  def self.from_query_string(qs, facet_fields = [])
152
237
  params = SearchParams.new
153
238
  params.facets = facet_fields
data/lib/solr.rb CHANGED
@@ -11,31 +11,45 @@ module SolrLite
11
11
  end
12
12
  end
13
13
 
14
+ # Represents a Solr instance.
15
+ # This is the main public interface to submit commands (get, search, delete, update) to Solr.
16
+ #
14
17
  class Solr
18
+
19
+ # [String] Set this value if you want to send a query parser (defType) attribute to Solr
20
+ # when submitting commands. Leave as nil to use the value configured on the server.
21
+ attr_accessor :def_type
22
+
15
23
  # Creates an instance of the Solr class.
16
- # Parameters:
17
- # solr_url: string with the URL to Solr ("http://localhost:8983/solr/bibdata")
18
- # logger: an instance of Rails::logger if using Rails.
19
- # Could also be SolrLite::DefaultLogger which defaults to the console.
20
- # Or nil to omit logging.
24
+ #
25
+ # @param solr_url [String] URL to Solr, e.g. "http://localhost:8983/solr/bibdata"
26
+ # @param logger [Object] An object that provides an `info` method to log information about the requests.
27
+ # It could be an instance of Rails::logger if using Rails or an instance of SolrLite::DefaultLogger
28
+ # that outputs to the console. Use `nil` to omit logging.
29
+ #
21
30
  def initialize(solr_url, logger = nil)
22
31
  raise "No solr_url was indicated" if solr_url == nil
23
32
  @solr_url = solr_url
24
33
  @logger = logger
34
+ @def_type = nil
25
35
  end
26
36
 
27
37
  # Fetches a Solr document by id.
28
- # Parameters:
29
- # id: ID of the document to fetch.
30
- # q_field: Query field (defaults to "q")
31
- # fl: list of fields to fetch (defaults to "*")
32
38
  #
33
- # Returns a hash with the document information or nil if no document was found.
34
- # Raises an exception if more than one document was found.
39
+ # @param id [String] ID of the document to fetch.
40
+ # @param q_field [String] Query field.
41
+ # @param fl [String] List of fields to fetch.
42
+ #
43
+ # @return [Hash] The document found or nil if no document was found.
44
+ # Raises an exception if more than one document was found.
45
+ #
35
46
  def get(id, q_field = "q", fl = "*")
36
47
  query_string = "#{q_field}=id%3A#{id}" # %3A => :
37
48
  query_string += "&fl=#{fl}"
38
49
  query_string += "&wt=json&indent=on"
50
+ if @def_type != nil
51
+ query_string += "&defType=#{@def_type}"
52
+ end
39
53
  url = "#{@solr_url}/select?#{query_string}"
40
54
  solr_response = Response.new(http_get(url), nil)
41
55
  if solr_response.num_found > 1
@@ -45,16 +59,17 @@ module SolrLite
45
59
  end
46
60
 
47
61
  # Issues a search request to Solr.
48
- # Parameters:
49
- # params: an instance of SolrParams.
50
- # extra_fqs: array of FilterQuery objects. This is used to
62
+ #
63
+ # @param params [SolrParams] Search parameters.
64
+ # @param extra_fqs [Array] Array of [SolrLite::FilterQuery] objects. This is used to
51
65
  # add filters to the search that we don't want to allow the
52
66
  # user to override.
53
- # qf: Used to override the server's qf value.
54
- # mm: Used to override the server's mm value.
55
- # debug: true to include debugQuery info in the response. (defaults to false)
67
+ # @param qf [String] Use to override the server's qf value.
68
+ # @param mm [String] Use to override the server's mm value.
69
+ # @param debug [Bool] Set to `true` to include `debugQuery` info in the response.
70
+ #
71
+ # @return [SolrLite::Response] The result of the search.
56
72
  #
57
- # Returns an instance of SolrLite::Response
58
73
  def search(params, extra_fqs = [], qf = nil, mm = nil, debug = false)
59
74
  if params.fl != nil
60
75
  query_string = "fl=#{params.fl.join(",")}"
@@ -78,28 +93,55 @@ module SolrLite
78
93
  query_string += "&debugQuery=true"
79
94
  end
80
95
 
96
+ if @def_type != nil
97
+ query_string += "&defType=#{@def_type}"
98
+ end
99
+
81
100
  url = "#{@solr_url}/select?#{query_string}"
82
101
  http_response = http_get(url)
83
102
  response = Response.new(http_response, params)
84
103
  response
85
104
  end
86
105
 
87
- # shortcut for search
106
+ # Shortcut for the `search` method.
107
+ #
108
+ # @param terms [String] the value to use as the query (q) in Solr.
109
+ # @return [SolrLite::Response] The result of the search.
110
+ #
88
111
  def search_text(terms)
89
112
  params = SearchParams.new(terms)
90
113
  search(params)
91
114
  end
92
115
 
116
+ # Calculates the starting row for a given page and page size.
117
+ #
118
+ # @param page [Integer] Page number.
119
+ # @param page_size [Integer] Number of documents per page.
120
+ #
121
+ # @return [Integer] The row number to pass to Solr to start at the given page.
122
+ #
93
123
  def start_row(page, page_size)
94
124
  (page - 1) * page_size
95
125
  end
96
126
 
127
+ # Issues an update to Solr with the data provided.
128
+ #
129
+ # @param json [String] String the data in JSON format to sent to Solr.
130
+ # Usually in the form `"[{ f1:v1, f2:v2 }, { f1:v3, f2:v4 }]"`
131
+ # @return [SolrLite::Response] The result of the update.
132
+ #
97
133
  def update(json)
98
134
  url = @solr_url + "/update?commit=true"
99
135
  solr_response = http_post_json(url, json)
100
136
  solr_response
101
137
  end
102
138
 
139
+
140
+ # Deletes a Solr document by id.
141
+ #
142
+ # @param id [String] ID of the document to delete.
143
+ # @return [SolrLite::Response] The result of the delete.
144
+ #
103
145
  def delete_by_id(id)
104
146
  # Use XML format here because that's the only way I could get
105
147
  # the delete to recognize ids with a colon (e.g. bdr:123).
@@ -113,6 +155,11 @@ module SolrLite
113
155
  solr_response
114
156
  end
115
157
 
158
+ # Deletes all documents that match a query in Solr.
159
+ #
160
+ # @param query [String] The query to pass to Solr.
161
+ # @return [SolrLite::Response] The result of the delete.
162
+ #
116
163
  def delete_by_query(query)
117
164
  url = @solr_url + "/update?commit=true"
118
165
  payload = '{ "delete" : { "query" : "' + query + '" } }'
@@ -120,6 +167,10 @@ module SolrLite
120
167
  solr_response
121
168
  end
122
169
 
170
+ # Deletes all documents in Solr.
171
+ #
172
+ # @return [SolrLite::Response] The result of the delete.
173
+ #
123
174
  def delete_all!()
124
175
  delete_by_query("*:*")
125
176
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solr_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hector Correa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-23 00:00:00.000000000 Z
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A lightweight gem to connect to Solr and run queries. Requires no extra
14
14
  dependencies.
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 2.5.2.1
50
+ rubygems_version: 2.7.7
51
51
  signing_key:
52
52
  specification_version: 4
53
53
  summary: A lightweight gem to connect to Solr and run queries