femalemoustache
femalemoustache

Reputation: 591

Sphinx connection error on search results page in Ruby on Rails application

I have a RoR app with Posgresql as database. I'm trying to setup search functionality by using Sphinx as search engine and Thinking Sphinx gem. I've installed sphinx with mysql and postgresql support, thinking sphinx v3 gem and it's dependencies.

The command

rake ts:index

performs without errors. Log says that I have 20 docs indexed (all my records of Post model). Then I've tried to create controller and view for search results page. Here's the Controller's search action

def search
    @query = Riddle::Query.escape(params[:q])
    @posts = Post.search(@query)
end

And when I try to use @posts variable in search view

localhost:3000/search?q=hello

I have following error.

ThinkingSphinx::ConnectionError in Main#search

Error connecting to Sphinx via the MySQL protocol. Error connecting to Sphinx via the MySQL protocol. Can't connect to MySQL server on '127.0.0.1' (61) - SELECT * FROM post_core WHERE MATCH('hello') AND sphinx_deleted = 0 LIMIT 0, 20; SHOW META

Upvotes: 3

Views: 1648

Answers (3)

SSR
SSR

Reputation: 6438

I was stuck at all and facing the same error, Finally I found that I have not installed "Sphinx" on my local system with this command,

sudo apt-get update
sudo apt-get install sphinxsearch

More detail regarding installation you can find here

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-sphinx-on-ubuntu-14-04

Make sure you should also have installed "mysql-server" on your system to create connection.

Also I looked @blelump's answer was my second step. You should include those gems too in order to run with sphinx.

Then move to @pat's answer was my final step to go, When you installed sphinx it will start service but you need to stop service to run,

rake ts:index
rake ts:rebuild

Here to go with SPHINX.

Upvotes: 0

pat
pat

Reputation: 16226

The ts:index task just stores the Sphinx data, it doesn't start the daemon which responds to search requests. You need to run ts:start rake task for this to happen.

Also: ts:rebuild does all of this at once: stops Sphinx (if it's running), indexes data, starts Sphinx.

Upvotes: 5

blelump
blelump

Reputation: 3243

Thinking Sphinx uses mysql for its internal purposes and you have to add mysql2 gem to your stack, e.g:

gem 'mysql2', '~> 0.3.13'
gem 'thinking-sphinx', '~> 3.1.1'

Upvotes: 1

Related Questions