pg_search_scope:链接范围似乎是不可能的
pg_search_scope: chaining scopes seems impossible
我有一个用于搜索 "documents" 的搜索表单,其中包含一小部分搜索条件,包括 "entire_text"、"keywords" 和 "description"。
我正在使用 pg_search_scope
,但我有 2 个不同的范围。
这是我的 document.rb:
pg_search_scope :search_entire_text,
:against => :entire_text,
:using => {
:tsearch => {
:prefix => true,
:dictionary => "french"
}
}
pg_search_scope :search_keywords,
:associated_against => {
:keywords => [:keyword]
},
:using => {
:tsearch => {
:any_word => true
}
}
每个单独工作正常。但我做不到:
@resultats = Document.search_keywords(params[:ch_document][:keywords]).search_entire_text(params[:ch_document][:entire_text])
有什么办法可以解决这个问题吗?
谢谢
我从未使用过 pg_search_scope
,但看起来您确实无法组合两个 pg_search_scope
。
你可以做的是将 :search_entire_text
与 pg_search_scope
一起使用,并在 Document.where([1,2,3])
中使用生成的 ID,这样你就可以对剩余的使用标准 rails 范围关键字搜索。
示例:
# If pluck doesn't work you can also use map(&:id)
txt_res_ids = Document.search_entire_text(params[:ch_document][:entire_text]).pluck(:id)
final_results = Document.where(id: txt_res_ids).some_keyword_scope.all
有效。这是完整的代码......如果这可以帮助某人:
Acte.rb
(我没有翻译成英文,注释的解释对应上面的问题)
pg_search_scope :cherche_texte_complet, #i.e. find entire text
:against => :texte_complet,
:using => {
:tsearch => {
:prefix => true,
:dictionary => "french"
}
}
pg_search_scope :cherche_mots_clefs, #find keywords
:associated_against => {
:motclefs => [:motcle]
},
:using => {
:tsearch => {
:any_word => true
}
}
def self.cherche_date(debut, fin) #find date between
where("acte_date BETWEEN :date_debut AND :date_fin", {date_debut: debut, date_fin: fin})
end
def self.cherche_mots(mots)
if mots.present? #the if ... else is necessary, see controller.rb
cherche_mots_clefs(mots)
else
order("id DESC")
end
end
def self.ids_texte_compl(ids)
if ids.any?
where("id = any (array #{ids})")
else
where("id IS NOT NULL")
end
end
和actes_controller.rb
ids = Acte.cherche_texte_complet(params[:ch_acte][:texte_complet]).pluck(:id)
@resultats = Acte.cherche_date(params[:ch_acte][:date_debut],params[:ch_acte][:date_fin])
.ids_texte_compl(ids)
.cherche_mots(params[:ch_acte][:mots])
谢谢!
链接至少在 pg_search 2.3.2 中有效
SomeModel.pg_search_based_scope("abc").pg_search_based_scope("xyz")
我有一个用于搜索 "documents" 的搜索表单,其中包含一小部分搜索条件,包括 "entire_text"、"keywords" 和 "description"。
我正在使用 pg_search_scope
,但我有 2 个不同的范围。
这是我的 document.rb:
pg_search_scope :search_entire_text,
:against => :entire_text,
:using => {
:tsearch => {
:prefix => true,
:dictionary => "french"
}
}
pg_search_scope :search_keywords,
:associated_against => {
:keywords => [:keyword]
},
:using => {
:tsearch => {
:any_word => true
}
}
每个单独工作正常。但我做不到:
@resultats = Document.search_keywords(params[:ch_document][:keywords]).search_entire_text(params[:ch_document][:entire_text])
有什么办法可以解决这个问题吗?
谢谢
我从未使用过 pg_search_scope
,但看起来您确实无法组合两个 pg_search_scope
。
你可以做的是将 :search_entire_text
与 pg_search_scope
一起使用,并在 Document.where([1,2,3])
中使用生成的 ID,这样你就可以对剩余的使用标准 rails 范围关键字搜索。
示例:
# If pluck doesn't work you can also use map(&:id)
txt_res_ids = Document.search_entire_text(params[:ch_document][:entire_text]).pluck(:id)
final_results = Document.where(id: txt_res_ids).some_keyword_scope.all
有效。这是完整的代码......如果这可以帮助某人:
Acte.rb
(我没有翻译成英文,注释的解释对应上面的问题)
pg_search_scope :cherche_texte_complet, #i.e. find entire text
:against => :texte_complet,
:using => {
:tsearch => {
:prefix => true,
:dictionary => "french"
}
}
pg_search_scope :cherche_mots_clefs, #find keywords
:associated_against => {
:motclefs => [:motcle]
},
:using => {
:tsearch => {
:any_word => true
}
}
def self.cherche_date(debut, fin) #find date between
where("acte_date BETWEEN :date_debut AND :date_fin", {date_debut: debut, date_fin: fin})
end
def self.cherche_mots(mots)
if mots.present? #the if ... else is necessary, see controller.rb
cherche_mots_clefs(mots)
else
order("id DESC")
end
end
def self.ids_texte_compl(ids)
if ids.any?
where("id = any (array #{ids})")
else
where("id IS NOT NULL")
end
end
和actes_controller.rb
ids = Acte.cherche_texte_complet(params[:ch_acte][:texte_complet]).pluck(:id)
@resultats = Acte.cherche_date(params[:ch_acte][:date_debut],params[:ch_acte][:date_fin])
.ids_texte_compl(ids)
.cherche_mots(params[:ch_acte][:mots])
谢谢!
链接至少在 pg_search 2.3.2 中有效
SomeModel.pg_search_based_scope("abc").pg_search_based_scope("xyz")