2013-01-18 10 views
6

मेरे पास एक रेल एप्लिकेशन है जो अब मॉडल पर खोज करने के लिए लोचदार खोज और टायर मणि के साथ स्थापित है और मैं सोच रहा था कि मुझे फ़ज़ी स्ट्रिंग करने के लिए अपना एप्लिकेशन कैसे सेट अप करना चाहिए मॉडल में कुछ इंडेक्स पर मिलान। मेरे पास शीर्षक, विवरण इत्यादि जैसी चीजों पर इंडेक्स पर सेट अप किया गया है, लेकिन मैं उनमें से कुछ पर फ़ज़ी स्ट्रिंग मिलान करना चाहता हूं और मुझे यकीन नहीं है कि यह कहां करें। यदि आप टिप्पणी करना चाहते हैं तो मैं नीचे अपना कोड शामिल करूंगा! धन्यवाद!रेल (टायर) और लोचदार खोज के साथ फ़ज़ी स्ट्रिंग मिलान

नियंत्रक में:

def search 
     @resource = Resource.search(params[:q], :page => (params[:page] || 1), 
           :per_page =>15, load: true) 
    end 

मॉडल में:

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

    belongs_to :user 
    has_many :resource_views, :class_name => 'UserResourceView' 

    has_reputation :votes, source: :user, aggregated_by: :sum 

    attr_accessible :title, :description, :link, :tag_list, :user_id, :youtubeID 
    acts_as_taggable 

    mapping do 
     indexes :id, :index => :not_analyzed 
     indexes :title, :analyzer => 'snowball', :boost => 40 
     indexes :tag_list, :analyzer => 'snowball', :boost => 8 
     indexes :description, :analyzer => 'snowball', :boost => 2 
     indexes :user_id, :analyzer => 'snowball' 
    end 
end 

उत्तर

2

अन्य उत्पन्न सुविधाओं, आदि प्राप्त करने के लिए (मेरे उदाहरण देखें इस उदाहरण भी उपयोग करता है Mongoid & कस्टम विश्लेषक बनाने का प्रयास करें अनुलग्नक, अगर आपको इसकी आवश्यकता नहीं है तो इसे न देखें):

class Document 
     include Mongoid::Document 
     include Mongoid::Timestamps 
     include Tire::Model::Search 
     include Tire::Model::Callbacks 

     field :filename, type: String 
     field :md5, type: String 
     field :tags, type: String 
     field :size, type: String 

     index({md5: 1}, {unique: true}) 
     validates_uniqueness_of :md5 


     DEFAULT_PAGE_SIZE = 10 

     settings :analysis => { 
      :filter => { 
       :ngram_filter => { 
        :type => "edgeNGram", 
        :min_gram => 2, 
        :max_gram => 12 
       }, 
       :custom_word_delimiter => { 
        :type => "word_delimiter", 
        :preserve_original => "true", 
        :catenate_all => "true", 
       } 
      }, :analyzer => { 
       :index_ngram_analyzer => { 
        :type => "custom", 
        :tokenizer => "standard", 
        :filter => ["lowercase", "ngram_filter", "asciifolding", "custom_word_delimiter"] 
       }, 
       :search_ngram_analyzer => { 
        :type => "custom", 
        :tokenizer => "standard", 
        :filter => ["standard", "lowercase", "ngram_filter", "custom_word_delimiter"] 
       }, 
       :suggestions => { 
        :tokenizer => "standard", 
        :filter => ["suggestions_shingle"] 
       } 
      } 
     } do 
     mapping { 
      indexes :id, index: :not_analyzed 
      indexes :filename, :type => 'string', :store => 'yes', :boost => 100, :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer 
      indexes :tags, :type => 'string', :store => 'yes', :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer 
      indexes :attachment, :type => 'attachment', 
        :fields => { 
         :content_type => {:store => 'yes'}, 
         :author => {:store => 'yes', :analyzer => 'keyword'}, 
         :title => {:store => 'yes'}, 
         :attachment => {:term_vector => 'with_positions_offsets', :boost => 90, :store => 'yes', :search_analyzer => :search_ngram_analyzer, :index_analyzer => :index_ngram_analyzer}, 
         :date => {:store => 'yes'} 
        } 
     } 
     end 


     def to_indexed_json 
     self.to_json(:methods => [:attachment]) 
     end 

     def attachment   
      path_to_file = "#{Rails.application.config.document_library}#{path}/#{filename}" 
      Base64.encode64(open(path_to_file) { |file| file.read }) 
     end 

     def self.search(query, options) 
     tire.search do 
      query { string "#{query}", :default_operator => :AND, :default_field => 'attachment', :fields => ['filename', 'attachment', 'tags'] } 
      highlight :attachment 
      page = (options[:page] || 1).to_i 
      search_size = options[:per_page] || DEFAULT_PAGE_SIZE 
      from (page -1) * search_size 
      size search_size 
      sort { by :_score, :desc } 
      if (options[:facet]) 
      filter :terms, :tags => [options[:facet]] 
      facet 'global-tags', :global => true do 
       terms :tags 
      end 
      facet 'current-tags' do 
       terms :tags 
      end 
      end 
     end 
     end 
    end 

उम्मीद है कि यह मदद करता है,

+0

सहायक, लेकिन लोचदार खोज बहुत ही बोझिल हो गया ताकि हम postgresql पर स्विच कर सकें। हालांकि धन्यवाद! – noname

+0

बहुत उपयोगी ... धैर्य के साथ, आपका उदाहरण एक आकर्षण की तरह काम करता है :) – Rinku

+1

'store => 'yes' पैरामीटर का क्या प्रभाव है? – phillbaker

संबंधित मुद्दे