2014-07-25 14 views
3

나는 잘 작동하는 레일 4 앱에 검색 엔진을 만들었지 만, 작동하기 위해 asciifolding 필터를 얻는 데 매우 힘든 시간을 보내고 있습니다. 내 모델에는 철자가 정확하지 않으면 오지 않는 악센트가 많은 용어가 있습니다. 예를 들어, "Rodriguez"에 대한 검색에서 "Rodríguez"로 검색 결과를 표시하고 싶습니다. 많은 다른 예제를 따르려고했지만 다음 코드로 데이터베이스를 리셋 할 때 어떤 이유로 검색이 작동하지 않습니다 (오류가 발생하지 않지만 쿼리에 관계없이 아무 것도 표시되지 않습니다).레일 4에서 탄성 압축 및 타이어를 사용하여 조립하기

class Product < ActiveRecord::Base 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 

    settings :analysis => { 
     :analyzer => { 
      :default => { 
      :tokenizer => "standard", 
      :filter => ["standard", "asciifolding"] 
      } 
     } 
    } do 

     mapping do 
      indexes :id, type: 'integer', index: :not_analyzed 
      indexes :name, boost: 5, analyzer: 'default' 
      indexes :website, index: :not_analyzed 
      indexes :price, type: 'integer', index: :not_analyzed 
      indexes :artist, boost: 3, analyzer: 'default' 
      indexes :company, boost: 4, analyzer: 'default' 
      indexes :date, type: 'date', index: :not_analyzed 
     end 
    end 

    def self.search(params) 
     tire.search(page: params[:page], per_page: 12) do 
     query { string params[:query], default_operator: "AND" } if params[:query].present? 
     end 
    end 

테스트하기 전에, 내가 함께 데이터베이스를 삭제 : 여기

내 모델입니다 그리고

rake db:setup 

나는 실행 내가 여러 가지 자원을 살펴 보았다

rake environment tire:import CLASS=Product FORCE=true 

elasticsearch와 타이어 문서를 포함하여, 그러나 무엇 이건 (나는 어리석은 실수를 예상하고있다) simp 그렇지 않을 것이다. 작은 메모, 내 데이터베이스를 채우기 위해 내가 csv 파일을 가져 왔지만, 왜 아무것도 영향을 미치지 않을거야, 특히이 양식에서 검색에 올라오고 있다고 생각하면 (괜찮아요 작동하지 않습니다 악센트 문제를 삭제할 때) 설정 부분과 매핑 블록 및 검색 방법을 가지고). 어떤 종류의 타이어를 불러야합니까? 인도하고 수입합니까? 어떤 도움을 주셔서 감사합니다! , 지금 악센트 불가지론를 검색 할 수 있습니다, 기본 필드를 식별함으로써

def self.search(params) 
      tire.search(page: params[:page], per_page: 12) do 
      query { string params[:query], analyzer: :search_analyzer, :default_field => 'name' } if params[:query].present? 
      end 
     end 

: UPDATE

그래서 나는 새로운 하나를 문제를 해결하지만 제기 된 검색 쿼리 편집을했다 하지만 지금은 검색이 이름만으로 제한되며 이전에 작동했던 다른 색인 된 속성에 대한 결과를받을 수 없습니다. 누구든지 여러 기본 필드를 설정하는 방법을 알고 있습니까?

답변

0

프로덕션 환경에서 사용하는 예입니다. 그것은 아니 해결책 귀하의 문제에 있지만 땅에 당신을 얻을 것이다. 내 코드는 icu_folding, shingle_filter, multi_fieldTireICU plugin을 사용하고 있습니다. 또한 Tire은 2013 년 9 월 이후 retired이며 새 프로젝트 인 경우 elasticsearch-ruby 또는 elasticsearch-rails을 사용해야합니다. 이 코드를 사용하면 RodRodríguez 인 사례를 자동 완성 할 수 있습니다.

class Profile 
    # ... 
    include Tire::Model::Persistence 
    # I'm also using ES as persistance storage. 
    include Tire::Model::DynamicPersistence 

    property :title, analyzer: 'snowball', type: :multi_field, 
    fields: { 
     title: { 
     type: 'string', 
     boost: 20.0, 
     search_analyzer: "autocomplete_search_analyzer", 
     index_analyzer: "autocomplete_indexer_analyzer" 
     }, 
     completed: { 
     type: 'string', 
     boost: 15.0, 
     search_analyzer: "autocomplete_search_analyzer", 
     index_analyzer: "autocomplete_indexer_analyzer" 
    } 
    } 

    CONFIGURATION = { 
    settings: { 
     analysis: { 
     analyzer: { 
      shingle_analyzer: { 
      tokenizer: "keyword", 
      filter: ["icu_folding", "lowercase", "shingle_filter"] 
      }, 
      sx_index_analyzer: { 
      tokenizer: "standard", 
      filter: ["icu_folding", "lowercase"] 
      }, 
      sx_search_analyzer: { 
      tokenizer: "standard", 
      filter: ["icu_folding", "lowercase"] 
      } 
     }, 
     filter: { 
      shingle_filter: { 
      type: "shingle", 
      min_shingle_size: 2, 
      max_shingle_size: 5 
      } 
     } 

     } 
    }, 

    mappings: { 
     profile: { 
     "_all" => { 
      enabled: true, 
      index_analyzer: "sx_index_analyzer", 
      search_analyzer: "sx_search_analyzer" 
     }, 

     properties: { 
     title: { 
     type: "multi_field", 
     fields: { 
      title: { 
      type: "string", 
      store: "yes", 
      boost: 20.0 
      #index_analyzer: "sx_index_analyzer", 
      #search_analyzer: "sx_search_analyzer" 
      }, 
      sortable: { 
      type: "string", 
      index: "analyzed" 
      }, 
      autocomplete: { 
      type: "string", 
      index_analyzer: "shingle_analyzer", 
      boost: 15.0 
      }, 
     } 
     } 
     } 
     } 
    } 
    } 

    def self.rebuild_index 
    Tire.index Profile.index_name do 
     delete if Profile.index.exists? 
     create Profile::CONFIGURATION 
    end 
    end 
end 

희망 하시겠습니까?

+0

감사합니다. 지금 테스트 할 시간이 없지만 가능한 경우 어떻게 작동하는지 알려 드리겠습니다. –

+0

올바른 대답이라고 판단되면 적절하다고 표시하십시오. 감사. ;) –