mwmitchell-rsolr-ext 0.8.0 → 0.9.5
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 +75 -10
- data/lib/rsolr-ext.rb +1 -1
- data/lib/rsolr-ext/connection.rb +31 -84
- data/lib/rsolr-ext/request.rb +2 -2
- data/lib/rsolr-ext/response.rb +9 -2
- data/rsolr-ext.gemspec +21 -19
- data/test/connection_test.rb +28 -1
- data/test/request_test.rb +0 -8
- metadata +13 -4
data/README.rdoc
CHANGED
@@ -1,7 +1,66 @@
|
|
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
|
-
==
|
4
|
+
==Requests
|
5
|
+
To use the RSolr::Ext connection instead of the normal RSolr connection:
|
6
|
+
solr = RSolr::Ext.connect
|
7
|
+
|
8
|
+
RSolr::Ext adds a #find and a #luke_admin method to the connection object.
|
9
|
+
|
10
|
+
===#luke
|
11
|
+
The #luke method returns a Hash/Mash result of a /admin/luke?numTerms=0 request:
|
12
|
+
solr.luke['index']
|
13
|
+
solr.luke['fields']
|
14
|
+
solr.luke['info']
|
15
|
+
|
16
|
+
===#find
|
17
|
+
The #find method listens for certain keys. All other keys are ignored, allowing the ability to mix-and-match special keys with normal Solr param keys. The recognized keys are describe below.
|
18
|
+
|
19
|
+
====:page
|
20
|
+
This maps to the Solr "start" param. The current "page" in the results set. RSolr::Ext handles the page-to-rows math for you.
|
21
|
+
|
22
|
+
====:per_page
|
23
|
+
This maps to the Solr "rows" param. How many "pages" in the result.
|
24
|
+
|
25
|
+
====:queries
|
26
|
+
This key maps to the Solr "q" param. Accepts a string, array or hash. When an array is used, each value is joined by a space. When a hash is used, the keys are used as Solr fields.
|
27
|
+
=====Examples
|
28
|
+
* :queries => 'normal' BECOMES ?q=normal
|
29
|
+
* :queries => ['one', 'two'] BECOMES ?q=one two
|
30
|
+
* :queries => {:title=>'one'} BECOMES ?q=title:(one)
|
31
|
+
* :queries => ['red', {:title=>'one'}] BECOMES ?q=red title:(one)
|
32
|
+
|
33
|
+
====:phrases
|
34
|
+
This value is mapped to the Solr "q" param. When this key is used, the value will become double-quoted, creating a Solr "phrase" based query.
|
35
|
+
=====Examples
|
36
|
+
* :phrases => 'normal' BECOMES ?q="normal"
|
37
|
+
* :phrases => ['one', 'two'] BECOMES ?q="one" "two"
|
38
|
+
* :phrases => {:title=>'one'} BECOMES ?q=title:("one")
|
39
|
+
* :phrases => ['red', {:title=>'one'}] BECOMES ?q="red" title:("one")
|
40
|
+
|
41
|
+
====:filters
|
42
|
+
The :filters key maps to the Solr :fq param. This has the same behavior as the :queries key, except it's for the :fq param.
|
43
|
+
=====Examples
|
44
|
+
* :filters => 'normal' BECOMES ?fq=normal
|
45
|
+
* :filters => ['one', 'two'] BECOMES ?fq=one two
|
46
|
+
* :filters => {:title=>'one'} BECOMES ?fq=title:(one)
|
47
|
+
* :filters => ['red', {:title=>'one'}] BECOMES ?fq=red title:(one)
|
48
|
+
|
49
|
+
====:phrase_filters
|
50
|
+
The :phrase_filters key maps to the Solr :fq param. This has the same behavior as the :phrases key, except it's for the :fq param.
|
51
|
+
=====Examples
|
52
|
+
* :phrase_filters => 'normal' BECOMES ?fq="normal"
|
53
|
+
* :phrase_filters => ['one', 'two'] BECOMES ?fq="one" "two"
|
54
|
+
* :phrase_filters => {:title=>'one'} BECOMES ?fq=title:("one")
|
55
|
+
* :phrase_filters => ['red', {:title=>'one'}] BECOMES ?fq="red" title:("one")
|
56
|
+
|
57
|
+
====:facets
|
58
|
+
The :facets does a few different things. First, it sets the Solr param facet=true. It accepts a hash with a single key called :fields. This should be an array of field names to facet on.
|
59
|
+
=====Examples
|
60
|
+
* :facets=>{:fields=>['cat', 'blah']} BECOMES ?facet=true&facet.field=cat&facet.field=blah
|
61
|
+
|
62
|
+
====Request Example
|
63
|
+
solr = RSolr::Ext.connect
|
5
64
|
solr_params = {
|
6
65
|
:page=>2,
|
7
66
|
:per_page=>10,
|
@@ -12,31 +71,37 @@ A set of helper methods/modules to assist in building Solr queries and handling
|
|
12
71
|
:facets=>{:fields=>['cat', 'blah']}
|
13
72
|
}
|
14
73
|
|
15
|
-
rsolr = RSolr::Ext.connect
|
16
|
-
|
17
74
|
response = rsolr.find(solr_params)
|
18
75
|
|
19
|
-
==
|
20
|
-
|
76
|
+
==Responses
|
77
|
+
RSolr::Ext decorates the normal output hash from RSolr and adds some helpful methods.
|
78
|
+
|
79
|
+
===Examples
|
80
|
+
solr = RSolr::Ext.connect
|
21
81
|
|
22
|
-
response =
|
82
|
+
response = solr.find :q=>'*:*'
|
23
83
|
|
24
84
|
response.ok?
|
25
85
|
response.params
|
26
86
|
response.docs
|
27
87
|
response.docs.previous_page
|
28
88
|
response.docs.next_page
|
29
|
-
response.facets
|
89
|
+
response.facets.each do |facet|
|
90
|
+
puts facet.name
|
91
|
+
facet.items.each do |item|
|
92
|
+
puts "#{facet.name}::#{item.value} (#{item.hits})"
|
93
|
+
end
|
94
|
+
end
|
30
95
|
|
31
96
|
You can access values in the response hash using symbols or strings.
|
32
97
|
|
33
|
-
===
|
98
|
+
===Documents/Pagination
|
34
99
|
If you wanna paginate, just throw the collection into the WillPaginate view helper.
|
35
100
|
<%= will_paginate response.docs %>
|
36
101
|
|
37
|
-
|
102
|
+
==The "Model" Module
|
38
103
|
You can create your own <read-only> "models" using RSolr::Ext::Model
|
39
|
-
|
104
|
+
|
40
105
|
class Book
|
41
106
|
include RSolr::Ext::Model
|
42
107
|
def self.find_by_author(author)
|
data/lib/rsolr-ext.rb
CHANGED
data/lib/rsolr-ext/connection.rb
CHANGED
@@ -1,91 +1,38 @@
|
|
1
|
-
# RSolr::Ext.connect() does this for you.
|
2
|
-
# Ext::Connection makes querying solr easier by providing a simple #find method.
|
3
|
-
#
|
4
|
-
# The #find method behaves differently depending on what you send in.
|
5
|
-
#
|
6
|
-
# The first argument can be either a symbol (:all, :first)
|
7
|
-
# OR
|
8
|
-
# a solr params hash
|
9
|
-
# OR
|
10
|
-
# a string that will be used for the query.
|
11
|
-
#
|
12
|
-
# If the first argument is a symbol, the second is used for solr params or the query.
|
13
|
-
#
|
14
|
-
# If the first argument is :first, then only a single document is returns (:rows=>0)
|
15
|
-
# If the first argument is :all, then all documents are returned
|
16
|
-
#
|
17
|
-
# If a hash is used for solr params, all of the normal RSolr::Ext::Request::Standard
|
18
|
-
# mappings are available (everything else gets passed to solr).
|
19
|
-
#
|
20
|
-
# If a string is used, it's set to the :q param.
|
21
|
-
#
|
22
|
-
# The last argument (after the query or solr params) is used for finding options.
|
23
|
-
# The following opts are allowed:
|
24
|
-
# :include_response - false is default - whether or not to return the whole solr response or just the doc(s)
|
25
|
-
# :handler - nil is default - the request path (/select or /search etc.)
|
26
1
|
module RSolr::Ext::Connection
|
27
2
|
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
response = RSolr::Ext::Response::Base.new(response)
|
45
|
-
|
46
|
-
if opts[:include_response] == true
|
47
|
-
response
|
48
|
-
else
|
49
|
-
if mode == :first
|
50
|
-
# return only one doc
|
51
|
-
response['response']['docs'].first
|
52
|
-
else
|
53
|
-
# return all docs
|
54
|
-
response['response']['docs']
|
55
|
-
end
|
56
|
-
end
|
3
|
+
# TWO modes of arguments:
|
4
|
+
#
|
5
|
+
# <request-handler-path>, <solr-params-hash>
|
6
|
+
# OR
|
7
|
+
# <solr-params-hash>
|
8
|
+
#
|
9
|
+
# The default request-handler-path is /select
|
10
|
+
#
|
11
|
+
# If a hash is used for solr params, all of the normal RSolr::Ext::Request
|
12
|
+
# mappings are available (everything else gets passed to solr).
|
13
|
+
# Returns a new RSolr::Ext::Response::Base object.
|
14
|
+
def find *args
|
15
|
+
path = args.first.is_a?(String) ? args.shift : '/select'
|
16
|
+
params = args.pop || {}
|
17
|
+
response = self.request path, RSolr::Ext::Request.map(params)
|
18
|
+
RSolr::Ext::Response::Base.new(response)
|
57
19
|
end
|
58
20
|
|
59
|
-
#
|
60
|
-
#
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
mode = :all
|
75
|
-
valid_modes = [:all, :first]
|
76
|
-
if args[0].is_a?(Symbol)
|
77
|
-
mode = valid_modes.include?(args[0]) ? args.shift : raise("Invalid find mode; should be :first or :all")
|
78
|
-
end
|
79
|
-
# extract solr params
|
80
|
-
solr_params = args.shift
|
81
|
-
# if solr_params is NOT a hash like object
|
82
|
-
# force it into a string for the :q param
|
83
|
-
unless solr_params.respond_to?(:each_pair)
|
84
|
-
solr_params = {:q=>solr_params.to_s}
|
85
|
-
end
|
86
|
-
# extract options
|
87
|
-
opts = args.shift || {}
|
88
|
-
[mode, solr_params, opts]
|
21
|
+
# TWO modes of arguments:
|
22
|
+
#
|
23
|
+
# <request-handler-path>, <solr-params-hash>
|
24
|
+
# OR
|
25
|
+
# <solr-params-hash>
|
26
|
+
#
|
27
|
+
# The default request-handler-path is /admin/luke
|
28
|
+
# The default params are numTerms=0
|
29
|
+
#
|
30
|
+
# Returns a new Mash object.
|
31
|
+
def luke *args
|
32
|
+
path = args.first.is_a?(String) ? args.shift : '/admin/luke'
|
33
|
+
params = args.pop || {}
|
34
|
+
params['numTerms'] ||= 0
|
35
|
+
self.request(path, params).to_mash
|
89
36
|
end
|
90
37
|
|
91
38
|
end
|
data/lib/rsolr-ext/request.rb
CHANGED
@@ -24,8 +24,8 @@ module RSolr::Ext::Request
|
|
24
24
|
if filters = input.delete(:filters)
|
25
25
|
output[:fq] = append_to_param output[:fq], build_query(filters), false
|
26
26
|
end
|
27
|
-
if
|
28
|
-
output[:fq] = append_to_param output[:fq], build_query(
|
27
|
+
if phrase_filters = input.delete(:phrase_filters)
|
28
|
+
output[:fq] = append_to_param output[:fq], build_query(phrase_filters, true), false
|
29
29
|
end
|
30
30
|
if facets = input.delete(:facets)
|
31
31
|
output[:facet] = true
|
data/lib/rsolr-ext/response.rb
CHANGED
@@ -6,8 +6,11 @@ module RSolr::Ext::Response
|
|
6
6
|
|
7
7
|
class Base < Mash
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
attr :original_hash
|
10
|
+
|
11
|
+
def initialize hash
|
12
|
+
super hash
|
13
|
+
@original_hash = hash
|
11
14
|
extend Response if self['response']
|
12
15
|
extend Docs if self['response'] and self['response']['docs']
|
13
16
|
extend Facets if self['facet_counts']
|
@@ -26,6 +29,10 @@ module RSolr::Ext::Response
|
|
26
29
|
header['status'] == 0
|
27
30
|
end
|
28
31
|
|
32
|
+
def method_missing *args, &blk
|
33
|
+
self.original_hash.send *args, &blk
|
34
|
+
end
|
35
|
+
|
29
36
|
end
|
30
37
|
|
31
38
|
module Response
|
data/rsolr-ext.gemspec
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
s.name = "rsolr-ext"
|
3
|
+
s.version = "0.9.5"
|
4
|
+
s.date = "2009-09-04"
|
5
|
+
s.summary = "An extension lib for RSolr"
|
6
|
+
s.email = "goodieboy@gmail.com"
|
7
|
+
s.homepage = "http://github.com/mwmitchell/rsolr_ext"
|
8
|
+
s.description = "An extension lib for RSolr"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Matt Mitchell"]
|
11
|
+
s.files = [
|
12
|
+
|
13
13
|
"lib/mash.rb",
|
14
|
-
|
14
|
+
|
15
15
|
"lib/rsolr-ext/connection.rb",
|
16
|
-
|
16
|
+
|
17
17
|
"lib/rsolr-ext/doc.rb",
|
18
|
-
|
18
|
+
|
19
19
|
"lib/rsolr-ext/model.rb",
|
20
|
-
|
20
|
+
|
21
21
|
"lib/rsolr-ext/request.rb",
|
22
|
-
|
22
|
+
|
23
23
|
"lib/rsolr-ext/response/docs.rb",
|
24
24
|
"lib/rsolr-ext/response/facets.rb",
|
25
25
|
"lib/rsolr-ext/response/spelling.rb",
|
26
|
-
|
26
|
+
|
27
27
|
"lib/rsolr-ext/response.rb",
|
28
|
-
|
28
|
+
|
29
29
|
"lib/rsolr-ext.rb",
|
30
|
-
|
30
|
+
|
31
31
|
"LICENSE",
|
32
32
|
"README.rdoc",
|
33
33
|
"rsolr-ext.gemspec"
|
@@ -42,4 +42,6 @@ Gem::Specification.new do |s|
|
|
42
42
|
|
43
43
|
s.extra_rdoc_files = %w(LICENSE README.rdoc)
|
44
44
|
|
45
|
+
s.add_dependency("mwmitchell-rsolr", [">= 0.9.5"])
|
46
|
+
|
45
47
|
end
|
data/test/connection_test.rb
CHANGED
@@ -4,9 +4,36 @@ require 'helper'
|
|
4
4
|
|
5
5
|
class RSolrExtConnectionTest < Test::Unit::TestCase
|
6
6
|
|
7
|
-
test '
|
7
|
+
test 'the #connect method' do
|
8
8
|
connection = RSolr::Ext.connect
|
9
9
|
assert connection.respond_to?(:find)
|
10
10
|
end
|
11
11
|
|
12
|
+
test 'the #find method' do
|
13
|
+
connection = RSolr::Ext.connect
|
14
|
+
response = connection.find :q=>'*:*'
|
15
|
+
assert response.kind_of?(Mash)
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'the #find method with a custom request handler' do
|
19
|
+
connection = RSolr::Ext.connect
|
20
|
+
response = connection.find '/select', :q=>'*:*'
|
21
|
+
assert response.adapter_response[:path]=~/\/select/
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'the response' do
|
25
|
+
connection = RSolr::Ext.connect
|
26
|
+
response = connection.find :q=>'*:*'
|
27
|
+
assert response.respond_to?(:ok?)
|
28
|
+
assert response.ok?
|
29
|
+
end
|
30
|
+
|
31
|
+
test 'the #luke method' do
|
32
|
+
info = RSolr::Ext.connect.luke
|
33
|
+
assert info.kind_of?(Mash)
|
34
|
+
assert info.key?('fields')
|
35
|
+
assert info.key?('index')
|
36
|
+
assert info.key?('info')
|
37
|
+
end
|
38
|
+
|
12
39
|
end
|
data/test/request_test.rb
CHANGED
@@ -1,14 +1,6 @@
|
|
1
1
|
require 'test_unit_test_case'
|
2
2
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'rsolr-ext')
|
3
3
|
|
4
|
-
class Symbol
|
5
|
-
|
6
|
-
def method_missing n,&b
|
7
|
-
[self, n].join('.').to_sym
|
8
|
-
end
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
4
|
class RSolrExtRequestTest < Test::Unit::TestCase
|
13
5
|
|
14
6
|
test 'standard request' do
|
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.
|
4
|
+
version: 0.9.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Mitchell
|
@@ -9,10 +9,19 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-04 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mwmitchell-rsolr
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.5
|
24
|
+
version:
|
16
25
|
description: An extension lib for RSolr
|
17
26
|
email: goodieboy@gmail.com
|
18
27
|
executables: []
|