Permalink
Browse files

Add sphinx_setup task.

Improve documentation.

[git-p4: depot-paths = "//src/Sphincter/dev/": change = 3496]
  • Loading branch information...
1 parent e1a6d05 commit 670aa9fa738e892723cf043bc33492b8a5046647 @drbrain drbrain committed Aug 10, 2007
Showing with 87 additions and 16 deletions.
  1. +35 −14 README.txt
  2. +2 −2 Rakefile
  3. +50 −0 lib/sphincter/tasks.rb
View
@@ -4,6 +4,10 @@ Eric Hodel <drbrain@segment7.net>
http://seattlerb.org/Sphincter
+File bugs:
+
+http://rubyforge.org/tracker/?func=add&group_id=1513&atid=5921
+
Sphincter was named by David Yeu.
== DESCRIPTION:
@@ -41,22 +45,24 @@ For complete documentation:
Download and install Sphinx from http://www.sphinxsearch.com/downloads.html
-Download Sphinx Ruby API from http://rubyforge.org/frs/?group_id=2604&release_id=11049
-
-Unpack Sphinx Ruby API into vendor/plugins/.
-
Install Sphincter:
$ gem install Sphincter
+Load Sphincter tasks in Rakefile:
+
+ require 'sphincter/tasks'
+
+Setup the Dmytro Shteflyuk's Sphinx client:
+
+ $ rake sphincter:setup_sphinx
+
+Add vendor/plugins/sphinx to your SCM system.
+
Load Sphincter in config/environment.rb:
require 'sphincter'
-By default, Sphincter will run searchd on the same port for all
-environments. See Sphincter::Configure for how to configure different
-environments to use different ports.
-
Add indexes to models:
class Post < ActiveRecord::Base
@@ -74,12 +80,25 @@ Add searching UI:
end
end
+ <ol>
+ <% @results.records.each do |post| -%>
+ <li>
+ <div><%= link_to post.title, post_path(post) %></div>
+ <div><%= truncate post.body, 250 %></div>
+ </li>
+ <% end -%>
+ </ol>
+
Start searchd:
$ rake sphincter:start_searchd
Then test it out in your browser.
+NOTE: By default, Sphincter will run searchd on the same port for all
+environments. See Sphincter::Configure for how to configure different
+environments to use different ports.
+
== TESTING QUICK-START:
See Sphinx::SearchStub.
@@ -92,33 +111,35 @@ Example ActiveRecord model:
class Post < ActiveRecord::Base
belongs_to :blog
+ belongs_to :user
# published is a boolean and title and body are string or text fields
+ # user.name is automatically fetched via the user association
add_index :fields => %w[title body published]
end
-
+
Simple search:
Post.search 'words'
-
+
Only search published posts:
Post.search 'words', :conditions => { :published => 1 }
-
+
Only search posts created in the last week:
now = Time.now
ago = now - 1.weeks
Post.search 'words', :between => { :created_on => [ago, now] }
-
+
Pagination (defaults to ten records/page):
Post.search 'words', :page => 2
-
+
Pagination with custom page size:
Post.search 'words', :page => 2, :per_page => 20
-
+
Pagination with custom page size (better):
Add to config/sphincter.yml:
View
@@ -9,8 +9,8 @@ Hoe.new('Sphincter', Sphincter::VERSION) do |p|
p.rubyforge_name = 'seattlerb'
p.author = 'Eric Hodel'
p.email = 'drbrain@segment7.net'
- p.summary = p.paragraphs_of('README.txt', 5).first
- p.description = p.paragraphs_of('README.txt', 6).first
+ p.summary = p.paragraphs_of('README.txt', 7).first
+ p.description = p.paragraphs_of('README.txt', 8).first
p.url = p.paragraphs_of('README.txt', 2).first
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
View
@@ -45,6 +45,56 @@
desc 'Restarts the searchd sphinx daemon'
task :restart_searchd => %w[sphincter:stop_searchd sphincter:start_searchd]
+ desc 'Sets up the sphinx client'
+ task :setup_sphinx do
+ require 'fileutils'
+ require 'tmpdir'
+ require 'open-uri'
+
+ verbose = Rake.application.options.trace
+
+ begin
+ tmpdir = File.join Dir.tmpdir, "Sphincter_setup_#{$$}"
+
+ mkdir tmpdir, :verbose => true
+
+ chdir tmpdir
+
+ src = open "http://rubyforge.org/frs/download.php/19571/sphinx-0.3.0.zip"
+ File.open('sphinx-0.3.0.zip', 'wb') { |dst| dst.write src.read }
+
+ quiet = verbose ? '' : ' -q'
+ sh "unzip#{quiet} sphinx-0.3.0.zip" or
+ raise "couldn't unzip sphinx-0.3.0.zip"
+
+ File.open 'sphinx.patch', 'wb' do |patch|
+ patch.puts <<-EOF
+--- sphinx/lib/client.rb.orig 2007-04-05 06:38:14.000000000 -0700
++++ sphinx/lib/client.rb 2007-07-29 20:23:18.000000000 -0700
+@@ -398,6 +398,7 @@
+ \r
+ result['matches'][doc] ||= {}\r
+ result['matches'][doc]['weight'] = weight\r
++ result['matches'][doc]['index'] = count\r
+ attrs_names_in_order.each do |attr|\r
+ val = response[p, 4].unpack('N*').first; p += 4\r
+ result['matches'][doc]['attrs'] ||= {}\r
+ EOF
+ end
+
+ quiet = verbose ? ' --verbose' : ''
+ sh "patch#{quiet} -p0 sphinx/lib/client.rb sphinx.patch" or
+ raise "couldn't patch sphinx"
+
+ sphinx_plugin_dir = File.join RAILS_ROOT, 'vendor', 'plugins', 'sphinx'
+ rm_rf sphinx_plugin_dir, :verbose => true
+
+ mv 'sphinx', sphinx_plugin_dir, :verbose => true
+ ensure
+ rm_rf tmpdir, :verbose => true
+ end
+ end
+
desc 'Starts the searchd sphinx daemon'
task :start_searchd => :index do
unless Sphincter::Configure.searchd_running? then

0 comments on commit 670aa9f

Please sign in to comment.