websolr-rails 0.0.0 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +15 -0
- data/CHANGELOG +8 -0
- data/LICENSE +19 -0
- data/README.rdoc +69 -0
- data/Rakefile +112 -0
- data/TESTING_THE_PLUGIN +25 -0
- data/VERSION +1 -0
- data/bin/websolr +10 -0
- data/config/solr.yml +15 -0
- data/config/solr_environment.rb +22 -0
- data/generate_rdoc.sh +13 -0
- data/lib/acts_as_solr.rb +65 -0
- data/lib/acts_methods.rb +281 -0
- data/lib/class_methods.rb +243 -0
- data/lib/common_methods.rb +89 -0
- data/lib/deprecation.rb +61 -0
- data/lib/instance_methods.rb +181 -0
- data/lib/lazy_document.rb +18 -0
- data/lib/parser_methods.rb +230 -0
- data/lib/search_results.rb +72 -0
- data/lib/solr.rb +21 -0
- data/lib/solr/connection.rb +190 -0
- data/lib/solr/document.rb +78 -0
- data/lib/solr/exception.rb +13 -0
- data/lib/solr/field.rb +39 -0
- data/lib/solr/importer.rb +19 -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 +43 -0
- data/lib/solr/importer/xpath_mapper.rb +35 -0
- data/lib/solr/indexer.rb +52 -0
- data/lib/solr/request.rb +26 -0
- data/lib/solr/request/add_document.rb +63 -0
- data/lib/solr/request/base.rb +36 -0
- data/lib/solr/request/commit.rb +31 -0
- data/lib/solr/request/delete.rb +50 -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 +51 -0
- data/lib/solr/request/optimize.rb +21 -0
- data/lib/solr/request/ping.rb +36 -0
- data/lib/solr/request/select.rb +56 -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/response.rb +27 -0
- data/lib/solr/response/add_document.rb +17 -0
- data/lib/solr/response/base.rb +42 -0
- data/lib/solr/response/commit.rb +17 -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 +28 -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 +64 -0
- data/lib/solr/response/xml.rb +42 -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_fixtures.rb +13 -0
- data/lib/tasks/database.rake +18 -0
- data/lib/tasks/solr.rake +94 -0
- data/lib/tasks/test.rake +7 -0
- data/lib/websolr-rails.rb +1 -0
- data/lib/websolr.rb +1 -0
- data/lib/websolr_controller.rb +233 -0
- data/lib/websolr_option_parser.rb +61 -0
- data/lib/websolr_rails.rb +1 -0
- data/lib/websolr_rails/tasks.rb +4 -0
- data/lib/will_paginate_support.rb +12 -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/logs/.empty-dir-for-git +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/tmp/.empty-dir-for-git +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 +70 -0
- data/test/unit/class_methods_shoulda.rb +88 -0
- data/test/unit/common_methods_shoulda.rb +112 -0
- data/test/unit/instance_methods_shoulda.rb +326 -0
- data/test/unit/lazy_document_shoulda.rb +35 -0
- data/test/unit/parser_instance.rb +19 -0
- data/test/unit/parser_methods_shoulda.rb +278 -0
- data/test/unit/solr_instance.rb +46 -0
- data/test/unit/test_helper.rb +14 -0
- data/websolr-rails.gemspec +243 -0
- metadata +229 -18
data/lib/solr/xml.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# The ASF licenses this file to You under the Apache License, Version 2.0
|
2
|
+
# (the "License"); you may not use this file except in compliance with
|
3
|
+
# the License. You may obtain a copy of the License at
|
4
|
+
#
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
#
|
7
|
+
# Unless required by applicable law or agreed to in writing, software
|
8
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
9
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
10
|
+
# See the License for the specific language governing permissions and
|
11
|
+
# limitations under the License.
|
12
|
+
|
13
|
+
module Solr::XML
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
|
18
|
+
# If we can load rubygems and libxml-ruby...
|
19
|
+
require 'rubygems'
|
20
|
+
require 'xml/libxml'
|
21
|
+
raise "acts_as_solr requires libxml-ruby 0.7 or greater" unless XML::Node.public_instance_methods.include?("attributes")
|
22
|
+
|
23
|
+
# then make a few modifications to XML::Node so it can stand in for REXML::Element
|
24
|
+
class XML::Node
|
25
|
+
# element.add_element(another_element) should work
|
26
|
+
alias_method :add_element, :<<
|
27
|
+
|
28
|
+
|
29
|
+
# element.text = "blah" should work
|
30
|
+
def text=(x)
|
31
|
+
self << x.to_s
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# And use XML::Node for our XML generation
|
36
|
+
Solr::XML::Element = XML::Node
|
37
|
+
|
38
|
+
rescue LoadError => e # If we can't load either rubygems or libxml-ruby
|
39
|
+
puts "Requiring REXML"
|
40
|
+
# Just use REXML.
|
41
|
+
require 'rexml/document'
|
42
|
+
Solr::XML::Element = REXML::Element
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActsAsSolr
|
2
|
+
|
3
|
+
class SolrFixtures
|
4
|
+
def self.load(table_names)
|
5
|
+
[table_names].flatten.map { |n| n.to_s }.each do |table_name|
|
6
|
+
klass = instance_eval(File.split(table_name.to_s).last.to_s.gsub('_',' ').split(" ").collect{|w| w.capitalize}.to_s.singularize)
|
7
|
+
klass.rebuild_solr_index if klass.respond_to?(:rebuild_solr_index)
|
8
|
+
end
|
9
|
+
ActsAsSolr::Post.execute(Solr::Request::Commit.new)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../solr_fixtures'
|
2
|
+
|
3
|
+
namespace :db do
|
4
|
+
namespace :fixtures do
|
5
|
+
desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y"
|
6
|
+
task :load => :environment do
|
7
|
+
begin
|
8
|
+
ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "*:*"))
|
9
|
+
ActsAsSolr::Post.execute(Solr::Request::Commit.new)
|
10
|
+
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
|
11
|
+
ActsAsSolr::SolrFixtures.load(File.basename(fixture_file, '.*'))
|
12
|
+
end
|
13
|
+
puts "The fixtures loaded have been added to Solr"
|
14
|
+
rescue
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/tasks/solr.rake
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'net/http'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
namespace :solr do
|
7
|
+
|
8
|
+
desc 'Starts Solr. Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.'
|
9
|
+
task :start do
|
10
|
+
puts "Use: websolr start"
|
11
|
+
end
|
12
|
+
|
13
|
+
desc 'Stops Solr. Specify the environment by using: RAILS_ENV=your_env. Defaults to development if none.'
|
14
|
+
task :stop do
|
15
|
+
puts "Use: websolr stop"
|
16
|
+
end
|
17
|
+
|
18
|
+
# this task is by Henrik Nyh
|
19
|
+
# http://henrik.nyh.se/2007/06/rake-task-to-reindex-models-for-acts_as_solr
|
20
|
+
desc %{Reindexes data for all acts_as_solr models. Clears index first to get rid of orphaned records and optimizes index afterwards. RAILS_ENV=your_env to set environment. ONLY=book,person,magazine to only reindex those models; EXCEPT=book,magazine to exclude those models. START_SERVER=true to solr:start before and solr:stop after. BATCH=123 to post/commit in batches of that size: default is 300. CLEAR=false to not clear the index first; OPTIMIZE=false to not optimize the index afterwards.}
|
21
|
+
task :reindex => :environment do
|
22
|
+
require "#{File.dirname(__FILE__)}/../../config/solr_environment.rb"
|
23
|
+
|
24
|
+
includes = env_array_to_constants('ONLY')
|
25
|
+
if includes.empty?
|
26
|
+
includes = Dir.glob("#{RAILS_ROOT}/app/models/*.rb").map { |path| File.basename(path, ".rb").camelize.constantize }
|
27
|
+
end
|
28
|
+
excludes = env_array_to_constants('EXCEPT')
|
29
|
+
includes -= excludes
|
30
|
+
|
31
|
+
optimize = env_to_bool('OPTIMIZE', true)
|
32
|
+
start_server = env_to_bool('START_SERVER', false)
|
33
|
+
clear_first = env_to_bool('CLEAR', true)
|
34
|
+
batch_size = ENV['BATCH'].to_i.nonzero? || 300
|
35
|
+
debug_output = env_to_bool("DEBUG", false)
|
36
|
+
|
37
|
+
RAILS_DEFAULT_LOGGER.level = ActiveSupport::BufferedLogger::INFO unless debug_output
|
38
|
+
|
39
|
+
if start_server
|
40
|
+
puts "Starting Solr server..."
|
41
|
+
Rake::Task["solr:start"].invoke
|
42
|
+
end
|
43
|
+
|
44
|
+
# Disable solr_optimize
|
45
|
+
module ActsAsSolr::CommonMethods
|
46
|
+
def blank() end
|
47
|
+
alias_method :deferred_solr_optimize, :solr_optimize
|
48
|
+
alias_method :solr_optimize, :blank
|
49
|
+
end
|
50
|
+
|
51
|
+
models = includes.select { |m| m.respond_to?(:rebuild_solr_index) }
|
52
|
+
models.each do |model|
|
53
|
+
|
54
|
+
if clear_first
|
55
|
+
puts "Clearing index for #{model}..."
|
56
|
+
ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "#{model.solr_configuration[:type_field]}:#{model}"))
|
57
|
+
ActsAsSolr::Post.execute(Solr::Request::Commit.new)
|
58
|
+
end
|
59
|
+
|
60
|
+
puts "Rebuilding index for #{model}..."
|
61
|
+
model.rebuild_solr_index(batch_size)
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
if models.empty?
|
66
|
+
puts "There were no models to reindex."
|
67
|
+
elsif optimize
|
68
|
+
puts "Optimizing..."
|
69
|
+
models.last.deferred_solr_optimize
|
70
|
+
end
|
71
|
+
|
72
|
+
if start_server
|
73
|
+
puts "Shutting down Solr server..."
|
74
|
+
Rake::Task["solr:stop"].invoke
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def env_array_to_constants(env)
|
80
|
+
env = ENV[env] || ''
|
81
|
+
env.split(/\s*,\s*/).map { |m| m.singularize.camelize.constantize }.uniq
|
82
|
+
end
|
83
|
+
|
84
|
+
def env_to_bool(env, default)
|
85
|
+
env = ENV[env] || ''
|
86
|
+
case env
|
87
|
+
when /^true$/i then true
|
88
|
+
when /^false$/i then false
|
89
|
+
else default
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
data/lib/tasks/test.rake
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/acts_as_solr"
|
data/lib/websolr.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/acts_as_solr"
|
@@ -0,0 +1,233 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "rubygems"
|
3
|
+
require "restclient"
|
4
|
+
require 'rexml/document'
|
5
|
+
require "fileutils"
|
6
|
+
require "net/http"
|
7
|
+
include FileUtils
|
8
|
+
|
9
|
+
SOLR_PATH = "#{File.dirname(File.expand_path(__FILE__))}/../solr" unless defined? SOLR_PATH
|
10
|
+
SOLR_LOGS_PATH = "#{ENV["PWD"]}/log" unless defined? SOLR_LOGS_PATH
|
11
|
+
SOLR_PIDS_PATH = "#{ENV["PWD"]}/tmp/pids" unless defined? SOLR_PIDS_PATH
|
12
|
+
SOLR_DATA_PATH = "#{ENV["PWD"]}/solr/#{ENV['RAILS_ENV']}" unless defined? SOLR_DATA_PATH
|
13
|
+
SOLR_JVM_OPTIONS = ENV["JAVA_OPTIONS"] || "-Xmx256M"
|
14
|
+
|
15
|
+
mkdir_p SOLR_PATH
|
16
|
+
mkdir_p SOLR_LOGS_PATH
|
17
|
+
mkdir_p SOLR_PIDS_PATH
|
18
|
+
mkdir_p SOLR_DATA_PATH
|
19
|
+
|
20
|
+
class WebsolrController
|
21
|
+
COMMANDS = %w[add list delete configure local:start local:stop]
|
22
|
+
SOLR_PORT = 8983
|
23
|
+
|
24
|
+
def initialize(parser)
|
25
|
+
@options = parser.options
|
26
|
+
@command = @options.delete(:command)
|
27
|
+
@parser = parser
|
28
|
+
@user = @options[:user] ||= ENV["WEBSOLR_USER"]
|
29
|
+
@pass = @options[:pass] ||= ENV["WEBSOLR_PWD"]
|
30
|
+
if @user && @pass
|
31
|
+
@base = "http://#{URI::escape @user}:#{URI::escape @pass}@websolr.com"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def die(s)
|
36
|
+
STDERR.puts s
|
37
|
+
exit(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
def required_options(hash)
|
41
|
+
hash = hash.dup
|
42
|
+
if hash.delete(:auth) && (!@user || !@pass)
|
43
|
+
die <<-STR
|
44
|
+
|
45
|
+
You need to specify your username and password, either on the command
|
46
|
+
line with the -u and -p flags, or in the WEBSOLR_USER and WEBSOLR_PWD
|
47
|
+
environment variables.
|
48
|
+
|
49
|
+
STR
|
50
|
+
end
|
51
|
+
hash.inject(true) do |memo, (key, flag)|
|
52
|
+
unless @options[key]
|
53
|
+
STDERR.puts "Please use the #{flag} flag to specify the #{key}."
|
54
|
+
end
|
55
|
+
memo && @options[key]
|
56
|
+
end || exit(1)
|
57
|
+
end
|
58
|
+
|
59
|
+
def url(url)
|
60
|
+
URI.join(@base, url).to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
def check_local_solr_conditions
|
64
|
+
ENV["RAILS_ENV"] = @options[:rails_env] || ENV["RAILS_ENV"] || "development"
|
65
|
+
begin
|
66
|
+
require "config/environment"
|
67
|
+
rescue LoadError
|
68
|
+
die("I can't find config/environment.rb. Are we in a rails app?")
|
69
|
+
end
|
70
|
+
|
71
|
+
unless ENV["WEBSOLR_URL"]
|
72
|
+
ENV["WEBSOLR_URL"] = "http://localhost:8983/solr"
|
73
|
+
puts <<-STR
|
74
|
+
|
75
|
+
You haven't configured your app. You might want to do that. I
|
76
|
+
assume you just want a quick development server, so I'll start
|
77
|
+
one up for you at http://localhost:8983/solr.
|
78
|
+
|
79
|
+
You should let Rails know about it by setting the WEBSOLR_URL, i.e:
|
80
|
+
|
81
|
+
> ./script/server WEBSOLR_URL=http://localhost:8983/solr
|
82
|
+
|
83
|
+
If you want to set up a full environment, run websolr configure.
|
84
|
+
|
85
|
+
STR
|
86
|
+
puts "Is this what you want? [yes]"
|
87
|
+
if STDIN.gets.strip =~/^(yes)?$/i
|
88
|
+
puts "Continuing...."
|
89
|
+
else
|
90
|
+
die "Aborted."
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
uri = URI.parse(ENV["WEBSOLR_URL"])
|
96
|
+
@port = uri.port
|
97
|
+
rescue URI::InvalidURIError => e
|
98
|
+
die(e.message)
|
99
|
+
end
|
100
|
+
|
101
|
+
def cmd_local_start
|
102
|
+
check_local_solr_conditions
|
103
|
+
begin
|
104
|
+
n = Net::HTTP.new('127.0.0.1', @port)
|
105
|
+
n.request_head('/').value
|
106
|
+
|
107
|
+
rescue Net::HTTPServerException #responding
|
108
|
+
puts "Port #{@port} in use" and return
|
109
|
+
|
110
|
+
rescue Errno::ECONNREFUSED #not responding
|
111
|
+
Dir.chdir(SOLR_PATH) do
|
112
|
+
pid = fork do
|
113
|
+
exec "java #{SOLR_JVM_OPTIONS} -Dsolr.data.dir=#{SOLR_DATA_PATH} -Djetty.logs=#{SOLR_LOGS_PATH} -Djetty.port=#{@port} -jar start.jar"
|
114
|
+
end
|
115
|
+
sleep(5)
|
116
|
+
File.open("#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid", "w"){ |f| f << pid}
|
117
|
+
puts "#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_PORT}, pid: #{pid}."
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def cmd_local_stop
|
123
|
+
ENV["RAILS_ENV"] = @options[:rails_env] || ENV["RAILS_ENV"] || "development"
|
124
|
+
fork do
|
125
|
+
file_path = "#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid"
|
126
|
+
if File.exists?(file_path)
|
127
|
+
File.open(file_path, "r") do |f|
|
128
|
+
pid = f.readline
|
129
|
+
Process.kill('TERM', pid.to_i)
|
130
|
+
end
|
131
|
+
File.unlink(file_path)
|
132
|
+
Rake::Task["solr:destroy_index"].invoke if ENV['RAILS_ENV'] == 'test'
|
133
|
+
puts "Solr shutdown successfully."
|
134
|
+
else
|
135
|
+
puts "PID file not found at #{file_path}. Either Solr is not running or no PID file was written."
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def cmd_add
|
141
|
+
required_options :name => "-n", :auth => true
|
142
|
+
doc = post "/slices.xml", {:slice => {:name => name}}
|
143
|
+
puts "#{x doc, '//name'}\t#{x doc, '//base-url'}"
|
144
|
+
end
|
145
|
+
|
146
|
+
def cmd_delete
|
147
|
+
required_options :name => "-n", :auth => true
|
148
|
+
delete "/slices/#{name}/destroy"
|
149
|
+
puts "done"
|
150
|
+
end
|
151
|
+
|
152
|
+
def x(doc, path)
|
153
|
+
REXML::XPath.first(doc, path).text
|
154
|
+
end
|
155
|
+
|
156
|
+
def cmd_list
|
157
|
+
required_options :auth => true
|
158
|
+
doc = get "/slices.xml"
|
159
|
+
REXML::XPath.each(doc, "//slice") do |node|
|
160
|
+
puts "#{x node, 'name'}\t#{x node, 'base-url'}"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
%w[get post delete put].each do |verb|
|
165
|
+
eval <<-STR
|
166
|
+
def #{verb}(url, params = {})
|
167
|
+
str = RestClient.#{verb} url(url), params
|
168
|
+
return nil if str.strip == ""
|
169
|
+
REXML::Document.new(StringIO.new str)
|
170
|
+
rescue RestClient::RequestFailed => e
|
171
|
+
print_errors REXML::Document.new(StringIO.new e.response.body)
|
172
|
+
end
|
173
|
+
STR
|
174
|
+
end
|
175
|
+
|
176
|
+
def print_errors(doc)
|
177
|
+
REXML::XPath.each(doc, "//error") do |node|
|
178
|
+
STDERR.puts "Error: #{node.text}"
|
179
|
+
end
|
180
|
+
exit 1
|
181
|
+
end
|
182
|
+
|
183
|
+
def cmd_configure
|
184
|
+
required_options :name => "-n", :auth => true
|
185
|
+
doc = get "/slices.xml"
|
186
|
+
found = false
|
187
|
+
REXML::XPath.each(doc, "//slice") do |node|
|
188
|
+
if x(node, 'name') == self.name
|
189
|
+
found = true
|
190
|
+
FileUtils.mkdir_p "config/initializers"
|
191
|
+
path = "config/initializers/websolr.rb"
|
192
|
+
puts "Writing #{path}"
|
193
|
+
File.open(path, "w") do |f|
|
194
|
+
str = <<-STR
|
195
|
+
require 'websolr'
|
196
|
+
case RAILS_ENV
|
197
|
+
when 'production'
|
198
|
+
ENV['WEBSOLR_URL'] ||= '#{x node, 'base-url'}'
|
199
|
+
else
|
200
|
+
ENV['WEBSOLR_URL'] ||= 'http://localhost:8983/solr'
|
201
|
+
end
|
202
|
+
STR
|
203
|
+
f.puts str
|
204
|
+
end
|
205
|
+
|
206
|
+
FileUtils.mkdir_p "lib/tasks"
|
207
|
+
path = "lib/tasks/websolr.rake"
|
208
|
+
puts "Writing #{path}"
|
209
|
+
File.open(path, "w") do |f|
|
210
|
+
f.puts "require 'rubygems'\nrequire 'websolr_rails/tasks'"
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
unless found
|
215
|
+
STDERR.puts "Error: Index not found"
|
216
|
+
exit 1
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def start
|
221
|
+
if(COMMANDS.include?(@command))
|
222
|
+
send("cmd_#{@command.gsub(/\W+/, '_')}")
|
223
|
+
else
|
224
|
+
puts @parser
|
225
|
+
exit(1)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def method_missing(method, *a, &b)
|
230
|
+
return @options[method] if @options[method]
|
231
|
+
super(method, *a, &b)
|
232
|
+
end
|
233
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
class WebsolrOptionParser < OptionParser
|
4
|
+
attr_accessor :options
|
5
|
+
|
6
|
+
def usage
|
7
|
+
"Usage: #{$0} COMMAND [INDEX_NAME] [options]
|
8
|
+
|
9
|
+
COMMANDs:
|
10
|
+
local:start - starts the local development server
|
11
|
+
local:stop - stops the local development server
|
12
|
+
|
13
|
+
add - creates a new index
|
14
|
+
list - shows your indexes
|
15
|
+
delete - deletes an index
|
16
|
+
configure - adds websolr to your current Rails app
|
17
|
+
|
18
|
+
"
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse!
|
22
|
+
super
|
23
|
+
self.options[:command] = ARGV[0]
|
24
|
+
self.options[:name] ||= ARGV[1]
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
self.options = {}
|
29
|
+
super do |opts|
|
30
|
+
|
31
|
+
yield opts if block_given?
|
32
|
+
|
33
|
+
opts.banner = usage
|
34
|
+
|
35
|
+
opts.on("-u", "--user=USER", "Your Websolr username") do |u|
|
36
|
+
options[:user] = u
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on("-p", "--password=PASSWORD", "Your Websolr password") do |p|
|
40
|
+
options[:pass] = p
|
41
|
+
end
|
42
|
+
|
43
|
+
opts.on("-n", "--name=NAME", "Name of the index") do |p|
|
44
|
+
options[:name] = p
|
45
|
+
end
|
46
|
+
|
47
|
+
opts.on("-e", "--rails-env=ENV", "RAILS_ENV") do |p|
|
48
|
+
options[:rails_env] = p
|
49
|
+
end
|
50
|
+
|
51
|
+
opts.on("-i", "--invitation=CODE", "Your invitation code") do |p|
|
52
|
+
options[:invitation_code] = p
|
53
|
+
end
|
54
|
+
|
55
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
56
|
+
puts opts
|
57
|
+
exit
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|