gpt4 book ai didi

ruby-on-rails - pg_search : ordering of "multisearchable" results

转载 作者:行者123 更新时间:2023-12-04 19:02:57 24 4
gpt4 key购买 nike

我将 Rails 4.2.4 与 pg_search 1.0.5 一起使用。

class Advert < ActiveRecord::Base

include PgSearch
multisearchable :against => [:title, :body]

end

我想按 :created_at 对 pg_search 结果进行排序我的广告记录的日期。从逻辑上讲,在我看来,以下内容可以工作( :asc ):
> @pgs = PgSearch.multisearch('55948189').order(created_at: :asc)

PgSearch::Document Load (136.1ms) SELECT "pg_search_documents".* FROM "pg_search_documents" INNER JOIN (SELECT "pg_search_documents"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))), (to_tsquery('simple', ''' ' || '55948189' || ' ''')), 0)) AS rank FROM "pg_search_documents" WHERE (((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || '55948189' || ' '''))))) AS pg_search_ce9b9dd18c5c0023f2116f ON "pg_search_documents"."id" = pg_search_ce9b9dd18c5c0023f2116f.pg_search_id ORDER BY pg_search_ce9b9dd18c5c0023f2116f.rank DESC, "pg_search_documents"."id" ASC, "pg_search_documents"."created_at" ASC
=> #<ActiveRecord::Relation [

#<PgSearch::Document id: 3148, content: "Aleksandra | Tallinn | 55948189 Simpatichnaja i se...", searchable_id: 3148, searchable_type: "Advert", created_at: "2015-10-01 11:44:06", updated_at: "2015-10-01 11:44:30">,
#<PgSearch::Document id: 3275, content: "Aleksandra | Tallinn | 55948189 Simpatichnaja i se...", searchable_id: 3275, searchable_type: "Advert", created_at: "2015-10-02 11:05:49", updated_at: "2015-10-02 11:28:48">]>

但是相反的顺序( :desc )会产生相同的结果:
> @pgs = PgSearch.multisearch('55948189').order(created_at: :desc)

PgSearch::Document Load (108.4ms) SELECT "pg_search_documents".* FROM "pg_search_documents" INNER JOIN (SELECT "pg_search_documents"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))), (to_tsquery('simple', ''' ' || '55948189' || ' ''')), 0)) AS rank FROM "pg_search_documents" WHERE (((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || '55948189' || ' '''))))) AS pg_search_ce9b9dd18c5c0023f2116f ON "pg_search_documents"."id" = pg_search_ce9b9dd18c5c0023f2116f.pg_search_id ORDER BY pg_search_ce9b9dd18c5c0023f2116f.rank DESC, "pg_search_documents"."id" ASC, "pg_search_documents"."created_at" DESC
=> #<ActiveRecord::Relation [

#<PgSearch::Document id: 3148, content: "Aleksandra | Tallinn | 55948189 Simpatichnaja i se...", searchable_id: 3148, searchable_type: "Advert", created_at: "2015-10-01 11:44:06", updated_at: "2015-10-01 11:44:30">,
#<PgSearch::Document id: 3275, content: "Aleksandra | Tallinn | 55948189 Simpatichnaja i se...", searchable_id: 3275, searchable_type: "Advert", created_at: "2015-10-02 11:05:49", updated_at: "2015-10-02 11:28:48">]>

谁能解释我如何通过 :created_at 对我的搜索结果进行排序日期?

更新 1 : 是否可以通过 pg_search固定结果的顺序没有任何方法可以通过“created_at”进行订购?

我现在正在更仔细地阅读文档并发现以下语句:“默认情况下,pg_search 根据可搜索文本和查询之间的 :tsearch 相似度对结果进行排名。要使用不同的排名算法,您可以将 :ranked_by 选项传递给 pg_search_scope .” (见 https://github.com/Casecommons/pg_search)

最佳答案

有一个简单的答案,但它不适用于“multisearchable”。

首先,我需要从“multisearchable”(适用于多个模型)更改为“pg_search_scope”(仅在单个模型中搜索)。

其次,我需要使用 :order_within_rank .由于结果都是搜索词的100%匹配,根据pg_search它们的排名相同。 . :order_within_rank作为决胜局给出次要的排名。

class Advert < ActiveRecord::Base

include PgSearch
pg_search_scope :search_by_full_advert,
:against => [:title, :body],
:order_within_rank => "adverts.created_at DESC"

end

关于ruby-on-rails - pg_search : ordering of "multisearchable" results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32907692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com