respetar el orden de los artículos

siempre ordenamos primero por número de orden y fecha de creación,
siempre decrecientes.  esto permite que les usuaries prioricen contenido
usando las herramientas de reordenamiento.

pg_search no soporta esto, siempre ordena por cuánto corresponde el
resultado con la búsqueda, así que lo emparchamos para que respete el
orden que necesitamos.

el reporte de error relacionado es este:
https://github.com/Casecommons/pg_search/issues/467
This commit is contained in:
f 2021-05-06 19:07:11 -03:00
parent f9dca42f95
commit d61d1cad56
5 changed files with 29 additions and 1 deletions

View file

@ -26,6 +26,9 @@ class IndexedPost < ApplicationRecord
}
}
# Trae los IndexedPost en el orden en que van a terminar en el sitio.
default_scope lambda { order(order: :desc, created_at: :desc) }
belongs_to :site
# Convertir locale a direccionario de PG

View file

@ -18,6 +18,8 @@ class Post
indexed_post.title = title.value
indexed_post.front_matter = indexable_front_matter
indexed_post.content = indexable_content
indexed_post.created_at = date.value
indexed_post.order = attribute?(:order) ? order.value : 0
end
end

View file

@ -66,3 +66,16 @@ module Jekyll
end
end
end
# No aplicar el orden por ranking para poder obtener los artículos en el
# orden que tendrían en el sitio final.
module PgSearch
ScopeOptions.class_eval do
def apply(scope)
scope = include_table_aliasing_for_rank(scope)
rank_table_alias = scope.pg_search_rank_table_alias(include_counter: true)
scope.joins(rank_join(rank_table_alias))
end
end
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
# Agrega el orden y fecha del post en el post indexado, de forma que los
# resultados se puedan obtener en el mismo orden.
class AddOrderToIndexedPosts < ActiveRecord::Migration[6.1]
def change
add_column :indexed_posts, :order, :integer, default: 0
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_05_04_224343) do
ActiveRecord::Schema.define(version: 2021_05_06_212356) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
@ -229,6 +229,7 @@ ActiveRecord::Schema.define(version: 2021_05_04_224343) do
t.jsonb "front_matter", default: "{}"
t.string "content", default: ""
t.tsvector "indexed_content"
t.integer "order", default: 0
t.index ["front_matter"], name: "index_indexed_posts_on_front_matter", using: :gin
t.index ["indexed_content"], name: "index_indexed_posts_on_indexed_content", using: :gin
t.index ["layout"], name: "index_indexed_posts_on_layout"