Solr reindex with associations - ruby-on-rails-3

I have the following code in my Model:
has_many :links
searchable do
text :name, :as => :name_textp
integer :membership_ordering
text :business_description, :stored => true
text :tags do
tags.map(&:name)
end
text :links_name do
company_links.map(&:name)
end
text :links_description do
company_links.map(&:description)
end
text :links_tags do
links.map(&:tags_text)
end
end
When for some reason I update the model, Solr automatically add the change to the index. But when I update links , it seems it doesn't until I manually call the sunspot:solr:reindex. I also added the following code to the association:
searchable do
text :name
text :description
text :tags_text
end
But I can't get it work. What else do I need to do?

have you been installing solr from scratch or with bundle install command
Please have a look to this procedure, I think that will make works your ruby / solr configuration.
Prefere to launch solr with :
bundle exec rake sunspot:solr:start
bundle exec rake sunspot:reindex
Enjoy :)
UPDATE 6/06 1H49 BRT
Do you have set autocommit in your solrconfig.xml:
<autoCommit>
<maxDocs>10000</maxDocs>
<maxTime>20000</maxTime>
</autoCommit>
UPDATE 6/06 9H41 BRT
Please take a look a this post: sunset not reindexing it seems to be a common issue.
Can be overpassed by performing a commit.
As dzone says :
you will need regular reindexing with sunspot.

Related

ThinkingSphinx::SphinxError index item_core,item_delta: query error: no field 'deleted_at' found in schema

I have an Item model:
class Item < ActiveRecord::Base
...
define_index do
...
has deleted_at
indexes deleted_at
end
...
Then I stop the server, run rake ts:rebuild && rake ts:reindex && rake ts:restart, restart the server. And I still get the error.
The query that triggers the error is this:
Item.search({conditions: {deleted_at: nil}})
What's wrong ?
Note: I am using acts_as_paranoid. My database is migrated, the table items has the column deleted_at.
Somewhere in schema.rb:
...
create_table "items", :force => true do |t|
...
t.datetime "deleted_at"
There's a couple of related issues here:
You've got an attribute and a field with the same name (deleted_at) - you should alias one to have a different name using the :as option. Otherwise, Sphinx gets confused when reading the generated SQL data.
You've got a date column as a field, when it almost certainly isn't required - do you want users searching by deleted_at, or is it purely something for you to manage internally? I'm guessing the latter. So, remove the indexes deleted_at line.
You're setting a field condition in your search call when it should really be an attribute filter - which is done using the :with option.
Sphinx has no concept of nil, but should translate NULL values from the database into zeros.
So, with all that in mind, your index definition should rather be:
define_index do
# ...
has deleted_at
end
Run rake ts:rebuild to ensure Sphinx is aware of the changes and your index files are updated.
Then your search call is:
Item.search with: {deleted_at: 0}

Mongoid code not working in production

The following code works in development like a charm, but in production no update is ever issued to the database. Basically I am trying to update a collection element that is nested 3 levels deep ..
def set_collection_value
#article = Article.find(params[:id])
#article.highlights.items.find(params[:item_id])
item.update_attributes(params[:values])
render :json => item
end
The model Article in question looks like this:
class Article
include Mongoid::Document
embeds_one :highlight, class_name: 'Highlight'
end
highlight.rb
class Highlight
includes Mongoid::Document
embedded_in :article
embeds_many :items, class_name: 'HighlightElement'
end
highlight_item.rb
class HighlightElement
include Mongoid::Document
embedded_in :highlight
field :title, type: String
field :teaser, type: String
field :image, type: String
field :body, type: String
attr_accessible :title, :teaser, :image, :body
end
The funny thing here is that even when I run webrick locally in production mode it works like a charm. Only once I deploy to my ubuntu server running mongodb v1.2.2 the above code silently does nothing.
I even went as far as to copy my environments/development.rb onto my environments/production.rb hoping to fix the issue.. but to no avail..
Any ideas? I am running rails 3.2.7 with mongoid 3.0.3
It would also be helpful if someone could point out how to make the moped/mongoid debug log messages show up in production.log. I configured Debugging as described in the logs - but these debug messages are only visible when running rails s - not in production.log
Solved it myself..
Turns out Mongodb 1.2.2 from the official Ubuntu repository sources was the problem.
After updating to 2.0.5 everything worked again correctly.

Searching issue with the rails-sunspot gem

I'm very new to Solr and the Rails Sunspot gem, but it looks very promising for complex searching on a big database.
What I'm trying to do is allow a model in my rails app to be searched on a few fulltext columns and then a collection of its "filters" (which are just a has_and_belongs_to_many association of names).
I tried setting up my model search block as follows
self.searchable do
text :name, :boost => 5
text :description, :instructions
text :filters do
filters.map(&:name)
end
end
And my controller looks like so:
#search = ModelName.search do
keywords params[:q].to_s
end
However, I cannot seem to produce any results based on keywords found in the filters association. Am I doing something wrong? This is all very new to me.
When you initially set up your classes for search, you need to reindex the data into Solr. Have you done that? If not:
rake sunspot:reindex

How to add_index in mysql2+rails 3?

How do you add indexes in mysql2?
with line:
execute "alter table urli_development.slugs ADD INDEX slugs_sluggable_id(sluggable_id)"
everything works fine.
with line: add_index :slugs, :sluggable_id I get this 'Invalid date' error.
Problem is that I have to add following line:
add_index(:slugs, [:name, :sluggable_type, :sequence, :scope], :unique => true, :name => 'index_slugs_on_n_s_s_and_s')
Above line doesn't work.
I use Rails 3 and mysql2 and I get this 'Invalid date' error when I try to migrate database.
How do you do it?
I've had that problem too and to be quite frankly haven't got to a complete "why".But I do want to share something I just found out.
Try creating your table by excluding the "id" field....the add_index will run right away.So
class CreateSlugs < ActiverRecord::Migration
def self.up
create_table :slugs,:id=>false do |t|
t.string "name"
t.timestamps
end
add_index :indexes,"name",:name=>"slugs_name",:unique=>true
add_column :slugs,:id,:primary_key
end
end
Or if you wish to do it within a different migration
class AlterSlugs < ActiveRecord::Migration
def self
#add_index :slugs,:name,:unique
#add_column :slugs,:id,:primary_key
end
def self.down
end
end
Actually is pretty interesting-> If you switch those two lines (add_index and add_column) ...IT WON'T WORK!
you ran into this bug.
(I assume you're on Windows)
You can solve it by copying a fresh file:///C:/../mysql/lib/opt/libmysql.dll from your mysql 5.1 install to your Ruby/bin directory.
Then it works again!
You need to copy and paste, "libmysql.ddl" anew into ruby's bin folder.
You can get it in the folder of Mysql Workbench if you have it installed in your machine or from alternative sources.
Try using a previous version of the mysql gem instead of mysql2...
gem 'mysql', '2.8.1'
# gem 'mysql2'
I've run into this bug twice, in two different projects and this saved my day in both cases :)
It's also worth mentioning that add_index causes no trouble at all using mysql2 in Ubuntu. I guess this is a windows only issue.

model missing required attr_accessor for 'photo_file_name' when uploading with paperclip and S3 on heroku

Setting up paperclip with S3 in my linux dev environment was a snap -- everything works out of the box. However, I can't get it to work on Heroku.
When I try to do an upload, the log shows:
Processing ItemsController#create (for 72.177.97.9 at 2010-08-26 16:35:14) [POST]
Parameters: {"commit"=>"Create", "authenticity_token"=>"0Hy3qvQBHE1gvFVaq32HMy2ZIopelV0BHbrSeHkO1Qw=", "item"=>{"photo"=>#<File:/home/slugs/270862_4aa601b_4b6f/mnt/tmp/RackMultipart20100826-6286-1256pvc-0>, "price"=>"342", "name"=>"a new item", "description"=>"a new item", "sold"=>"0"}}
Paperclip::PaperclipError (Item model missing required attr_accessor for 'photo_file_name'):
I found one blog post that referenced this error, and it said to add this to my model:
attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at
That does indeed make the model missing required attr_accessor for 'photo_file_name' error go away, but it still doesn't work. See my other question for details. As I have figured out that with the attr_accessor lines added to my model the uploads fail even on my dev system, I suspect that is not the right answer.
Found the problem: needed to update the database.
heroku run rake:db:migrate
heroku restart
I had done what I thought would have accomplished the same thing already:
heroku rake db:schema:load
but perhaps that doesn't work or something went wrong in the process.
Error like this occurs if you create wrong column type in migration. When you define new table migration for paperclip, you need to specify t.attachment :name insted of t.string :name. Or add_attachment :table, :name when you add new paperclip column in existed table. And now you don't need to add these attributes in attr_accessor in model.
Well, this message seems to be because the columns it's missing. Try create a migration creating the columns:
class AddPhotoToEvent < ActiveRecord::Migration
def change
add_column :events, :photo_file_name, :string
add_column :events, :photo_content_type, :string
add_column :events, :photo_file_size, :integer
add_column :events, :photo_updated_at, :datetime
end
end
This work for me, here i have a table events with photo