Thursday, September 29, 2011

Running Thinking Sphinx in two applications

Here i'm gonna explain you how to run thinking sphinx in two applications.
Solved Answer: thinking sphinx undefined local variable
Solution for " thinking sphinx undefined local variable "
Answer: This can be done by running thinking sphinx server on two ports
for example: you have two rails applications on your server.
now by default thinking sphinx runs on port 9312

step 1: stop thinking sphinx server if running on first application by typing " rake ts:stop " in your           terminal.
step 2: go to first application folder and delete the files " development.sphinx.conf, test.sphinx.conf and production.sphinx.conf " which are in config folder.
step 3: find the file named sphinx.yml in same config folder, if it doesn't exist create new file with name   " sphinx.yml "
step 4: edit the file sphinx.yml, place this below code in it.
#################################################################################
development:
  port: 9313
test:
  port: 9314
production:
  port: 9315
#################################################################################
IMPORTANT NOTE: SPACE BEFORE THE WORD PORT SHOULD BE TWO SPACES, IF NOT IT DOESN'T WORK.

step 5: now open terminal and go to your application folder and type " rake ts:config "
           thinking sphinx will create new file
           " development.sphinx.conf "
           ( if you have done indexing for testing and production mode then those two files will also be created. )
           you can check that file where you can see in line no 7
           " listen = 127.0.0.1:9313 "
           listen port will be same as you assigned in sphinx.yml file.
step 6: do the same procedure for second application but with different port numbers
           for example:
#################################################################################
           development:
             port: 9316
           test:
             port: 9317
           production:
             port: 9318
#################################################################################
step 7: now start your rails applications on different ports ( i.e ex: " rails s -p 4000 " )
           and thinking sphinx servers on both applications ( rake ts:start )

thats it.

was this helpfull please let me know if you have queries.
please post comments and let me know some feedback.








Wednesday, September 28, 2011

Rails Thinking Sphinx

Here i'm gonna explain you how to use thinking sphinx in rails application.
this can be done in 5 steps 
step 1:  install the gem thinking sphinx.
           i have installed through terminal ( ubuntu linux ) by typing 
           " sudo gem install thinking-sphinx "
           this will install the gem for you.
           include this gem in gem file of your rails application.
           i.e gem 'thinking-sphinx'
           now run bundle install.
step 2:  open the model file for which you want to add search module.
           here i'm taking an example model  " student "
           in this model file student.rb we have two fields 
           1.name
           2.school
           here we are making search on this two fields " name " and " school ". 
           now, add this code in student.rb file
#################################################################################
              define_index do
                  indexes :name
                  indexes :school
              end
#################################################################################
           here we are telling the thinking-sphinx gem to make indexing on these two fields.
step 3:  open the controller file i.e students_controller.rb 
           go to " index " method 
           here basically you will have 
           " @students = Student.all "
           now change this to 
           " @students = Student.search params[:search] "
           here we are adding parameter "search " to make search on the model student.
step 4:  open the view file of index method i.e index.html.erb
           add this form in it
#################################################################################
                            <%= form_tag students_path, :method => :get do  %>
                                   <p>
                                         <%= text_field_tag :search  % >                              
                                   </p>
                                  <p class="button">
                                         <%= submit_tag "Search" %>
                                 </p>
                            <% end %>
#################################################################################
            here we are adding search field to input search request and return the result.
step 5: open terminal and start thinking sphinx indexing by typing
              " rake ts:index "
           next, start thinking sphinx server by typing 
              " rake ts:start "
           you can stop thinking sphinx by typing
             " rake ts:stop "
start you rails server and check it out.
Note: unless you start thinking sphinx server you cant even view page.
thats it. 
was this helpfull please let me know if you have queries.
please post comments and let me know some feedback.