Dear list,

I 'm using a text input field with autocompletion . The suggestions come 
from a ferret index which is created by getting all the terms belonging to 
other indices. Here is the code:

class Suggestion

  attr_accessor :term

  def self.index(create)
    [Person, Project, Orgunit].each{|kl|
      terms = self.all_terms(kl)
      terms.each{|term|
        suggestion = Suggestion.new
        suggestion.term = term
        SUGGESTION_INDEX << suggestion.to_doc
      }
    }
    SUGGESTION_INDEX.optimize
  end

  def self.all_terms(klass)
    reader = Index::IndexReader.new(Object.const_get(klass.name.upcase + 
"_INDEX_DIR"))
    terms = []
    begin
    reader.field_names.each {|field_name|
    term_enum = reader.terms(field_name)
      begin
        term = term_enum.term()
        if !term.nil?
            if klass::SUGGESTIONABLE_FIELDS.include?(field_name)
              terms << term
            end
        end
      end while term_enum.next?
    }
    ensure
      reader.close
    end
    return terms
  end

  def to_doc
    doc = {}
    doc[:term] = self.term
    return doc
  end

end


It works very well except that the indexing process takes a long time. Does 
anybody knows if there's a better way to do this?
Is there another way to get all the terms of an index?

Thank you.

Johan

Analyst Programmer
Belgian Biodiversity Platform ( http://www.biodiversity.be)
Belgian Federal Science Policy Office (http://www.belspo.be )
Tel:+32 2 650 5751 Fax: +32 2 650 5124

_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to