Thinking Sphinx on Windows
In one of my Ruby on Rails projects, I needed a full-text search engine to integrate. And after some research, I decided that Thinking Sphinx is the way to go. It seemed simple, fast and well documented. Unfortunately, I had some issues when tried to get it running on Windows.
The first step is installing Sphinx itself. While this looks simple, it took me five hours to move to the next step!
When I checked Sphinx documentation, it said that I needed Microsoft Visual C/C++ Studio to compile the source code! What? This is another project. After some (a lot of) research, some people suggested that I just needed to download the binaries, not the source code, and add the bin folder to the Windows Path environemnt variable. I tried that, but got errors later when tried to index data. So here is what works:
- Go to Sphinx downloads page and download the Win32 release binaries with MySQL support. At the time of writing it is sphinx-0.9.8-win32.zip. Of course, you should get the one with PostgreSQL support if you need it.
- Unzip the downloaded file and open the bin folder. Select and copy all files and paste them in your ruby/bin folder. If some files are already exist in your ruby/bin folder, don't replace them.
- Install the Thinking Sphinx plugin:
- To set a model to be indexed, add some fields and attributes (refer to the primer):
class Post < ActiveRecord::Base define_index do #sphinx fields indexes title end end - Tell Sphinx to index the data:
Now, you may get something like this in your console:rake thinking_sphinx:index
It seems that it couldn't continue. To solve this, make sure you copied all the .exe files from sphinx-0.9.8-win32/bin to ruby/bin. If you keep getting the same result, you may need to copy the libmySQL.dll file from mysql/bin to ruby/bin (check this post for details). Now, you should get something like this:rake thinking_sphinx:index Generating Configuration to F:/InstantRails/rails_apps/community/config/development.sphinx.conf indexer --config F:/InstantRails/rails_apps/community/config/development.sphinx.conf --all
Also, note the newly generated files:Generating Configuration to F:/InstantRails/rails_apps/community/config/development.sphinx.conf indexer --config F:/InstantRails/rails_apps/community/config/development.sphinx.conf --all Sphinx 0.9.8-release (r1371) Copyright (c) 2001-2008, Andrew Aksyonoff using config file 'F:/InstantRails/rails_apps/community/config/development.sphinx.conf'... indexing index 'post_core'... collected 2 docs, 0.0 MB collected 0 attr values sorted 0.0 Mvalues, 100.0% done sorted 0.0 Mhits, 100.0% done total 2 docs, 27 bytes total 0.030 sec, 904.37 bytes/sec, 66.99 docs/sec distributed index 'post' can not be directly indexed; skipping.- config/development.sphinx.conf
- db/sphinx/development/post_core.spa
- db/sphinx/development/post_core.spd
- db/sphinx/development/post_core.sph
- db/sphinx/development/post_core.spi
- db/sphinx/development/post_core.spm
- db/sphinx/development/post_core.spp
A distributed index is made up of other indexes. It doesn't need to be indexed, but Sphinx tries to anyway, and then complains when it can't. I'm not sure about the reasoning for this, but it's not important: You can ignore the error, it doesn't stop anything working, searching and indexing will happen as expected.
- Start a Sphinx searchd daemon:
You should get something like this:rake thinking_sphinx:startsearchd --config F:/InstantRails/rails_apps/community/config/development.sphinx.conf [Wed Oct 29 14:26:44.390 2008] [ 2796] WARNING: forcing --console mode on Windows [Wed Oct 29 14:26:44.390 2008] [ 2796] using config file 'F:/InstantRails/rails_apps/community/config/development.sphinx.conf'... [Wed Oct 29 14:26:44.406 2008] [ 2796] creating server socket on 127.0.0.1:3312 [Wed Oct 29 14:26:44.406 2008] [ 2796] accepting connections
ruby script/plugin install git://github.com/freelancing-god/thinking-sphinx.git
Now, you are ready to search. To test it, you can modify the index action as follows:
class PostsController < BaseController
def index
@posts = Post.search params[:search]
end
end
And your view may look like this:
<% for post in @posts %>
<h2><%=h post.subject %></h2>
<%=h post.body %>
<% end %>
Now, navigate to /posts?search=myword where myword is a string that can be found in the title of some of your posts. You should see that the results are filtered according to the keyword sent.
Congratulations! You are thinking sphinx on Windows.
Did you like this article? Bookmark it:
Related Articles
- Running Aptana/Eclipse and InstantRails from a USB Drive
- Thinking Sphinx in Arabic/Unicode
- Thinking Sphinx on Windows
- Installing CommunityEngine on Rails 2.1
- We Chose CommunityEngine for a Rails Social Network
3 Comments
Leave a Comment
If you want to post code, do this:
<pre><code class="ruby|javascript|css|html"> your code here </code></pre>








jc
November 29th, 2008 - 10:10 PM
just what i was looking for! this should be on the thinking sphinx man page
farmer
December 15th, 2008 - 08:25 AM
Thanks, that was very helpful! There was one file in overlap - iconv.dll - as you mentioned might be the case. I just copied the .exe files and it fixed it. Also I first tried including the /bin directory for sphinx in my path but that didn't help.
Hatem
December 15th, 2008 - 04:38 PM
@jc: Thank you. I hope I saved you some time.
@farmer: Thanks, the same happened with me.