Search queries containing a period break the generated SQL in some versions of Active Record
Hi,
Eager loading associations seems to cause an error:
SomeSearch.pgsearch_scope('SomethingWith.com')
This works. However, once you add eager loading, will cause it to fail :
SomeSearch.pgsearch_scope('SomethingWith.com').includes(:some_association)
ActiveRecord::StatementInvalid: PGError: ERROR: column "pg_search_rank" does not exist
LINE 1: ...SomethingWith.com' || ' ''' || ':*')))) ORDER BY pg_search_...
Has anyone else faced this issue?
Interesting. I think it's because we add a select() to the scope to get the pg_search_rank to be attached to each record, and calling includes() overrides PgSearch's select().
If you reverse the scope calls does it have the same error?
In other words, try this:
SomeSearch.includes(:some_association).pgsearch_scope('SomethingWith.com')
Ah - This seems to be related to this : http://stackoverflow.com/questions/5199235/activerecord-query-changing-when-a-dot-period-is-in-condition-value
I think that Stack Overflow bug is actually unrelated. Over there, Rails is generating the SQL when you pass a hash into where(), but within PgSearch, we are generating SQL directly and passing a string to where().
I think that our bug is more related to this issue (on the Pivotal Tracker project) https://www.pivotaltracker.com/story/show/9808733
Hi,
Sorry didn't see the updates, yeah its the same regardless of the order of placement of includes.
But its not an ambigious error like pivotal tracker error suggests, whenever a period is added to the query, the whole query changes and loses the appended pg_search_rank.
Is it an option to put an includes writer on the scopes? That would be similar to how its passed in gems like Sunspot, with a dsl on the model, kinda makes sense.
So if we had access to an includes hash, in a similar way to column names here : https://github.com/Casecommons/pg_search/blob/master/lib/pg_search/configuration/association.rb#L20
Then includes gets applied by the scope instead of handling externally before/after.
I am also seeing this issue:
Say I have the following:
class Property << ActiveRecord::Base
has_many :open_houses
include PgSearch
pg_search_scope :search_by_keywords,
:against => :description,
:using => {
:tsearch => {
:dictionary => "english",
:any_word => true
}
}
end
And I try and do a query like so:
Property.search_by_keywords('brownstone').paginate(:conditions => ["(opt_out = false) AND (price >= ? AND price <= ?) AND (open_houses.start_time BETWEEN ? AND ?)", 50000, 350000, '2011-10-13 00:00:00', '2011-10-16 23:59:59'], :includes => [:open_houses], :page => 1, :per_page => 25)
it is generating the following query:
SELECT * FROM (
SELECT DISTINCT ON ("properties".id) "properties".id, pg_search_rank AS alias_0, "properties"."id" AS alias_1
FROM "properties"
LEFT OUTER JOIN "open_houses" ON open_houses.property_id = properties.id
WHERE (
(
(properties.opt_out = false) AND
(properties.price >= 50000) AND (properties.price <= 350000) AND
(open_houses.start_time BETWEEN '2011-10-13 00:00:00' AND '2011-10-16 23:59:59') AND
) AND
(
((to_tsvector(E'english', coalesce("properties"."description", ''))) @@ (to_tsquery(E'english', E''' ' || E'brownstone' || E' ''')))
)
) AS id_list
ORDER BY id_list.alias_0 DESC, id_list.alias_1
LIMIT 25 OFFSET 0
)
with the error:
ActiveRecord::StatementInvalid (PGError: ERROR: column "pg_search_rank" does not exist LINE 1: ...CT DISTINCT ON ("properties".id) "properties".id, pg_search_...
Is there any way to go it to not try and add the ranking when doing eager loading?
~ Tom
Tom - Remove the period in your conditions : 'open_houses.start_time', and i bet it will work.
@nertzy : definitely think this is related to this : https://github.com/rails/rails/issues/950 , so looks like a rails thing not a pg_search thing.
@tommeier i changed my :includes => [:open_houses] to a :joins => "LEFT JOIN open_houses on open_houses.property_id = properties.id" and the query worked
unforuntately I also want the open houses to eager load so I have both the :joins hard coded definition as well of the :includes => [:open_houses] and its now working ...
@tcocca yup, pretty much the same here, for now, written some breaking specs in active record, but the fix is eluding me.
You can also setup your own eager loading, effectively doing the same as an includes :
ActiveRecord::IdentityMap.use do
properties = Property.search_by_keywords('brownstone').paginate(:conditions => ["(opt_out = false) AND (price >= ? AND price <= ?) AND (open_houses.start_time BETWEEN ? AND ?)", 50000, 350000, '2011-10-13 00:00:00', '2011-10-16 23:59:59'], :page => 1, :per_page => 25)
includes = OpenHouse.where(:property_id => properties).all
end
Unfortunately (for this last app) stuck on rails 2.3.x for at least the next month but I'll get there soon!
On Fri, Oct 14, 2011 at 5:29 PM, Tom Meier < [email protected]>wrote:
@tcocca yup, pretty much the same here, for now, written some breaking specs in active record, but the fix is eluding me.
You can also setup your own eager loading, effectively doing the same as an includes :
ActiveRecord::IdentityMap.use do properties = Property.search_by_keywords('brownstone').paginate(:conditions => ["(opt_out = false) AND (price >= ? AND price <= ?) AND (open_houses.start_time BETWEEN ? AND ?)", 50000, 350000, '2011-10-13 00:00:00', '2011-10-16 23:59:59'], :page => 1, :per_page => 25)
includes = OpenHouse.where(:property_id => properties).allend
Reply to this email directly or view it on GitHub: https://github.com/Casecommons/pg_search/issues/14#issuecomment-2411640
@tcocca I discovered that someone is maintaining a fork of pg_search for Rails 2.3 support.
https://github.com/bloomfire/pg_search
I can't vouch for the quality of their efforts, but it's worth a try if you think you'll be stuck on Rails 2.3 for a while.
I am also encountering this error on eager loading using includes... any solution for this problem?
I observe that the error for me is happening when count is called on the query.. else the query generated works fine...
No fix yet : https://github.com/rails/rails/issues/950 , aiming for rails 3.2
Using RoR 3.2.1, pg_search 0.4.1
"PG::Error: ERROR: column "pg_search_rank" does not exist ..." eager loading join query
No solutions for this???? work around on 3.2.1?? seems that solution around references are include in unreleased RoR 4.0 But we can do anythink in 3.2.x versions?
--- following the problem I found this results:
This works properly
my_full_text_search_scope("Pedro").includes(:addresses)
But this not
my_full_text_search_scope("Pedro.com").includes(:addresses)
Note the dot at search string. If a dot is included the query "pg_search_rank" does not exist apears. The dot is not involved on left side field condition, is affected on right value side condition. If full text search without dot in strings search working ok.
Perhaps a previous text dot deleting for search will be a solution, but I'm looking for emails.
I'm looking for more elegant solutions about it, but not way actually (RoR 3.2.1). I'm think about workaround, based on avoid RoR (dot) problem, some like this:
query = query.gsub(".", "' || chr(46) || '")
my_pg_full_text_search(query).includes(:foo, :bar)
But some sanitizing removes the hack. Please Nertzy can you tell us how override the code to avoid the sanitization for this hack. here? https://github.com/Casecommons/pg_search/blob/master/lib/pg_search/scope_options.rb#L29 Can you provide best way to do? Thks!
daniel2d2art : the fix is this : https://github.com/rails/rails/issues/950 no easy solution will work.
I got around it by doing an IdentityMap. See all the comments further up.