2013-06-21 56 views
0

我在我的rails項目中使用了輪胎和elasticsearch,這是一個汽車零部件的零售網站。 ES正在爲用於瀏覽零件目錄的分面搜索頁面提供動力。我的問題是:如何才能使過濾條件只返回完全匹配,同時使用查詢字符串搜索相同的字段,使用分析器只使用查詢字符串?ElasticSearch:將查詢字符串與術語過濾器結合起來

我希望這是有道理的。我將嘗試提供一個示例:

問題中的模型/索引稱爲零件。假設一部分映射名爲categoriessub_categories。如果用戶選擇制動類別和制動鉗載波子類別(創造長期的過濾器),我必須確保從制動卡鉗子類別部分是不是也回來了 - 這是一個獨立的子類別。但是,我確實希望用戶能夠在搜索字段中簡單地輸入「剎車」之類的內容(創建一個query_string),並從所有這些類別中的產品中獲取結果。

下面是從部分型號的相關代碼:

def to_indexed_json 
    fits = fitments.try(:map) do |fit| 
     { 
     make: fit.try(:make).try(:name), 
     make_id: fit.try(:make).try(:id), 
     model: fit.try(:model).try(:name), 
     model_id: fit.try(:model).try(:id), 
     year: fit.year, 
     sub_model: fit.sub_model 
     } 
    end 
    { 
     id: id, 
     name: name, 
     description: description, 
     fitments: fits, 
     categories: root_categories, 
     sub_categories: sub_categories, 
     price: price, 
     condition_id: condition_id, 
     country_of_origin: country_of_origin, 
     brand: brand, 
     oem: oem, 
     thumb_url: part_images.first.try(:image).try(:thumb).try(:url), 
     city: user.try(:city), 
     inventory: inventory, 
     part_number: part_number, 
     user: user.try(:public_name) 
    }.to_json 
    end 

    mapping do 
    indexes :id, type: 'integer' 
    indexes :name, analyzer: 'snowball', boost: 40 
    indexes :description, analyzer: 'snowball', boost: 12 

    indexes :price, type: "integer" 
    indexes :country_of_origin, index: :not_analyzed 
    indexes :condition_id, type: "integer" 
    indexes :brand, index: :not_analyzed 
    indexes :oem, type: "boolean" 
    indexes :city, index: :not_analyzed 
    indexes :inventory, type: "integer" 
    indexes :part_number, index: :not_analyzed 
    indexes :user, index: :not_analyzed 

    indexes :thumb_url, index: :not_analyzed 

    indexes :fitments do 
     indexes :make 
     indexes :make_id, type: "integer" #, index: 'not_analyzed' 
     indexes :model 
     indexes :model_id, type: "integer" #, index: 'not_analyzed' 
     indexes :year, type: "integer" 
     indexes :sub_model 
    end 

    indexes :categories do 
     indexes :name, index: :not_analyzed 
     indexes :id, type: "integer" 
    end 

    indexes :sub_categories do 
     indexes :name, index: :not_analyzed 
     indexes :id, type: "integer" 
    end 

    end 

def search(params={}) 

    query_filters = [] 

    tire.search(:page => params[:page], :per_page => 20) do 

    query_filters << { :term => { 'fitments.make_id' => params[:make] }} if params[:make].present? 
    query_filters << { :term => { 'fitments.model_id' => params[:model] }} if params[:model].present? 
    query_filters << { :term => { 'categories.name' => params[:category] }} if params[:category].present? 
    query_filters << { :term => { 'sub_categories.name' => params[:sub_category] }} if params[:sub_category].present? 
    query_filters << { :term => { 'city' => params[:city] }} if params[:city].present? 
    query_filters << { :term => { 'condition_id' => params[:condition] }} if params[:condition].present? 
    query_filters << { :term => { 'brand' => params[:brand] }} if params[:brand].present? 
    query_filters << { :term => { 'oem' => params[:oem] }} if params[:oem].present? 

    query do 
     filtered do 
     query { 
      if params[:query].present? 
      string params[:query] 
      else 
      all 
      end 
     } 
     filter :and, query_filters unless query_filters.empty? 
     end 
    end 

    facet("categories") { terms 'categories.name', size: 50 } unless params[:category].present? 
    facet("cities") { terms 'city', size: 50 } unless params[:city].present? 
    if params[:category].present? && !params[:sub_category].present? 
     facet("sub_categories") { terms 'sub_categories.name', size: 50 } 
    end 
    facet("condition_id") { terms 'condition_id', size: 50 } unless params[:condition].present? 
    facet("brand") { terms 'brand', size: 50 } unless params[:brand].present? 
    facet("oem") { terms 'oem', size: 2 } unless params[:oem].present? 

    size params[:size] if params[:size] 

    end 
end 

回答

相關問題