Uniq is not uniquing #85

Closed
nathany opened this Issue Jan 24, 2013 · 4 comments

Projects

None yet

2 participants

@nathany
nathany commented Jan 24, 2013

If I run a search with uniq, eg. prefix_search("fuel").uniq.limit(5), the query contains SELECT DISTINCT but it still returns duplicate results.

For now I can use Ruby's standard uniq afterwards, but this will give me less results than I wanted (via the limit).

Is there any solution for this, or is it a limit of PostgreSQL's full text search?

Collaborator
nertzy commented Jan 25, 2013

I believe this one is also related to #60. The problem is probably that pg_search is adding a big expression to the select part of the query called pg_search_rank, and so the distinct is applying to that part as well, which means every record that has a slightly different rank value calculated is considered distinct.

I hope to get some time to dig into this soon.

nathany commented Aug 12, 2013

@nertzy That makes sense. I suppose the only way around that would be to move the ts_rank code directly into an order by scope and not select it at all?

Update: I guess that won't even work -- "PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list"

Collaborator
nertzy commented Aug 23, 2013

If you want to remove the rank from the order expression, you could chain ActiveRecord::QueryMethods#reorder onto the end.

I'm not sure, but you might then need to add another order clause after that.

prefix_search("fuel").uniq.limit(5).reorder
nathany commented Aug 23, 2013

Sounds good. Thanks.

@nathany nathany closed this Aug 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment