The search function works fine in development and Production. But when I run Rspec tests. the result array is always empty
In my search action log, I can see the data has been indexed and fetched correctly:
Parameters: {"utf8"=>"✓", "ga_client_id"=>"", "search"=>"David"}
SOLR Request (3.9ms) [ path=#<RSolr::Client:0x007fb7f9141f00> parameters={data: fq=type%3AToon&sort=created_at_d+asc&q=David&fl=%2A+score&qf=name_text+author_text+toon_typetext+tags_text&defType=dismax&start=0&rows=9, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8981/solr/select?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
Toon Load (0.4ms) SELECT "toons".* FROM "toons" WHERE "toons"."id" IN (15550, 15551, 15552) AND (state in ('created','flagged'))
In my Controller
#search = Toon.solr_search do
fulltext params[:search]
order_by :created_at, :asc
paginate :page => params[:page], :per_page => 9
end
#toons = #search.results
My rspec test: https://gist.github.com/jianbo/6360486
In testing View
<%= #search.inspect %> This output correctly as follow:
<Sunspot::Search:{:fq=>["type:Toon"], :sort=>"created_at_d asc", :q=>"papabear", :fl=>"* score", :qf=>"name_text author_text toon_type_text tags_text", :defType=>"dismax", :start=>0, :rows=>9}>
<%= #toons.inspect %> is empty array
UPDATE
I just found that if I do no clear the index and the database, and after running the test twice, I was able to search for the result that was in the first run test.
You will want to fire up a Solr instance for the tests. There is a very helpful snippet here that can help: https://github.com/sunspot/sunspot/wiki/RSpec-and-Sunspot#running-sunspot-during-testing
Related
i'm trying to implement simple search function with thinking sphinx, basing on this. i managed to set sphinx up, but when i try it in a console, 0 results are found every time. i'm wondering what is wrong here...
indices/news_post_index.rb:
ThinkingSphinx::Index.define :news_post, :with => :active_record do
indexes title
indexes content
end
and the rails console output:
2.0.0p0 :013 > NewsPost.create!(title: "awesome post", content: "this post is awesome and you know it", published_at: "2001-01-01")
(0.3ms) BEGIN
NewsPost Exists (60.7ms) SELECT 1 AS one FROM `news_posts` WHERE `news_posts`.`slug` = 'awesome-post' LIMIT 1
SQL (48.3ms) INSERT INTO `news_posts` (`content`, `created_at`, `published_at`, `slug`, `title`, `updated_at`) VALUES ('this post is awesome and you know it', '2013-09-10 08:18:35', '2001-01-01', 'awesome-post', 'awesome post', '2013-09-10 08:18:35')
(40.1ms) COMMIT
=> #<NewsPost id: 2, published_at: "2001-01-01", content: "this post is awesome and you know it", title: "awesome post", created_at: "2013-09-10 08:18:35", updated_at: "2013-09-10 08:18:35", slug: "awesome-post">
2.0.0p0 :014 > NewsPost.search "awesome post"
Sphinx Query (0.9ms) SELECT * FROM `news_post_core` WHERE MATCH('awesome post #sphinx_internal_class_name (NewsPost)') AND `sphinx_deleted` = 0 LIMIT 0, 20
Sphinx Found 0 results
=> []
From what i understand title is indexed, and so I should be able to search for it. but no matter what my key is, results are always empty array, no results at all...
Run rake ts:index after every update, then search.
I am trying to save some information, actually editing one existing, and I created a form like this
%form{ :action => "/users/custom", :method => "post", :controller => "/users", data: {remote: true}}
%input{:type => "text", :name => "name", :class => "text ", :value => " #{#current_user.first_name} #{#current_user.last_name}" }
%input{:type => "text", :name => "age", :class => "text ", :value => " #{#current_user.age}" }
and I have others (weight, and address)... the problem that only one for "mail" with the exact same way of these fields is being persisted on the DB, but the other fields, for example age they get persisted with 0
On my controller I have something like this
#user = User.find(session[:current_user])
#user.email = params[:mail]
#user.weight = params[:weight].to_i
... and so on...
if #user.save
flash[:notice] = 'The User is successfully saved!'
end
any way to fix this? or to know what is wrong?
thanks
EDIT
Started POST "/users/custom" for 192.168.1.21 at 2013-05-08 15:50:04 -0600
Processing by UsersController#custom as JS
Parameters: {"name"=>"name", "mail"=>"NEW MAIL", "sex"=>"1", "weight"=>"180" ... }
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
(0.1ms) BEGIN
(0.3ms) UPDATE `users` SET `email` = 'NEW MAIL', `sex` = 0, `ssn` = 0, `updated_at` = '2013-05-08 21:50:04' WHERE `users`.`id` = 1
(1.2ms) COMMIT
editing the name doesnt work, the only thing that works its the email.
When I want to edit them with the scaffolds' default forms, I cant see the field weight and so on, just few, incluiding name (which from here I cant edit either)
My model has some restrictions.
validates :first_name, :format => { :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed" }, :allow_blank => true
The problem is even if I remove it... keeps inserting 0... any idea?
You can put a debug breakpoint by doing: binding.pry anywhere in your controller action. Be sure to add the Pry gem to your Gemfile like so gem 'pry'.
Then execute your code and the breakpoint should hit in your server window for you to debug the problem.
I am using sunspot to search my local db. After adding the gems, running the generate command, and booting up the solr server I do the following:
class Style < ActiveRecord::Base
attr_accessible :full_name, :brand_name
searchable do
text :full_name
text :brand_name
end
end
Added the above to my Style model and re-indexed (I had already indexed prior to creating this post, which is why I re-indexed to put it here)
funkdified#vizio ~/rails_projects/goodsounds.org $ rake sunspot:solr:reindex
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
*Note: the reindex task will remove your current indexes and start from scratch.
If you have a large dataset, reindexing can take a very long time, possibly weeks.
This is not encouraged if you have anywhere near or over 1 million rows.
Are you sure you want to drop your indexes and completely reindex? (y/n)
y
[#######################################] [14/14] [100.00%] [00:00] [00:00] [53.19/s]
Then I try a search and get nothing
1.9.3p392 :003 > Style.search { fulltext 'Monkey' }.results
SOLR Request (10.4ms) [ path=#<RSolr::Client:0x0000000685ab28> parameters={data: fq=type%3AStyle&q=Monkey&fl=%2A+score&qf=full_name_text+brand_name_text&defType=dismax&start=0&rows=30, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8982/solr/select?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
=> []
But, wait shouldn't it have worked and picked this up?
Style.first
Style Load (1.3ms) SELECT "styles".* FROM "styles" LIMIT 1
=> #<Style id: 54, brand_name: "Monkey", full_name "Monkey Chicken", created_at: "2013-02-01 23:25:58", updated_at: "2013-02-16 03:02:16">
Here is one more clue. I am seeing "unknown field" for brand_name (setup in Style.rb)
If you change the schema (the "searchable" block) you have to either reindex all models:
rake sunspot:solr:reindex
or reindex that specific model with a given batch size (here 500):
rake sunspot:solr:reindex[500,Style]
as per the Sunspot doco on Github (search "Reindexing Objects").
FYI, to use Style.reindex for non-schema changes, you will have to call Sunspot.commit to save changes.
I find it more convenient to check response for some requests from within console
>> app.put '/users/2/'
=> 500
But wasn't able to find a way to specify request parameters. How I have to do that?
If you want to put or post to a URL there are also methods for that. You can copy/paste the parameters exactly as they are displayed in your Rails production log:
app.post('/foo', {"this" => "that", "items" => ["bar", "baz"]})
app.put('/foo', {"this" => "that", "items" => ["bar", "baz"]})
If you want to sent a custom header, you can add an optional third parameter:
app.post('/foo', {:this => "that", :items => ["bar", "baz"]}, {"X-Do-Something" => "yes"})
Any of the get/post/put/delete methods will display their full log output on the console for you to examine. If you want to get information such as the response body returned, HTTP status or response headers these are easy too:
app.response.body
app.response.status
app.response.headers.inspect
Source: http://andyjeffries.co.uk/articles/debug-level-logging-for-a-single-rails-production-request
The above has changed to
app.post '/foo', params: {"this" => "that", "items" => ["bar", "baz"]}
Also for forms I had to give an authenticity_token as well.
So in my example the full command was
app.post '/login', params: {email: 'my_email#gmail.com', password: 'abcd', authenticity_token: 'my_authenticity_token_generated_for_this_view' }
I've written a basic Rails 3 application that shows a form and an upload form on specific URLs. It was all working fine yesterday, but now I'm running into several problems that require fixing. I'll try to describe each problem as best as I can. The reason i'm combining them, is because I feel they're all related and preventing me from finishing my task.
1. Cannot run the application in development mode
For some unknown reason, I cannot get the application to run in development mode. Currently i've overwritten the production.rb file from the environment with the settings from the development environment to get actuall stacktraces.
I've added the RailsEnv production setting to my VirtualHost setting in apache2, but it seems to make no difference. Nor does settings ENV variable to production.
2. ArgumentError on all calls
Whatever call I seem to make, results in this error message. The logfile tells me the following:
Started GET "/" for 192.168.33.82 at
Thu Apr 07 00:54:48 -0700 2011
ArgumentError (wrong number of
arguments (1 for 0)):
Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.0ms) Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(4.1ms) Rendered
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb
within rescues/layout (8.4ms)
This means nothing to me really. I have no clue what's going wrong. I currently have only one controller which looks like this:
class SearchEngineController < ApplicationController
def upload
end
def search
#rows = nil
end
# This function will receive the query string from the search form and perform a search on the
# F.I.S.E index to find any matching results
def query
index = Ferret::Index::Index.new :path => "/public/F.I.S.E", :default_field => 'content'
#rows = Array.New
index.search_each "content|title:#{params[:query]}" do |id,score, title|
#rows << {:id => id, :score => score, :title => title}
end
render :search
end
# This function will receive the file uploaded by the user and process it into the
# F.I.S.E for searching on keywords and synonims
def process
index = Ferret::Index::Index.new :path => "public/F.I.S.E", :default_field => 'content'
file = File.open params[:file], "r"
xml = REXML::Document.new file
filename = params[:file]
title = xml.root.elements['//body/title/text()']
content = xml.root.elements['normalize-space(//body)']
index << { :filename => filename, :title => title, :content => content}
file.close
FileUtils.rm file
end
end
The routing of my application has the following setup: Again this is all pretty basic and probably can be done better.
Roularta::Application.routes.draw do
# define all the url paths we support
match '/upload' => 'search_engine#upload', :via => :get
match '/process' => 'search_engine#process', :via => :post
# redirect the root of the application to the search page
root :to => 'search_engine#search'
# redirect all incoming requests to the query view of the search engine
match '/:controller(/:action(/:id))' => 'search_engine#search'
end
If anyone can spot what's wrong and why this application is failing, please let me know. If needed I can edit this awnser and include additional files that might be required to solve this problem.
EDIT
i've managed to get further by renaming one of the functions on the controller. I renamed search into create and now I'm getting back HAML errors. Perhaps I used a keyword...?
woot, finally found the solutions....
Seems I used keywords to define my actions, and Rails didn't like this. This solved issue 2.
Issue 1 got solved by adding Rails.env= 'development' to the environment.rb file