acts_as_solr_reloaded 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +13 -1
- data/VERSION +1 -1
- data/config/solr_environment.rb +1 -0
- data/generators/dynamic_attributes_migration/templates/migration.rb +3 -1
- data/lib/acts_as_solr.rb +17 -9
- data/lib/acts_as_solr/class_methods.rb +2 -0
- data/lib/acts_as_solr/mongo_mapper.rb +1 -1
- data/lib/acts_as_solr/parser_methods.rb +10 -3
- data/lib/acts_as_solr/search_results.rb +16 -0
- data/lib/solr/xml.rb +2 -2
- data/test/db/test.db +0 -0
- data/test/functional/acts_as_solr_test.rb +28 -0
- data/test/test_helper.rb +10 -0
- data/test/unit/parser_methods_shoulda.rb +1 -0
- metadata +12 -7
data/README.rdoc
CHANGED
@@ -5,9 +5,11 @@ This plugin adds full text search capabilities and many other nifty features fro
|
|
5
5
|
* faceting
|
6
6
|
* dynamic attributes
|
7
7
|
* integration with acts_as_taggable_on
|
8
|
+
* integration with will_paginate
|
8
9
|
* highlighting
|
9
10
|
* geolocation
|
10
11
|
* relevance
|
12
|
+
* suggest
|
11
13
|
|
12
14
|
Watch this screencast for a short demo of the latests features:
|
13
15
|
|
@@ -55,6 +57,16 @@ If you want to integrate the model with acts_as_taggable_on, just add the option
|
|
55
57
|
|
56
58
|
Please see ActsAsSolr::ActsMethods for a complete info
|
57
59
|
|
60
|
+
== PAGINATION
|
61
|
+
|
62
|
+
In your controller:
|
63
|
+
|
64
|
+
@search = Product.search "beer", :page => 2, :per_page => 20
|
65
|
+
|
66
|
+
And in your view:
|
67
|
+
|
68
|
+
will_paginate @search
|
69
|
+
|
58
70
|
== TESTING
|
59
71
|
|
60
72
|
To test code that uses acts_as_solr_reloaded you must start a Solr server for the test environment and also start MongoDB.
|
@@ -82,4 +94,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
82
94
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
83
95
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
84
96
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
85
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
97
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.6.0
|
data/config/solr_environment.rb
CHANGED
@@ -2,6 +2,7 @@ ENV['RAILS_ENV'] = (ENV['RAILS_ENV'] || 'development').dup
|
|
2
2
|
# RAILS_ROOT isn't defined yet, so figure it out.
|
3
3
|
require "uri"
|
4
4
|
require "fileutils"
|
5
|
+
require "yaml"
|
5
6
|
dir = File.dirname(__FILE__)
|
6
7
|
SOLR_PATH = File.expand_path("#{dir}/../solr") unless defined? SOLR_PATH
|
7
8
|
|
@@ -7,9 +7,11 @@ class DynamicAttributesMigration < ActiveRecord::Migration
|
|
7
7
|
t.text :value
|
8
8
|
t.timestamps
|
9
9
|
end
|
10
|
+
add_index :dynamic_attributes, [:dynamicable_id, :dynamicable_type, :name], :unique => true, :name => 'da_pk'
|
10
11
|
end
|
11
12
|
|
12
13
|
def self.down
|
14
|
+
remove_index 'da_pk'
|
13
15
|
drop_table :dynamic_attributes
|
14
16
|
end
|
15
|
-
end
|
17
|
+
end
|
data/lib/acts_as_solr.rb
CHANGED
@@ -21,16 +21,24 @@ require File.dirname(__FILE__) + '/acts_as_solr/lazy_document'
|
|
21
21
|
require File.dirname(__FILE__) + '/acts_as_solr/mongo_mapper'
|
22
22
|
|
23
23
|
module ActsAsSolr
|
24
|
+
class Post
|
25
|
+
class << self
|
26
|
+
def config
|
27
|
+
@config ||= YAML::load_file("#{Rails.root}/config/solr.yml")[Rails.env]
|
28
|
+
end
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
def credentials
|
31
|
+
@credentials ||= {:username => config['username'], :password => config['password']}
|
32
|
+
end
|
33
|
+
|
34
|
+
def url(core)
|
35
|
+
core.nil? ? config['url'] : "#{config['url']}/#{core}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def execute(request, core = nil)
|
39
|
+
connection = Solr::Connection.new(url(core), credentials)
|
40
|
+
connection.send request
|
41
|
+
end
|
34
42
|
end
|
35
43
|
end
|
36
44
|
end
|
@@ -17,7 +17,9 @@ module ActsAsSolr #:nodoc:
|
|
17
17
|
#
|
18
18
|
# ====options:
|
19
19
|
# offset:: - The first document to be retrieved (offset)
|
20
|
+
# page:: - The page to be retrieved
|
20
21
|
# limit:: - The number of rows per page
|
22
|
+
# per_page:: - Alias for limit
|
21
23
|
# order:: - Orders (sort by) the result set using a given criteria:
|
22
24
|
#
|
23
25
|
# Book.find_by_solr 'ruby', :order => 'description asc'
|
@@ -6,7 +6,7 @@ module ActsAsSolr #:nodoc:
|
|
6
6
|
def parse_query(query=nil, options={}, models=nil)
|
7
7
|
valid_options = [ :offset, :limit, :facets, :models, :results_format, :order,
|
8
8
|
:scores, :operator, :include, :lazy, :joins, :select, :core,
|
9
|
-
:around, :relevance, :highlight]
|
9
|
+
:around, :relevance, :highlight, :page, :per_page]
|
10
10
|
query_options = {}
|
11
11
|
|
12
12
|
return nil if (query.nil? || query.strip == '')
|
@@ -14,8 +14,10 @@ module ActsAsSolr #:nodoc:
|
|
14
14
|
raise "Invalid parameters: #{(options.keys - valid_options).join(',')}" unless (options.keys - valid_options).empty?
|
15
15
|
begin
|
16
16
|
Deprecation.validate_query(options)
|
17
|
-
|
18
|
-
|
17
|
+
per_page = options[:per_page] || options[:limit] || 30
|
18
|
+
offset = options[:offset] || (((options[:page] || 1).to_i - 1) * per_page)
|
19
|
+
query_options[:rows] = per_page
|
20
|
+
query_options[:start] = offset
|
19
21
|
query_options[:operator] = options[:operator]
|
20
22
|
|
21
23
|
query = add_relevance query, options[:relevance]
|
@@ -121,6 +123,11 @@ module ActsAsSolr #:nodoc:
|
|
121
123
|
}
|
122
124
|
results.update(:spellcheck => solr_data.data['spellcheck']) unless solr_data.nil?
|
123
125
|
results.update(:facets => {'facet_fields' => []}) if options[:facets]
|
126
|
+
unless solr_data.header['params'].nil?
|
127
|
+
header = solr_data.header
|
128
|
+
results.update :rows => header['params']['rows']
|
129
|
+
results.update :start => header['params']['start']
|
130
|
+
end
|
124
131
|
return SearchResults.new(results) if (solr_data.nil? || solr_data.total_hits == 0)
|
125
132
|
|
126
133
|
configuration.update(options) if options.is_a?(Hash)
|
@@ -63,10 +63,26 @@ module ActsAsSolr #:nodoc:
|
|
63
63
|
@solr_data[:highlights]
|
64
64
|
end
|
65
65
|
|
66
|
+
# Returns a suggested query
|
66
67
|
def suggest
|
67
68
|
@solr_data[:spellcheck]['suggestions']['collation'].match(/\((.+)\) /)[1]
|
68
69
|
end
|
69
70
|
|
71
|
+
# Returns the number of documents per page
|
72
|
+
def per_page
|
73
|
+
@solr_data[:rows].to_i
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns the number of pages found
|
77
|
+
def total_pages
|
78
|
+
(total / per_page.to_f).ceil
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns the current page
|
82
|
+
def current_page
|
83
|
+
(@solr_data[:start].to_i / per_page) + 1
|
84
|
+
end
|
85
|
+
|
70
86
|
alias docs results
|
71
87
|
alias records results
|
72
88
|
alias num_found total
|
data/lib/solr/xml.rb
CHANGED
@@ -18,7 +18,7 @@ begin
|
|
18
18
|
# If we can load rubygems and libxml-ruby...
|
19
19
|
require 'rubygems'
|
20
20
|
require 'xml/libxml'
|
21
|
-
raise "acts_as_solr requires libxml-ruby 0.7 or greater" unless XML::Node.public_instance_methods.include?(
|
21
|
+
raise "acts_as_solr requires libxml-ruby 0.7 or greater" unless XML::Node.public_instance_methods.collect{|x| x.to_sym}.include?(:attributes)
|
22
22
|
|
23
23
|
# then make a few modifications to XML::Node so it can stand in for REXML::Element
|
24
24
|
class XML::Node
|
@@ -41,4 +41,4 @@ rescue LoadError => e # If we can't load either rubygems or libxml-ruby
|
|
41
41
|
require 'rexml/document'
|
42
42
|
Solr::XML::Element = REXML::Element
|
43
43
|
|
44
|
-
end
|
44
|
+
end
|
data/test/db/test.db
CHANGED
Binary file
|
@@ -470,4 +470,32 @@ class ActsAsSolrTest < Test::Unit::TestCase
|
|
470
470
|
Document.new(:name => "mapper").save
|
471
471
|
assert_equal "mapper", Document.search("mapper").docs.first.name
|
472
472
|
end
|
473
|
+
|
474
|
+
def test_total_pages_is_returned_when_limit_specified
|
475
|
+
assert_equal 2, Posting.search("test", :limit => 1).total_pages
|
476
|
+
end
|
477
|
+
|
478
|
+
def test_total_pages_is_returned_when_limit_not_specified
|
479
|
+
assert_equal 1, Posting.search("test").total_pages
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_current_page_is_returned
|
483
|
+
assert_equal 2, Posting.search("test", :limit => 1, :offset => 1).current_page
|
484
|
+
end
|
485
|
+
|
486
|
+
def test_current_page_1_is_returned
|
487
|
+
assert_equal 1, Posting.search("test").current_page
|
488
|
+
end
|
489
|
+
|
490
|
+
def test_current_page_1_is_returned_when_no_records_found
|
491
|
+
assert_equal 1, Posting.search("nothing").current_page
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_page_parameter_is_accepted
|
495
|
+
assert_equal 2, Posting.search("test", :limit => 1, :page => 2).current_page
|
496
|
+
end
|
497
|
+
|
498
|
+
def test_per_page_parameter_is_accepted
|
499
|
+
assert_equal 1, Posting.search("test", :per_page => 1).per_page
|
500
|
+
end
|
473
501
|
end
|
data/test/test_helper.rb
CHANGED
@@ -58,3 +58,13 @@ class Test::Unit::TestCase
|
|
58
58
|
ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "type_s:#{table_name.to_s.capitalize.singularize}"))
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
class Rails
|
63
|
+
def self.root
|
64
|
+
RAILS_ROOT
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.env
|
68
|
+
RAILS_ENV
|
69
|
+
end
|
70
|
+
end
|
@@ -15,6 +15,7 @@ class ParserMethodsTest < Test::Unit::TestCase
|
|
15
15
|
@results.stubs(:max_score).returns 2.1
|
16
16
|
@results.stubs(:highlighting).returns []
|
17
17
|
@results.stubs(:data).returns({"responseHeader" => {"QTime" => "10.2"}})
|
18
|
+
@results.stubs(:header).returns({})
|
18
19
|
end
|
19
20
|
|
20
21
|
should "return a SearchResults object" do
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_solr_reloaded
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 1
|
7
|
-
-
|
8
|
+
- 6
|
8
9
|
- 0
|
9
|
-
version: 1.
|
10
|
+
version: 1.6.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Diego Carrion
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-03-20 00:00:00 -03:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -161,28 +162,32 @@ homepage: http://github.com/dcrec1/acts_as_solr_reloaded
|
|
161
162
|
licenses: []
|
162
163
|
|
163
164
|
post_install_message:
|
164
|
-
rdoc_options:
|
165
|
-
|
165
|
+
rdoc_options: []
|
166
|
+
|
166
167
|
require_paths:
|
167
168
|
- lib
|
168
169
|
required_ruby_version: !ruby/object:Gem::Requirement
|
170
|
+
none: false
|
169
171
|
requirements:
|
170
172
|
- - ">="
|
171
173
|
- !ruby/object:Gem::Version
|
174
|
+
hash: 3
|
172
175
|
segments:
|
173
176
|
- 0
|
174
177
|
version: "0"
|
175
178
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
|
+
none: false
|
176
180
|
requirements:
|
177
181
|
- - ">="
|
178
182
|
- !ruby/object:Gem::Version
|
183
|
+
hash: 3
|
179
184
|
segments:
|
180
185
|
- 0
|
181
186
|
version: "0"
|
182
187
|
requirements: []
|
183
188
|
|
184
189
|
rubyforge_project:
|
185
|
-
rubygems_version: 1.
|
190
|
+
rubygems_version: 1.5.0
|
186
191
|
signing_key:
|
187
192
|
specification_version: 3
|
188
193
|
summary: This gem adds full text search capabilities and many other nifty features from Apache Solr to any Rails model.
|