def add_document(doc, analyzer = nil)
      @dir.synchronize do
        ensure_writer_open()
        if doc.is_a?(String) or doc.is_a?(Array)
          doc = {@default_input_field => doc}
        end

        # delete existing documents with the same key
        if @key
          if @key.is_a?(Array)
            query = @key.inject(BooleanQuery.new()) do |bq, field|
              bq.add_query(TermQuery.new(field, doc[field].to_s), :must)
              bq
            end
            query_delete(query)
          else
            id = doc[@key].to_s
            if id
              @writer.delete(@key, id)
            end
          end
        end
        ensure_writer_open()

        if analyzer
          old_analyzer = @writer.analyzer
          @writer.analyzer = analyzer
          @writer.add_document(doc)
          @writer.analyzer = old_analyzer
        else
          @writer.add_document(doc)
        end

        flush() if @auto_flush
      end
    end
def add_document(doc, analyzer = nil)
      @dir.synchronize do
        ensure_writer_open()
        if doc.is_a?(String) or doc.is_a?(Array)
          doc = {@default_input_field => doc}
        end

        # delete existing documents with the same key
        if @key
          if @key.is_a?(Array)
            query = @key.inject(BooleanQuery.new()) do |bq, field|
              bq.add_query(TermQuery.new(field, doc[field].to_s), :must)
              bq
            end
            query_delete(query)
          else
            id = doc[@key].to_s
            if id
              @writer.delete(@key, id)
            end
          end
        end
        ensure_writer_open()

        if analyzer
          old_analyzer = @writer.analyzer
          @writer.analyzer = analyzer
          @writer.add_document(doc)
          @writer.analyzer = old_analyzer
        else
          @writer.add_document(doc)
        end

        flush() if @auto_flush
      end
    end
Adds a document to this index, using the provided analyzer instead of
 the local analyzer if provided.  If the document contains more than
 IndexWriter::MAX_FIELD_LENGTH terms for a given field, the remainder are
 discarded.

 There are three ways to add a document to the index.
 To add a document you can simply add a string or an array of strings.
 This will store all the strings in the "" (ie empty string) field
 (unless you specify the default_field when you create the index).

   index << "This is a new document to be indexed"
   index << ["And here", "is another", "new document", "to be indexed"]

 But these are pretty simple documents. If this is all you want to index
 you could probably just use SimpleSearch. So let's give our documents
 some fields;

   index << {:title => "Programming Ruby", :content => "blah blah blah"}
   index << {:title => "Programming Ruby", :content => "yada yada yada"}

 Or if you are indexing data stored in a database, you'll probably want
 to store the id;

   index << {:id => row.id, :title => row.title, :date => row.date}

 See FieldInfos for more information on how to set field properties.