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

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}

Related

What is the source of "unknown OID" errors in Rails?

When replicating an app to production, my POSTGIS table columns started misbehaving, with Rails informing me there was an "unknown OID 26865" and that the fields would be treated as String.
Instead of current_pos yielding e. g.
#<RGeo::Geographic::SphericalPointImpl:0x22fabdc "POINT (13.39318248760133 52.52908798020595)"> I would get 0101000020E6100000FFDD958664C92A403619DEE6B2434A40. It looked like the activerecord-postgis-adapter was not installed, or installed badly, but I eliminated that possibility by testing for the existence of data type RGeo::Feature::Point and by test-assigning
current_pos = "POINT (13.39318248760133 52.52908798020595)"
to the field - which proceeded without error but then yielded another incomprehensible hex string like the above.
Also, strangely enough, POSTGIS was working correctly within the database, e.g. giving correct results for a ST_DISTANCE query. A very limited problem thus, where writing, writing-parsing (from Point to hex format), manipulating by SQL and reading all worked, only the parsing upon read didn't.
When I tried to use migrations to ensure the database column would have the correct type, the migrations failed, giving
undefined method `st_point' for #<ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition:0x00000005cb80b8>
I spent several hours trying all kinds of solutions, even re-installing the server from scratch, double-checking version numbers of everything, installing a slightly newer version of Ruby and a slightly older version of POSTGIS (to match my other environment), exporting the database and starting with a clean one, and so on. After I had done migrations and arrived at the "undefined method st_point" error, I was finally able to find the solution via Google, way down in a Github issue, and it's really simple:
In config/database.yml, swap out postgres:// for postgis:// in the database url. If you're using Heroku, this may require some ugly manipulation:
production:
url: <%= ENV.fetch('DATABASE_URL', '').sub(/^postgres/, "postgis") %>
So silly...
Do not forget to add activerecord-postgis-adapter to your Gemfile so #Sprachprofi's solution can run.

How to integrate Convert_tz of MySql in thinking sphinx

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}

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.

using .find with a regular expression

Is there anyway in ruby on rails to make a query using regexp?
I think it might depend on the database. I know in MySQL you can do something like:
Model.find(:conditions => "field REGEXP '.*'")
if you want a true regex, or you can use the LIKE syntax for similar string matching.
While it doesn't have regex, there's also a gem called MetaWhere that provides more advanced finder functionality.

How to search the pre and post part of the word using Thinking_sphinx gem on rails

I have created my search part successfully with the thinking_sphinx gem and I have searched a word it is showing correct, But the problem with the search is it need full word to be given to search exact match. I dont want to give exact word instead any particular character or part of words also be taken into account. help me to solve this problem..
example
say my word is "Sample"
instead of giving sample i need to specify part of the word like.
amp or ple
for rails code we will specify
find :all, :conditions => ["user_id like ? ", "%#{search}%"]
how to specify this, by using thinking_sphinx gem.
i tried with,
User.search "*amp*"
but of no result guide me to solve this.
Got the Result,
At first I have added
development:
enable_star: 1
min_infix_len: 3
on config -> sphinx.yml
and I have rebuild my application by using
rake ts:rebuild
and then on controller,
User.search "*mple", :star => true
which result,
sample

Resources