2013-06-21 4 views
0

자동차 부품의 소매 사이트 인 내 레일 프로젝트에서 tire와 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 

답변