How to integrate Convert_tz of MySql in thinking sphinx - ruby-on-rails

how can i integrate Convert_tz of Mysql in thinking sphinx?
i tried to look up for answers but i'am not able to find any.
I'am using ruby - 1.9.3 and rails 3.2 thinking sphinx 2.0.11
eg:
Model.search("", with: {attribute: ">=CONVERT_TZ(#{Time.now}, 'UTC', 'America/New_York')" })

Sphinx has no concept of timezones, and so it doesn't have the CONVERT_TZ function (or anything like it). By default, Sphinx will store times as whatever they're in your database as (very likely UTC), so your best bet is to convert times to the appropriate UTC values when filtering.
And if you want to use greater-than/less-than logic in filters, it needs to be done as a range:
Model.search with: {attribute: Time.zone.now..1.year.from_now}

Related

Ruby on Rails Full Text Search

How can i search full text with sentence and search any word matched or different configuration
example:
"professor john smith" is searching in model title
it have to display result which matched the "proffessor john smith", "professor john" ", "proffesson smith"
There are lots of fulltext search technologies supports rails. elasticsearch and solar search ar eone of them
Rails cast about integrating solar search is here
There are some technologies compatible with rails. For example sunspot, as #scottxu wrote, or elasticsearch, as #knotito said. Good solution also - sphinx (fast and lite) and thinking_sphinx gem. If you use postgresql as database, and you do not want use external solutions - you can choose pg_search gem, that emplements fulltext search with pg technologies only. I prefer elasticsearch, but it requires JVM, and some people don't like this.
updated:
how to use pg_search:
First, you should add pg_search to gemfile, make bundle install, and add to your model something like:
include PgSearch
pg_search_scope :search_by_title, :against => :title
if you didn't do it before.
After, you can try to test it in rails console:
Professor.search_by_title('professor smith')
For fulltext searches you can use Elasticsearch
To implement that you can follow this railscast
you can use sunspot
description:
Sunspot is a Ruby library for expressive, powerful interaction with the Solr search engine. Sunspot is built on top of the RSolr library, which provides a low-level interface for Solr interaction; Sunspot provides a simple, intuitive, expressive DSL backed by powerful features for indexing objects and searching for them.

Ruby on rails -elasticsearch tire impoting existing db

I have a Ruby on rails 3.2 application. I want to enable text based search on a model that has a lot of data already populated in it. Suppose the name of the model class is Post. I am planning on using elasticsearch since I heard it is one of the best real-time search engines around and I am using tire gem so that my application can interact with elasticsearch.
As I am new to elasticsearch I am having trouble creating indices for the existing data for the model. I am using mongodb as the backend database. Can anyone tell me how to import the indices.
I have already tried
Tire.index "posts" do
import Post.all
end
The error that I got was :
BSON::InvalidObjectId: illegal ObjectId format: Career Guidance
from /Users/anirvan/.rvm/gems/ruby-1.9.3-p125/gems/bson-1.5.1/lib/bson/types/object_id.rb:126:in `from_string'
Can anyone help me out here ?
I use Mysql and this code in bash(from railscasts.com):
rake environment tire:import CLASS=Post FORCE=true
http://www.elasticsearch.org/guide/appendix/clients.html
tire: Ruby API & DSL, with full Rails ActiveModel compatibility and mongoid integration through mebla.
Try it, maybe help you.

Partial matches with postgresql full-text search using texticle on Heroku

Trying to get searching working on Heroku using partial search
The following query generates an SQL error on Heroku, but works correctly in my locally version:
#events.search(params[:search]+":*")
I am using the Heroku shared database service, is this a possible difference in syntax between PostgreSQL versions?
What syntax should I be using to do a partial matching searching against a full-text index in PostgreSQL 8?
Here are the changes in PostgreSQL 9.1.
Perhaps you could try using string interpolation instead of concatenation.
#events.search("#{params[:search]}:*")
I'm not really sure what the kiss emoticon :* adds to texticle's functionality. Maybe I need to learn more SQL.
It turns out that PostgreSQL version 8 does not support partial searches using the :* syntax.
As http://www.postgresql.org/docs/9.0/interactive/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES says, using ":" is for specify prefix matching. E.g. If searching "Australia" with "Aus:" will work but not "ust:*".
So concat OR xxxx LIKE "%yyy%" will works better
You can use the tsearch option with a prefix:
:tsearch => {:prefix => true}

How do I format datetime values to more friendly formats?

I'm fairly new to Ruby on Rails. I created a blog using SQLite3, because I am unable to install MySQL, and would like to display the date of creation next to the title.
The date of creation is created using a timestamp. As I am new to RoR I created the blog using generate scaffold so I'm not 100% sure how that all works.
At the moment, when I display the created_at field, I'm given an ugly format:
2011-12-05 14:11:10 UTC
Is there a way to change this so that it display DD-MM-YYYY HH:MM, or preferably to say "posted 30 days ago". I realize the latter would be a lot more tricky.
I think you're looking for strftime - ruby documentation here.
Example:
irb(main):001:0> a = Time.now
irb(main):002:0> a.strftime("%d-%m-%Y %H:%M")
=> "05-12-2011 15:08"
First of all: you will have to install something other than SQLite (I recommend PostgreSQL, not MySQL). SQLite is unsuitable for production, and you don't really want your dev and production DB servers to be different.
To your question: Ruby and Rails have lots of date formatting methods. to_s might do what you want, or there's things like ActionView::Helpers::DateHelper.time_ago_in_words.

mysql2 driver seems to write invalid queries

I'm developing an application layer on top of a rails app developed by someone else.
His application uses a module called request_logger to write to a table, which worked fine under ruby1.8/rails2/mysql gem, but in my ruby1.9/rails3/mysql2 environment, activerecord falls over, suggesting that the generated query is invalid.
It obviously is, all mysql relation names are wrapped in double quotes instead of backticks.
The call to activerecord itself just sets a bunch of attributes with
log.attributes = {
:user_id => user_id,
:controller => controller,
...etc
}
and then calls
log.save
So I'm leaning towards it not being dodgy invocation. Any suggestions?
mysql2 works fine for a lot of people, but it unashamedly sacrifices conformance to the MySQL C API for performance in the common tasks. Perhaps, if request_logger is low-level enough, it's expecting calls to exist which don't.
It's trivial to switch back to using mysql - give it a try, and if it works, stick with it. Remember to change both your Gemfile and your config/database.yml settings.
It turned out to be what seems to be a change in behaviour between rails 2 and 3 (we have the same setup working fine in rails 2)
We use database.yml to specify an (empty) "master" database and then feed in our clients with shards+octopus.
The master db is sqlite for simplicity, and it seems that activerecord was feeding off requests formatted for sqlite to the mysql2 shards, regardless of their adaptor type.

Resources