mwmitchell-rsolr-ext 0.6.1 → 0.7.0

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.
data/README.rdoc CHANGED
@@ -1,18 +1,14 @@
1
1
  =RSolr::Ext
2
2
  A set of helper methods/modules to assist in building Solr queries and handling responses when using the RSolr library.
3
3
 
4
- NOTE: The API for RSolr::Ext is pre 1.0. Things are changing quickly...
5
-
6
4
  ==Request Example
7
- std = RSolr::Ext::Request::Standard.new
8
-
9
- solr_params = std.map(
5
+ solr_params = RSolr::Ext.map_params(
10
6
  :page=>2,
11
7
  :per_page=>10,
12
8
  :phrases=>{:name=>'This is a phrase'},
13
9
  :filters=>['test', {:price=>(1..10)}],
14
10
  :phrase_filters=>{:manu=>['Apple']},
15
- :q=>'ipod',
11
+ :queries=>'ipod',
16
12
  :facets=>{:fields=>['cat', 'blah']}
17
13
  )
18
14
 
@@ -24,7 +20,7 @@ NOTE: The API for RSolr::Ext is pre 1.0. Things are changing quickly...
24
20
  rsolr = RSolr.connect
25
21
 
26
22
  raw_response = rsolr.select(:q=>'*:*)
27
- r = RSolr::Ext::Response::Standard.new(raw_response)
23
+ r = RSolr::Ext::wrap_response(raw_response)
28
24
 
29
25
  r.ok?
30
26
  r.params
@@ -32,11 +28,33 @@ NOTE: The API for RSolr::Ext is pre 1.0. Things are changing quickly...
32
28
  r.docs.previous_page
33
29
  r.docs.next_page
34
30
  r.facets
35
-
31
+
32
+ You can access values in the response hash using symbols or strings.
33
+
36
34
  ===Doc Pagination
37
35
  After creating a RSolr::Ext::Response object, pass-in the response.docs to the will_paginate view helper:
38
36
  rsolr = RSolr.connect
39
37
  raw_response = rsolr.select(:q=>'*:*)
40
- @response = RSolr::Ext::Response::Standard.new(raw_response)
38
+ @response = RSolr::Ext.wrap_response(raw_response)
41
39
  # in view:
42
- <%= will_paginate @response.docs %>
40
+ <%= will_paginate @response.docs %>
41
+
42
+ ==The Findable Module
43
+
44
+ You can get a modified RSolr.connect object by calling RSolr::Ext.connect.
45
+
46
+ The object returned is an RSolr::Connection::Adapter (Direct or HTTP) with additional methods attached, most notably #find, which comes from the RSolr::Ext::Findable module.
47
+
48
+ The #find method provides a convenient way to search solr. Here are some examples:
49
+
50
+ # q=jefferson - returns all docs
51
+ all_jefferson_docs = find 'jefferson'
52
+
53
+ # q=jefferson&rows=1 -- first doc only
54
+ a_single_jefferson_doc = find :first, 'jefferson'
55
+
56
+ # q=jefferson&fq=type:"book" - all docs
57
+ books_about_jefferson = find 'jefferson', :phrase_filters=>{:type=>'book'}
58
+
59
+ # q=something -- the entire response
60
+ solr_response = find {:q=>'something'}, :include_response=>true
@@ -0,0 +1,92 @@
1
+ # Findable can be mixed into whatever RSolr.connect returns.
2
+ # RSolr::Ext.connect() does this for you.
3
+ # Findable makes querying solr easier by providing a simple #find method.
4
+ #
5
+ # The #find method behaves differently depending on what you send in.
6
+ #
7
+ # The first argument can be either a symbol (:all, :first)
8
+ # OR
9
+ # a solr params hash
10
+ # OR
11
+ # a string that will be used for the query.
12
+ #
13
+ # If the first argument is a symbol, the second is used for solr params or the query.
14
+ #
15
+ # If the first argument is :first, then only a single document is returns (:rows=>0)
16
+ # If the first argument is :all, then all documents are returned
17
+ #
18
+ # If a hash is used for solr params, all of the normal RSolr::Ext::Request::Standard
19
+ # mappings are available (everything else gets passed to solr).
20
+ #
21
+ # If a string is used, it's set to the :q param.
22
+ #
23
+ # The last argument (after the query or solr params) is used for finding options.
24
+ # The following opts are allowed:
25
+ # :include_response - false is default - whether or not to return the whole solr response or just the doc(s)
26
+ # :handler - nil is default - the request path (/select or /search etc.)
27
+ module RSolr::Ext::Findable
28
+
29
+ # Examples:
30
+ # find 'jefferson' # q=jefferson - all docs
31
+ # find :first, 'jefferson' # q=jefferson&rows=1 - first doc only
32
+ # find 'jefferson', :phrase_filters=>{:type=>'book'} # q=jefferson&fq=type:"book" - all docs
33
+ # find {:q=>'something'}, :include_response=>true # q=something -- the entire response
34
+ def find(*args, &blk)
35
+ mode, solr_params, opts = extract_find_opts!(*args)
36
+
37
+ opts[:include_response] = false unless opts.key?(:include_response)
38
+
39
+ if solr_params.is_a?(String)
40
+ solr_params = {:q=>solr_params}
41
+ end
42
+
43
+ solr_params[:rows] = 1 if mode == :first
44
+ valid_solr_params = RSolr::Ext.map_params(solr_params)
45
+
46
+ response = opts[:handler] ? send_request(opts[:handler], valid_solr_params) : select(valid_solr_params)
47
+ return response if response.is_a?(String)
48
+
49
+ response = RSolr::Ext::wrap_response(response)
50
+
51
+ if block_given? and response['response']['docs']
52
+ # yield each doc if a block is given
53
+ response['response']['docs'].each_with_index do |doc,i|
54
+ response['response']['docs'][i] = yield(doc)
55
+ end
56
+ end
57
+ if mode == :first
58
+ # return only one doc
59
+ response['response']['docs'].first
60
+ elsif opts[:include_response] == false
61
+ # return all docs
62
+ response['response']['docs']
63
+ else
64
+ # return the entire response
65
+ response
66
+ end
67
+ end
68
+
69
+ # find_by_id(10, :handler=>'catalog')
70
+ def find_by_id(id, solr_params={}, opts={}, &blk)
71
+ solr_params[:phrases] ||= {}
72
+ solr_params[:phrases][:id] = id
73
+ self.find(:first, solr_params, opts, &blk)
74
+ end
75
+
76
+ protected
77
+
78
+ def extract_find_opts!(*args)
79
+ mode = :all
80
+ # extract the mode (:all or :first)
81
+ valid_modes = [:all, :first]
82
+ if args[0].is_a?(Symbol)
83
+ mode = valid_modes.include?(args[0]) ? args.shift : raise("Invalid find mode; should be :first or :all")
84
+ end
85
+ # extract solr params
86
+ solr_params = args.shift || {}
87
+ # extract options
88
+ opts = args.shift || {}
89
+ [mode, solr_params, opts]
90
+ end
91
+
92
+ end
@@ -10,6 +10,7 @@ module RSolr::Ext::Request
10
10
  MAPPED_PARAMS = [
11
11
  :per_page,
12
12
  :page,
13
+ :queries, # fielded queries
13
14
  :phrases, # quoted q param
14
15
  :filters, # fq params
15
16
  :phrase_filters, # quoted fq params,
@@ -26,7 +27,11 @@ module RSolr::Ext::Request
26
27
  page = page < 1 ? 0 : page
27
28
  output[:start] = page * output[:rows]
28
29
  end
29
-
30
+
31
+ def map_queries(value,output)
32
+ output[:q] = append_to_param(output[:q], build_query(value, false))
33
+ end
34
+
30
35
  def map_phrases(value,output)
31
36
  output[:q] = append_to_param(output[:q], build_query(value, true))
32
37
  end
@@ -24,8 +24,8 @@ module RSolr::Ext::Response
24
24
 
25
25
  def initialize(*args)
26
26
  super(*args)
27
- extend Docs
28
- extend Facets
27
+ extend Docs if self['response']['docs']
28
+ extend Facets if key?('facet_counts')
29
29
  end
30
30
 
31
31
  def response
data/lib/rsolr-ext.rb CHANGED
@@ -16,16 +16,37 @@ unless Hash.respond_to?(:to_mash)
16
16
  end
17
17
  end
18
18
 
19
+ require 'rubygems'
20
+ require 'rsolr'
21
+
19
22
  module RSolr
20
23
 
21
24
  module Ext
22
25
 
23
- VERSION = '0.6.1'
26
+ VERSION = '0.7.0'
24
27
 
25
28
  autoload :Request, 'rsolr-ext/request.rb'
26
29
  autoload :Response, 'rsolr-ext/response.rb'
27
30
  autoload :Mapable, 'rsolr-ext/mapable.rb'
28
- autoload :Access, 'rsolr-ext/access.rb'
31
+ autoload :Findable, 'rsolr-ext/findable.rb'
32
+
33
+ # RSolr::Ext.map_params({})
34
+ def self.map_params(r)
35
+ RSolr::Ext::Request::Standard.new.map(r)
36
+ end
37
+
38
+ # RSolr::Ext.wrap_response({})
39
+ def self.wrap_response(r)
40
+ RSolr::Ext::Response::Standard.new(r)
41
+ end
42
+
43
+ # c = RSolr::Ext.connect
44
+ # c.find(:q=>'*:*').docs.size
45
+ def self.connect(*args)
46
+ connection = RSolr.connect(*args)
47
+ connection.extend RSolr::Ext::Findable
48
+ connection
49
+ end
29
50
 
30
51
  end
31
52
 
data/rsolr-ext.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rsolr-ext"
3
- s.version = "0.6.1"
3
+ s.version = "0.7.0"
4
4
  s.date = "2009-05-04"
5
5
  s.summary = "An extension lib for RSolr"
6
6
  s.email = "goodieboy@gmail.com"
@@ -12,6 +12,8 @@ Gem::Specification.new do |s|
12
12
 
13
13
  "lib/mash.rb",
14
14
 
15
+ "lib/rsolr-ext/findable.rb",
16
+
15
17
  "lib/rsolr-ext/mapable.rb",
16
18
 
17
19
  "lib/rsolr-ext/request/queryable.rb",
@@ -28,6 +30,7 @@ Gem::Specification.new do |s|
28
30
  "rsolr-ext.gemspec"
29
31
  ]
30
32
  s.test_files = [
33
+ 'test/findable_test.rb',
31
34
  'test/request_test.rb',
32
35
  'test/response_test.rb',
33
36
  'test/test_unit_test_case.rb',
@@ -0,0 +1,12 @@
1
+ require 'test_unit_test_case'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'rsolr-ext')
3
+ require 'helper'
4
+
5
+ class RSolrExtFindableTest < Test::Unit::TestCase
6
+
7
+ test 'RSolr::connect' do
8
+ connection = RSolr::Ext.connect
9
+ assert connection.respond_to?(:find)
10
+ end
11
+
12
+ end
data/test/request_test.rb CHANGED
@@ -11,7 +11,7 @@ class RSolrExtRequestTest < Test::Unit::TestCase
11
11
  :phrases=>{:name=>'This is a phrase'},
12
12
  :filters=>['test', {:price=>(1..10)}],
13
13
  :phrase_filters=>{:manu=>['Apple']},
14
- :q=>'ipod',
14
+ :queries=>'ipod',
15
15
  :facets=>{:fields=>['cat', 'blah']}
16
16
  )
17
17
  assert_equal ["test", "price:[1 TO 10]", "manu:\"Apple\""], solr_params[:fq]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwmitchell-rsolr-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Mitchell
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  - README.rdoc
25
25
  files:
26
26
  - lib/mash.rb
27
+ - lib/rsolr-ext/findable.rb
27
28
  - lib/rsolr-ext/mapable.rb
28
29
  - lib/rsolr-ext/request/queryable.rb
29
30
  - lib/rsolr-ext/request.rb
@@ -61,6 +62,7 @@ signing_key:
61
62
  specification_version: 2
62
63
  summary: An extension lib for RSolr
63
64
  test_files:
65
+ - test/findable_test.rb
64
66
  - test/request_test.rb
65
67
  - test/response_test.rb
66
68
  - test/test_unit_test_case.rb