簡體   English   中英

PG_Search和多個字段

[英]PG_Search and multiple fields

我正在使用pg-search搜索模型。 我遇到的問題是,如果我搜索一個字段或另一個字段中的關鍵字,但是如果我輸入的關鍵字我知道在2個字段中我正在使用它不會返回任何內容。

這是我的搜索

@providers = Provider.text_search(params[:query])

這是幫助者

def self.text_search(query)
  if query.present?
    where("address @@ :q or conditions @@ :q", q: "%#{query}%")
  else
    scoped
  end
end

總結一下...如果我為地址輸入2個關鍵字(即:郵政編碼和街道名稱)或2個條件關鍵字(即:全新),它可以工作,但如果我在搜索字段中輸入新的郵政編碼,則不會返回什么

這是查詢

Provider Load (1.3ms)  SELECT "providers".* FROM "providers"  WHERE (address @@ '%new rancho%' or conditions @@ '%new rancho%')

編輯我忘了發布pg_search_scope

pg_search_scope :search, against: [:address, :conditions], using: { tsearch: { dictionary: "english", prefix: true, any_word: true} }

在你的模型中嘗試這樣的東西

  include PgSearch

 pg_search_scope :search,  against: [:zipcode, :streetname], 
using: {tsearch: {prefix: true, any_word: true}}

  def self.text_search(query)
    if query.present?
      search(query)
    else
      scoped
    end
  end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM