honkster-acts_as_solr 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGE_LOG +233 -0
- data/LICENSE +19 -0
- data/README.markdown +118 -0
- data/README.rdoc +107 -0
- data/Rakefile +68 -0
- data/TESTING_THE_PLUGIN +25 -0
- data/VERSION +1 -0
- data/lib/acts_as_solr/acts_methods.rb +279 -0
- data/lib/acts_as_solr/class_methods.rb +236 -0
- data/lib/acts_as_solr/common_methods.rb +89 -0
- data/lib/acts_as_solr/deprecation.rb +61 -0
- data/lib/acts_as_solr/instance_methods.rb +166 -0
- data/lib/acts_as_solr/lazy_document.rb +18 -0
- data/lib/acts_as_solr/parser_methods.rb +201 -0
- data/lib/acts_as_solr/search_results.rb +68 -0
- data/lib/acts_as_solr/solr_fixtures.rb +13 -0
- data/lib/acts_as_solr/tasks/database.rake +16 -0
- data/lib/acts_as_solr/tasks/solr.rake +132 -0
- data/lib/acts_as_solr/tasks/test.rake +5 -0
- data/lib/acts_as_solr/tasks.rb +10 -0
- data/lib/acts_as_solr.rb +64 -0
- data/lib/solr/connection.rb +177 -0
- data/lib/solr/document.rb +75 -0
- data/lib/solr/exception.rb +13 -0
- data/lib/solr/field.rb +36 -0
- data/lib/solr/importer/array_mapper.rb +26 -0
- data/lib/solr/importer/delimited_file_source.rb +38 -0
- data/lib/solr/importer/hpricot_mapper.rb +27 -0
- data/lib/solr/importer/mapper.rb +51 -0
- data/lib/solr/importer/solr_source.rb +41 -0
- data/lib/solr/importer/xpath_mapper.rb +35 -0
- data/lib/solr/importer.rb +19 -0
- data/lib/solr/indexer.rb +52 -0
- data/lib/solr/request/add_document.rb +58 -0
- data/lib/solr/request/base.rb +36 -0
- data/lib/solr/request/commit.rb +29 -0
- data/lib/solr/request/delete.rb +48 -0
- data/lib/solr/request/dismax.rb +46 -0
- data/lib/solr/request/index_info.rb +22 -0
- data/lib/solr/request/modify_document.rb +46 -0
- data/lib/solr/request/optimize.rb +19 -0
- data/lib/solr/request/ping.rb +36 -0
- data/lib/solr/request/select.rb +54 -0
- data/lib/solr/request/spellcheck.rb +30 -0
- data/lib/solr/request/standard.rb +402 -0
- data/lib/solr/request/update.rb +23 -0
- data/lib/solr/request.rb +26 -0
- data/lib/solr/response/add_document.rb +17 -0
- data/lib/solr/response/base.rb +42 -0
- data/lib/solr/response/commit.rb +15 -0
- data/lib/solr/response/delete.rb +13 -0
- data/lib/solr/response/dismax.rb +8 -0
- data/lib/solr/response/index_info.rb +26 -0
- data/lib/solr/response/modify_document.rb +17 -0
- data/lib/solr/response/optimize.rb +14 -0
- data/lib/solr/response/ping.rb +26 -0
- data/lib/solr/response/ruby.rb +42 -0
- data/lib/solr/response/select.rb +17 -0
- data/lib/solr/response/spellcheck.rb +20 -0
- data/lib/solr/response/standard.rb +60 -0
- data/lib/solr/response/xml.rb +39 -0
- data/lib/solr/response.rb +27 -0
- data/lib/solr/solrtasks.rb +27 -0
- data/lib/solr/util.rb +32 -0
- data/lib/solr/xml.rb +44 -0
- data/lib/solr.rb +26 -0
- data/solr/CHANGES.txt +1207 -0
- data/solr/LICENSE.txt +712 -0
- data/solr/NOTICE.txt +90 -0
- data/solr/etc/jetty.xml +205 -0
- data/solr/etc/webdefault.xml +379 -0
- data/solr/lib/easymock.jar +0 -0
- data/solr/lib/jetty-6.1.3.jar +0 -0
- data/solr/lib/jetty-util-6.1.3.jar +0 -0
- data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/solr/lib/servlet-api-2.4.jar +0 -0
- data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
- data/solr/lib/xpp3-1.1.3.4.O.jar +0 -0
- data/solr/solr/README.txt +52 -0
- data/solr/solr/bin/abc +176 -0
- data/solr/solr/bin/abo +176 -0
- data/solr/solr/bin/backup +108 -0
- data/solr/solr/bin/backupcleaner +142 -0
- data/solr/solr/bin/commit +128 -0
- data/solr/solr/bin/optimize +129 -0
- data/solr/solr/bin/readercycle +129 -0
- data/solr/solr/bin/rsyncd-disable +77 -0
- data/solr/solr/bin/rsyncd-enable +76 -0
- data/solr/solr/bin/rsyncd-start +145 -0
- data/solr/solr/bin/rsyncd-stop +105 -0
- data/solr/solr/bin/scripts-util +83 -0
- data/solr/solr/bin/snapcleaner +148 -0
- data/solr/solr/bin/snapinstaller +168 -0
- data/solr/solr/bin/snappuller +248 -0
- data/solr/solr/bin/snappuller-disable +77 -0
- data/solr/solr/bin/snappuller-enable +77 -0
- data/solr/solr/bin/snapshooter +109 -0
- data/solr/solr/conf/admin-extra.html +31 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +126 -0
- data/solr/solr/conf/scripts.conf +24 -0
- data/solr/solr/conf/solrconfig.xml +458 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/solr/conf/xslt/example.xsl +132 -0
- data/solr/solr/conf/xslt/example_atom.xsl +63 -0
- data/solr/solr/conf/xslt/example_rss.xsl +62 -0
- data/solr/start.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/test/config/solr.yml +2 -0
- data/test/db/connections/mysql/connection.rb +10 -0
- data/test/db/connections/sqlite/connection.rb +8 -0
- data/test/db/migrate/001_create_books.rb +15 -0
- data/test/db/migrate/002_create_movies.rb +12 -0
- data/test/db/migrate/003_create_categories.rb +11 -0
- data/test/db/migrate/004_create_electronics.rb +16 -0
- data/test/db/migrate/005_create_authors.rb +12 -0
- data/test/db/migrate/006_create_postings.rb +9 -0
- data/test/db/migrate/007_create_posts.rb +13 -0
- data/test/db/migrate/008_create_gadgets.rb +11 -0
- data/test/fixtures/authors.yml +9 -0
- data/test/fixtures/books.yml +13 -0
- data/test/fixtures/categories.yml +7 -0
- data/test/fixtures/db_definitions/mysql.sql +41 -0
- data/test/fixtures/electronics.yml +49 -0
- data/test/fixtures/movies.yml +9 -0
- data/test/fixtures/postings.yml +10 -0
- data/test/functional/acts_as_solr_test.rb +413 -0
- data/test/functional/association_indexing_test.rb +37 -0
- data/test/functional/faceted_search_test.rb +163 -0
- data/test/functional/multi_solr_search_test.rb +51 -0
- data/test/models/author.rb +10 -0
- data/test/models/book.rb +10 -0
- data/test/models/category.rb +8 -0
- data/test/models/electronic.rb +21 -0
- data/test/models/gadget.rb +9 -0
- data/test/models/movie.rb +17 -0
- data/test/models/novel.rb +2 -0
- data/test/models/post.rb +3 -0
- data/test/models/posting.rb +11 -0
- data/test/test_helper.rb +51 -0
- data/test/unit/acts_methods_shoulda.rb +68 -0
- data/test/unit/class_methods_shoulda.rb +85 -0
- data/test/unit/common_methods_shoulda.rb +111 -0
- data/test/unit/instance_methods_shoulda.rb +318 -0
- data/test/unit/lazy_document_shoulda.rb +34 -0
- data/test/unit/parser_instance.rb +19 -0
- data/test/unit/parser_methods_shoulda.rb +268 -0
- data/test/unit/solr_instance.rb +44 -0
- data/test/unit/test_helper.rb +22 -0
- metadata +239 -0
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
Dir["#{File.dirname(__FILE__)}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
|
7
|
+
|
8
|
+
desc "Default Task"
|
9
|
+
task :default => [:test]
|
10
|
+
|
11
|
+
desc "Runs the unit tests"
|
12
|
+
task :test => "test:unit"
|
13
|
+
|
14
|
+
namespace :test do
|
15
|
+
task :setup do
|
16
|
+
ENV['RAILS_ENV'] = "test"
|
17
|
+
require File.expand_path("#{File.dirname(__FILE__)}/config/solr_environment")
|
18
|
+
puts "Using " + DB
|
19
|
+
%x(mysql -u#{MYSQL_USER} < #{File.dirname(__FILE__) + "/test/fixtures/db_definitions/mysql.sql"}) if DB == 'mysql'
|
20
|
+
|
21
|
+
Rake::Task["test:migrate"].invoke
|
22
|
+
end
|
23
|
+
|
24
|
+
desc 'Measures test coverage using rcov'
|
25
|
+
task :rcov => :setup do
|
26
|
+
rm_f "coverage"
|
27
|
+
rm_f "coverage.data"
|
28
|
+
rcov = "rcov --rails --aggregate coverage.data --text-summary -Ilib"
|
29
|
+
|
30
|
+
system("#{rcov} --html #{Dir.glob('test/**/*_test.rb').join(' ')}")
|
31
|
+
system("open coverage/index.html") if PLATFORM['darwin']
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Runs the functional tests, testing integration with Solr'
|
35
|
+
Rake::TestTask.new('functional' => :setup) do |t|
|
36
|
+
t.pattern = "test/functional/*_test.rb"
|
37
|
+
t.verbose = true
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "Unit tests"
|
41
|
+
Rake::TestTask.new(:unit) do |t|
|
42
|
+
t.libs << 'test/unit'
|
43
|
+
t.pattern = "test/unit/*_shoulda.rb"
|
44
|
+
t.verbose = true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Rake::RDocTask.new do |rd|
|
49
|
+
rd.main = "README.rdoc"
|
50
|
+
rd.rdoc_dir = "rdoc"
|
51
|
+
rd.rdoc_files.exclude("lib/solr/**/*.rb", "lib/solr.rb")
|
52
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
53
|
+
end
|
54
|
+
|
55
|
+
begin
|
56
|
+
require 'jeweler'
|
57
|
+
Jeweler::Tasks.new do |s|
|
58
|
+
s.name = "acts_as_solr"
|
59
|
+
s.summary = "This plugin adds full text search capabilities and many other nifty features from Apache�s Solr to any Rails model. I'm currently rearranging the test suite to include a real unit test suite, and adding a few features I need myself."
|
60
|
+
s.email = "meyer@paperplanes.de"
|
61
|
+
s.homepage = "http://github.com/mattmatt/acts_as_solr"
|
62
|
+
s.description = "This plugin adds full text search capabilities and many other nifty features from Apache�s Solr to any Rails model. I'm currently rearranging the test suite to include a real unit test suite, and adding a few features I need myself."
|
63
|
+
s.authors = ["Mathias Meyer"]
|
64
|
+
s.files = FileList["[A-Z]*", "{bin,generators,lib,solr,test}/**/*"]
|
65
|
+
end
|
66
|
+
rescue LoadError
|
67
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
68
|
+
end
|
data/TESTING_THE_PLUGIN
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
acts_as_solr comes with a quick and fast unit test suite, and with a longer-running
|
2
|
+
functional test suite, the latter testing the actual integration with Solr.
|
3
|
+
|
4
|
+
The unit test suite is written using Shoulda, so make sure you have a recent version
|
5
|
+
installed.
|
6
|
+
|
7
|
+
Running `rake test` or just `rake` will run both test suites. Use `rake test:unit` to
|
8
|
+
just run the unit test suite.
|
9
|
+
|
10
|
+
== How to run functional tests for this plugin:
|
11
|
+
To run the acts_as_solr's plugin tests run the following steps:
|
12
|
+
|
13
|
+
- create a MySQL database called "actsassolr_test" (if you want to use MySQL)
|
14
|
+
|
15
|
+
- create a new Rails project, if needed (the plugin can only be tested from within a Rails project); move/checkout acts_as_solr into its vendor/plugins/, as usual
|
16
|
+
|
17
|
+
- copy vendor/plugins/acts_as_solr/config/solr.yml to config/ (the Rails config folder)
|
18
|
+
|
19
|
+
- rake solr:start RAILS_ENV=test
|
20
|
+
|
21
|
+
- rake test:functional (Accepts the following arguments: DB=sqlite|mysql and MYSQL_USER=user)
|
22
|
+
|
23
|
+
== Troubleshooting:
|
24
|
+
If for some reason the tests don't run and you get MySQL errors, make sure you edit the MYSQL_USER entry under
|
25
|
+
config/environment.rb. It's recommended to create or use a MySQL user with no password.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,279 @@
|
|
1
|
+
module ActsAsSolr #:nodoc:
|
2
|
+
|
3
|
+
module ActsMethods
|
4
|
+
|
5
|
+
# declares a class as solr-searchable
|
6
|
+
#
|
7
|
+
# ==== options:
|
8
|
+
# fields:: This option can be used to specify only the fields you'd
|
9
|
+
# like to index. If not given, all the attributes from the
|
10
|
+
# class will be indexed. You can also use this option to
|
11
|
+
# include methods that should be indexed as fields
|
12
|
+
#
|
13
|
+
# class Movie < ActiveRecord::Base
|
14
|
+
# acts_as_solr :fields => [:name, :description, :current_time]
|
15
|
+
# def current_time
|
16
|
+
# Time.now.to_s
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Each field passed can also be a hash with the value being a field type
|
21
|
+
#
|
22
|
+
# class Electronic < ActiveRecord::Base
|
23
|
+
# acts_as_solr :fields => [{:price => :range_float}]
|
24
|
+
# def current_time
|
25
|
+
# Time.now
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# The field types accepted are:
|
30
|
+
#
|
31
|
+
# :float:: Index the field value as a float (ie.: 12.87)
|
32
|
+
# :integer:: Index the field value as an integer (ie.: 31)
|
33
|
+
# :boolean:: Index the field value as a boolean (ie.: true/false)
|
34
|
+
# :date:: Index the field value as a date (ie.: Wed Nov 15 23:13:03 PST 2006)
|
35
|
+
# :string:: Index the field value as a text string, not applying the same indexing
|
36
|
+
# filters as a regular text field
|
37
|
+
# :range_integer:: Index the field value for integer range queries (ie.:[5 TO 20])
|
38
|
+
# :range_float:: Index the field value for float range queries (ie.:[14.56 TO 19.99])
|
39
|
+
#
|
40
|
+
# Setting the field type preserves its original type when indexed
|
41
|
+
#
|
42
|
+
# The field may also be passed with a hash value containing options
|
43
|
+
#
|
44
|
+
# class Author < ActiveRecord::Base
|
45
|
+
# acts_as_solr :fields => [{:full_name => {:type => :text, :as => :name}}]
|
46
|
+
# def full_name
|
47
|
+
# self.first_name + ' ' + self.last_name
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# The options accepted are:
|
52
|
+
#
|
53
|
+
# :type:: Index the field using the specified type
|
54
|
+
# :as:: Index the field using the specified field name
|
55
|
+
#
|
56
|
+
# additional_fields:: This option takes fields to be include in the index
|
57
|
+
# in addition to those derived from the database. You
|
58
|
+
# can also use this option to include custom fields
|
59
|
+
# derived from methods you define. This option will be
|
60
|
+
# ignored if the :fields option is given. It also accepts
|
61
|
+
# the same field types as the option above
|
62
|
+
#
|
63
|
+
# class Movie < ActiveRecord::Base
|
64
|
+
# acts_as_solr :additional_fields => [:current_time]
|
65
|
+
# def current_time
|
66
|
+
# Time.now.to_s
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# exclude_fields:: This option taks an array of fields that should be ignored from indexing:
|
71
|
+
#
|
72
|
+
# class User < ActiveRecord::Base
|
73
|
+
# acts_as_solr :exclude_fields => [:password, :login, :credit_card_number]
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# include:: This option can be used for association indexing, which
|
77
|
+
# means you can include any :has_one, :has_many, :belongs_to
|
78
|
+
# and :has_and_belongs_to_many association to be indexed:
|
79
|
+
#
|
80
|
+
# class Category < ActiveRecord::Base
|
81
|
+
# has_many :books
|
82
|
+
# acts_as_solr :include => [:books]
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# Each association may also be specified as a hash with an option hash as a value
|
86
|
+
#
|
87
|
+
# class Book < ActiveRecord::Base
|
88
|
+
# belongs_to :author
|
89
|
+
# has_many :distribution_companies
|
90
|
+
# has_many :copyright_dates
|
91
|
+
# has_many :media_types
|
92
|
+
# acts_as_solr(
|
93
|
+
# :fields => [:name, :description],
|
94
|
+
# :include => [
|
95
|
+
# {:author => {:using => :fullname, :as => :name}},
|
96
|
+
# {:media_types => {:using => lambda{|media| type_lookup(media.id)}}}
|
97
|
+
# {:distribution_companies => {:as => :distributor, :multivalued => true}},
|
98
|
+
# {:copyright_dates => {:as => :copyright, :type => :date}}
|
99
|
+
# ]
|
100
|
+
# ]
|
101
|
+
#
|
102
|
+
# The options accepted are:
|
103
|
+
#
|
104
|
+
# :type:: Index the associated objects using the specified type
|
105
|
+
# :as:: Index the associated objects using the specified field name
|
106
|
+
# :using:: Index the associated objects using the value returned by the specified method or proc. If a method
|
107
|
+
# symbol is supplied, it will be sent to each object to look up the value to index; if a proc is
|
108
|
+
# supplied, it will be called once for each object with the object as the only argument
|
109
|
+
# :multivalued:: Index the associated objects using one field for each object rather than joining them
|
110
|
+
# all into a single field
|
111
|
+
#
|
112
|
+
# facets:: This option can be used to specify the fields you'd like to
|
113
|
+
# index as facet fields
|
114
|
+
#
|
115
|
+
# class Electronic < ActiveRecord::Base
|
116
|
+
# acts_as_solr :facets => [:category, :manufacturer]
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# boost:: You can pass a boost (float) value that will be used to boost the document and/or a field. To specify a more
|
120
|
+
# boost for the document, you can either pass a block or a symbol. The block will be called with the record
|
121
|
+
# as an argument, a symbol will result in the according method being called:
|
122
|
+
#
|
123
|
+
# class Electronic < ActiveRecord::Base
|
124
|
+
# acts_as_solr :fields => [{:price => {:boost => 5.0}}], :boost => 10.0
|
125
|
+
# end
|
126
|
+
#
|
127
|
+
# class Electronic < ActiveRecord::Base
|
128
|
+
# acts_as_solr :fields => [{:price => {:boost => 5.0}}], :boost => proc {|record| record.id + 120*37}
|
129
|
+
# end
|
130
|
+
#
|
131
|
+
# class Electronic < ActiveRecord::Base
|
132
|
+
# acts_as_solr :fields => [{:price => {:boost => :price_rating}}], :boost => 10.0
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
# if:: Only indexes the record if the condition evaluated is true. The argument has to be
|
136
|
+
# either a symbol, string (to be eval'ed), proc/method, or class implementing a static
|
137
|
+
# validation method. It behaves the same way as ActiveRecord's :if option.
|
138
|
+
#
|
139
|
+
# class Electronic < ActiveRecord::Base
|
140
|
+
# acts_as_solr :if => proc{|record| record.is_active?}
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# offline:: Assumes that your using an outside mechanism to explicitly trigger indexing records, e.g. you only
|
144
|
+
# want to update your index through some asynchronous mechanism. Will accept either a boolean or a block
|
145
|
+
# that will be evaluated before actually contacting the index for saving or destroying a document. Defaults
|
146
|
+
# to false. It doesn't refer to the mechanism of an offline index in general, but just to get a centralized point
|
147
|
+
# where you can control indexing. Note: This is only enabled for saving records. acts_as_solr doesn't always like
|
148
|
+
# it, if you have a different number of results coming from the database and the index. This might be rectified in
|
149
|
+
# another patch to support lazy loading.
|
150
|
+
#
|
151
|
+
# class Electronic < ActiveRecord::Base
|
152
|
+
# acts_as_solr :offline => proc {|record| record.automatic_indexing_disabled?}
|
153
|
+
# end
|
154
|
+
#
|
155
|
+
# auto_commit:: The commit command will be sent to Solr only if its value is set to true:
|
156
|
+
#
|
157
|
+
# class Author < ActiveRecord::Base
|
158
|
+
# acts_as_solr :auto_commit => false
|
159
|
+
# end
|
160
|
+
#
|
161
|
+
def acts_as_solr(options={}, solr_options={})
|
162
|
+
|
163
|
+
extend ClassMethods
|
164
|
+
include InstanceMethods
|
165
|
+
include CommonMethods
|
166
|
+
include ParserMethods
|
167
|
+
|
168
|
+
cattr_accessor :configuration
|
169
|
+
cattr_accessor :solr_configuration
|
170
|
+
|
171
|
+
self.configuration = {
|
172
|
+
:fields => nil,
|
173
|
+
:additional_fields => nil,
|
174
|
+
:exclude_fields => [],
|
175
|
+
:auto_commit => true,
|
176
|
+
:include => nil,
|
177
|
+
:facets => nil,
|
178
|
+
:boost => nil,
|
179
|
+
:if => "true",
|
180
|
+
:offline => false
|
181
|
+
}
|
182
|
+
self.solr_configuration = {
|
183
|
+
:type_field => "type_s",
|
184
|
+
:primary_key_field => "pk_i",
|
185
|
+
:default_boost => 1.0
|
186
|
+
}
|
187
|
+
|
188
|
+
configuration.update(options) if options.is_a?(Hash)
|
189
|
+
solr_configuration.update(solr_options) if solr_options.is_a?(Hash)
|
190
|
+
Deprecation.validate_index(configuration)
|
191
|
+
|
192
|
+
configuration[:solr_fields] = {}
|
193
|
+
configuration[:solr_includes] = {}
|
194
|
+
|
195
|
+
after_save :solr_save
|
196
|
+
after_destroy :solr_destroy
|
197
|
+
|
198
|
+
if configuration[:fields].respond_to?(:each)
|
199
|
+
process_fields(configuration[:fields])
|
200
|
+
else
|
201
|
+
process_fields(self.new.attributes.keys.map { |k| k.to_sym })
|
202
|
+
process_fields(configuration[:additional_fields])
|
203
|
+
end
|
204
|
+
|
205
|
+
if configuration[:include].respond_to?(:each)
|
206
|
+
process_includes(configuration[:include])
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
def get_field_value(field)
|
212
|
+
field_name, options = determine_field_name_and_options(field)
|
213
|
+
configuration[:solr_fields][field_name] = options
|
214
|
+
|
215
|
+
define_method("#{field_name}_for_solr".to_sym) do
|
216
|
+
begin
|
217
|
+
value = self[field_name] || self.instance_variable_get("@#{field_name.to_s}".to_sym) || self.send(field_name.to_sym)
|
218
|
+
case options[:type]
|
219
|
+
# format dates properly; return nil for nil dates
|
220
|
+
when :date
|
221
|
+
value ? (value.respond_to?(:utc) ? value.utc : value).strftime("%Y-%m-%dT%H:%M:%SZ") : nil
|
222
|
+
else value
|
223
|
+
end
|
224
|
+
rescue
|
225
|
+
puts $!
|
226
|
+
logger.debug "There was a problem getting the value for the field '#{field_name}': #{$!}"
|
227
|
+
value = ''
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def process_fields(raw_field)
|
233
|
+
if raw_field.respond_to?(:each)
|
234
|
+
raw_field.each do |field|
|
235
|
+
next if configuration[:exclude_fields].include?(field)
|
236
|
+
get_field_value(field)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def process_includes(includes)
|
242
|
+
if includes.respond_to?(:each)
|
243
|
+
includes.each do |assoc|
|
244
|
+
field_name, options = determine_field_name_and_options(assoc)
|
245
|
+
configuration[:solr_includes][field_name] = options
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def determine_field_name_and_options(field)
|
251
|
+
if field.is_a?(Hash)
|
252
|
+
name = field.keys.first
|
253
|
+
options = field.values.first
|
254
|
+
if options.is_a?(Hash)
|
255
|
+
[name, {:type => type_for_field(field)}.merge(options)]
|
256
|
+
else
|
257
|
+
[name, {:type => options}]
|
258
|
+
end
|
259
|
+
else
|
260
|
+
[field, {:type => type_for_field(field)}]
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
def type_for_field(field)
|
265
|
+
if configuration[:facets] && configuration[:facets].include?(field)
|
266
|
+
:facet
|
267
|
+
elsif column = columns_hash[field.to_s]
|
268
|
+
case column.type
|
269
|
+
when :string then :text
|
270
|
+
when :datetime then :date
|
271
|
+
when :time then :date
|
272
|
+
else column.type
|
273
|
+
end
|
274
|
+
else
|
275
|
+
:text
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
@@ -0,0 +1,236 @@
|
|
1
|
+
module ActsAsSolr #:nodoc:
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
include CommonMethods
|
5
|
+
include ParserMethods
|
6
|
+
|
7
|
+
# Finds instances of a model. Terms are ANDed by default, can be overwritten
|
8
|
+
# by using OR between terms
|
9
|
+
#
|
10
|
+
# Here's a sample (untested) code for your controller:
|
11
|
+
#
|
12
|
+
# def search
|
13
|
+
# results = Book.find_by_solr params[:query]
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# You can also search for specific fields by searching for 'field:value'
|
17
|
+
#
|
18
|
+
# ====options:
|
19
|
+
# offset:: - The first document to be retrieved (offset)
|
20
|
+
# limit:: - The number of rows per page
|
21
|
+
# order:: - Orders (sort by) the result set using a given criteria:
|
22
|
+
#
|
23
|
+
# Book.find_by_solr 'ruby', :order => 'description asc'
|
24
|
+
#
|
25
|
+
# field_types:: This option is deprecated and will be obsolete by version 1.0.
|
26
|
+
# There's no need to specify the :field_types anymore when doing a
|
27
|
+
# search in a model that specifies a field type for a field. The field
|
28
|
+
# types are automatically traced back when they're included.
|
29
|
+
#
|
30
|
+
# class Electronic < ActiveRecord::Base
|
31
|
+
# acts_as_solr :fields => [{:price => :range_float}]
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# facets:: This option argument accepts the following arguments:
|
35
|
+
# fields:: The fields to be included in the faceted search (Solr's facet.field)
|
36
|
+
# query:: The queries to be included in the faceted search (Solr's facet.query)
|
37
|
+
# zeros:: Display facets with count of zero. (true|false)
|
38
|
+
# sort:: Sorts the faceted resuls by highest to lowest count. (true|false)
|
39
|
+
# browse:: This is where the 'drill-down' of the facets work. Accepts an array of
|
40
|
+
# fields in the format "facet_field:term"
|
41
|
+
# mincount:: Replacement for zeros (it has been deprecated in Solr). Specifies the
|
42
|
+
# minimum count necessary for a facet field to be returned. (Solr's
|
43
|
+
# facet.mincount) Overrides :zeros if it is specified. Default is 0.
|
44
|
+
#
|
45
|
+
# dates:: Run date faceted queries using the following arguments:
|
46
|
+
# fields:: The fields to be included in the faceted date search (Solr's facet.date).
|
47
|
+
# It may be either a String/Symbol or Hash. If it's a hash the options are the
|
48
|
+
# same as date_facets minus the fields option (i.e., :start:, :end, :gap, :other,
|
49
|
+
# :between). These options if provided will override the base options.
|
50
|
+
# (Solr's f.<field_name>.date.<key>=<value>).
|
51
|
+
# start:: The lower bound for the first date range for all Date Faceting. Required if
|
52
|
+
# :fields is present
|
53
|
+
# end:: The upper bound for the last date range for all Date Faceting. Required if
|
54
|
+
# :fields is prsent
|
55
|
+
# gap:: The size of each date range expressed as an interval to be added to the lower
|
56
|
+
# bound using the DateMathParser syntax. Required if :fields is prsent
|
57
|
+
# hardend:: A Boolean parameter instructing Solr what do do in the event that
|
58
|
+
# facet.date.gap does not divide evenly between facet.date.start and facet.date.end.
|
59
|
+
# other:: This param indicates that in addition to the counts for each date range
|
60
|
+
# constraint between facet.date.start and facet.date.end, other counds should be
|
61
|
+
# calculated. May specify more then one in an Array. The possible options are:
|
62
|
+
# before:: - all records with lower bound less than start
|
63
|
+
# after:: - all records with upper bound greater than end
|
64
|
+
# between:: - all records with field values between start and end
|
65
|
+
# none:: - compute no other bounds (useful in per field assignment)
|
66
|
+
# all:: - shortcut for before, after, and between
|
67
|
+
# filter:: Similar to :query option provided by :facets, in that accepts an array of
|
68
|
+
# of date queries to limit results. Can not be used as a part of a :field hash.
|
69
|
+
# This is the only option that can be used if :fields is not present.
|
70
|
+
#
|
71
|
+
# Example:
|
72
|
+
#
|
73
|
+
# Electronic.find_by_solr "memory", :facets => {:zeros => false, :sort => true,
|
74
|
+
# :query => ["price:[* TO 200]",
|
75
|
+
# "price:[200 TO 500]",
|
76
|
+
# "price:[500 TO *]"],
|
77
|
+
# :fields => [:category, :manufacturer],
|
78
|
+
# :browse => ["category:Memory","manufacturer:Someone"]}
|
79
|
+
#
|
80
|
+
#
|
81
|
+
# Examples of date faceting:
|
82
|
+
#
|
83
|
+
# basic:
|
84
|
+
# Electronic.find_by_solr "memory", :facets => {:dates => {:fields => [:updated_at, :created_at],
|
85
|
+
# :start => 'NOW-10YEARS/DAY', :end => 'NOW/DAY', :gap => '+2YEARS', :other => :before}}
|
86
|
+
#
|
87
|
+
# advanced:
|
88
|
+
# Electronic.find_by_solr "memory", :facets => {:dates => {:fields => [:updated_at,
|
89
|
+
# {:created_at => {:start => 'NOW-20YEARS/DAY', :end => 'NOW-10YEARS/DAY', :other => [:before, :after]}
|
90
|
+
# }], :start => 'NOW-10YEARS/DAY', :end => 'NOW/DAY', :other => :before, :filter =>
|
91
|
+
# ["created_at:[NOW-10YEARS/DAY TO NOW/DAY]", "updated_at:[NOW-1YEAR/DAY TO NOW/DAY]"]}}
|
92
|
+
#
|
93
|
+
# filter only:
|
94
|
+
# Electronic.find_by_solr "memory", :facets => {:dates => {:filter => "updated_at:[NOW-1YEAR/DAY TO NOW/DAY]"}}
|
95
|
+
#
|
96
|
+
#
|
97
|
+
#
|
98
|
+
# scores:: If set to true this will return the score as a 'solr_score' attribute
|
99
|
+
# for each one of the instances found. Does not currently work with find_id_by_solr
|
100
|
+
#
|
101
|
+
# books = Book.find_by_solr 'ruby OR splinter', :scores => true
|
102
|
+
# books.records.first.solr_score
|
103
|
+
# => 1.21321397
|
104
|
+
# books.records.last.solr_score
|
105
|
+
# => 0.12321548
|
106
|
+
#
|
107
|
+
# lazy:: If set to true the search will return objects that will touch the database when you ask for one
|
108
|
+
# of their attributes for the first time. Useful when you're using fragment caching based solely on
|
109
|
+
# types and ids.
|
110
|
+
#
|
111
|
+
def find_by_solr(query, options={})
|
112
|
+
data = parse_query(query, options)
|
113
|
+
return parse_results(data, options) if data
|
114
|
+
end
|
115
|
+
|
116
|
+
# Finds instances of a model and returns an array with the ids:
|
117
|
+
# Book.find_id_by_solr "rails" => [1,4,7]
|
118
|
+
# The options accepted are the same as find_by_solr
|
119
|
+
#
|
120
|
+
def find_id_by_solr(query, options={})
|
121
|
+
data = parse_query(query, options)
|
122
|
+
return parse_results(data, {:format => :ids}) if data
|
123
|
+
end
|
124
|
+
|
125
|
+
# This method can be used to execute a search across multiple models:
|
126
|
+
# Book.multi_solr_search "Napoleon OR Tom", :models => [Movie]
|
127
|
+
#
|
128
|
+
# ====options:
|
129
|
+
# Accepts the same options as find_by_solr plus:
|
130
|
+
# models:: The additional models you'd like to include in the search
|
131
|
+
# results_format:: Specify the format of the results found
|
132
|
+
# :objects :: Will return an array with the results being objects (default). Example:
|
133
|
+
# Book.multi_solr_search "Napoleon OR Tom", :models => [Movie], :results_format => :objects
|
134
|
+
# :ids :: Will return an array with the ids of each entry found. Example:
|
135
|
+
# Book.multi_solr_search "Napoleon OR Tom", :models => [Movie], :results_format => :ids
|
136
|
+
# => [{"id" => "Movie:1"},{"id" => Book:1}]
|
137
|
+
# Where the value of each array is as Model:instance_id
|
138
|
+
# scores:: If set to true this will return the score as a 'solr_score' attribute
|
139
|
+
# for each one of the instances found. Does not currently work with find_id_by_solr
|
140
|
+
#
|
141
|
+
# books = Book.multi_solr_search 'ruby OR splinter', :scores => true
|
142
|
+
# books.records.first.solr_score
|
143
|
+
# => 1.21321397
|
144
|
+
# books.records.last.solr_score
|
145
|
+
# => 0.12321548
|
146
|
+
#
|
147
|
+
def multi_solr_search(query, options = {})
|
148
|
+
models = multi_model_suffix(options)
|
149
|
+
options.update(:results_format => :objects) unless options[:results_format]
|
150
|
+
data = parse_query(query, options, models)
|
151
|
+
|
152
|
+
if data.nil? or data.total_hits == 0
|
153
|
+
return SearchResults.new(:docs => [], :total => 0)
|
154
|
+
end
|
155
|
+
|
156
|
+
result = find_multi_search_objects(data, options)
|
157
|
+
if options[:scores] and options[:results_format] == :objects
|
158
|
+
add_scores(result, data)
|
159
|
+
end
|
160
|
+
SearchResults.new :docs => result, :total => data.total_hits
|
161
|
+
end
|
162
|
+
|
163
|
+
def find_multi_search_objects(data, options)
|
164
|
+
result = []
|
165
|
+
if options[:results_format] == :objects
|
166
|
+
data.hits.each do |doc|
|
167
|
+
k = doc.fetch('id').first.to_s.split(':')
|
168
|
+
result << k[0].constantize.find_by_id(k[1])
|
169
|
+
end
|
170
|
+
elsif options[:results_format] == :ids
|
171
|
+
data.hits.each{|doc| result << {"id" => doc.values.pop.to_s}}
|
172
|
+
end
|
173
|
+
result
|
174
|
+
end
|
175
|
+
|
176
|
+
def multi_model_suffix(options)
|
177
|
+
models = "AND (#{solr_configuration[:type_field]}:#{self.name}"
|
178
|
+
models << " OR " + options[:models].collect {|m| "#{solr_configuration[:type_field]}:" + m.to_s}.join(" OR ") if options[:models].is_a?(Array)
|
179
|
+
models << ")"
|
180
|
+
end
|
181
|
+
|
182
|
+
# returns the total number of documents found in the query specified:
|
183
|
+
# Book.count_by_solr 'rails' => 3
|
184
|
+
#
|
185
|
+
def count_by_solr(query, options = {})
|
186
|
+
data = parse_query(query, options)
|
187
|
+
data.total_hits
|
188
|
+
end
|
189
|
+
|
190
|
+
# It's used to rebuild the Solr index for a specific model.
|
191
|
+
# Book.rebuild_solr_index
|
192
|
+
#
|
193
|
+
# If batch_size is greater than 0, adds will be done in batches.
|
194
|
+
# NOTE: If using sqlserver, be sure to use a finder with an explicit order.
|
195
|
+
# Non-edge versions of rails do not handle pagination correctly for sqlserver
|
196
|
+
# without an order clause.
|
197
|
+
#
|
198
|
+
# If a finder block is given, it will be called to retrieve the items to index.
|
199
|
+
# This can be very useful for things such as updating based on conditions or
|
200
|
+
# using eager loading for indexed associations.
|
201
|
+
def rebuild_solr_index(batch_size=0, &finder)
|
202
|
+
finder ||= lambda { |ar, options| ar.find(:all, options.merge({:order => self.primary_key})) }
|
203
|
+
start_time = Time.now
|
204
|
+
|
205
|
+
if batch_size > 0
|
206
|
+
items_processed = 0
|
207
|
+
limit = batch_size
|
208
|
+
offset = 0
|
209
|
+
begin
|
210
|
+
iteration_start = Time.now
|
211
|
+
items = finder.call(self, {:limit => limit, :offset => offset})
|
212
|
+
add_batch = items.collect { |content| content.to_solr_doc }
|
213
|
+
|
214
|
+
if items.size > 0
|
215
|
+
solr_add add_batch
|
216
|
+
solr_commit
|
217
|
+
end
|
218
|
+
|
219
|
+
items_processed += items.size
|
220
|
+
last_id = items.last.id if items.last
|
221
|
+
time_so_far = Time.now - start_time
|
222
|
+
iteration_time = Time.now - iteration_start
|
223
|
+
logger.info "#{Process.pid}: #{items_processed} items for #{self.name} have been batch added to index in #{'%.3f' % time_so_far}s at #{'%.3f' % (items_processed / time_so_far)} items/sec (#{'%.3f' % (items.size / iteration_time)} items/sec for the last batch). Last id: #{last_id}"
|
224
|
+
offset += items.size
|
225
|
+
end while items.nil? || items.size > 0
|
226
|
+
else
|
227
|
+
items = finder.call(self, {})
|
228
|
+
items.each { |content| content.solr_save }
|
229
|
+
items_processed = items.size
|
230
|
+
end
|
231
|
+
solr_optimize
|
232
|
+
logger.info items_processed > 0 ? "Index for #{self.name} has been rebuilt" : "Nothing to index for #{self.name}"
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|