Casecommons / pg_search

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
http://www.casebook.net
MIT License
1.3k stars 369 forks source link

Add ranking order to set higher priorities over rank #513

Open keymastervn opened 1 year ago

keymastervn commented 1 year ago

This PR changes how the gem is ranked by default pg_search.rank DESC, followed up by order_within_rank as the tiebreaker. Now, it supports ordering by other fields BEFORE pg_search.rank

In my case, ranking is not the most important to be considered first. For instance, we have options to sort by relevance order (1) AND posted date order (2). The requirement for (2) will respect the posted date order before another matching, similar to partitioning by date and apply ranking inside the partitions.

This option ranking_order somehow is opposite to order_within_rank, please suggest me a proper name.

futuretap commented 1 year ago

I need to order using a custom distance function, so this unfortunately isn't flexible enough. However, I found I can simply use reorder to get rid of the pg_search ranking order and use my own order.

keymastervn commented 1 year ago

@futuretap I am still unclear on what your use case is, can you please provide me more context, perhaps a SQL query? The option ranking order may come with a hash or string. If your distance is the below distance_meters

SELECT earth_distance(
  ll_to_earth(37.77492, -122.41942),
  ll_to_earth(40.71278, -74.00595)
) AS distance_meters

Then the option is simply ranking_order: "distance_meters DESC".

In my case, I want to count the tsvector weight as well, so your change reorder must have a way to count #{rank_table_alias}.rank as well if I don't get wrong.

futuretap commented 1 year ago

My order is using PostGIS: location <-> 'SRID=4326;POINT(11.0 48.0)'::Geometry and I don't want to use the search rank at all.