         # File lib/ferret/index.rb, line 266
    266:     def add_document(doc, analyzer = nil)
    267:       @dir.synchrolock do
    268:         ensure_writer_open()
    269:         if doc.is_a?(String) or doc.is_a?(Array)
    270:           doc = {@default_input_field => doc}
    271:         end
    272:
    273:         # delete existing documents with the same key
    274:         if @key
    275:           if @key.is_a?(Array)
    276:             query = @key.inject(BooleanQuery.new()) do |bq, field|
    277:               bq.add_query(TermQuery.new(field, doc[field].to_s), :must)
    278:               bq
    279:             end
    280:             query_delete(query)
    281:           else
    282:             id = doc[@key].to_s
    283:             if id
    284:               ensure_writer_open()
    285:               @writer.delete(@key, id)
    286:               @writer.commit
    287:             end
    288:           end
    289:         end
    290:         ensure_writer_open()
    291:
    292:         if analyzer
    293:           old_analyzer = @writer.analyzer
    294:           @writer.analyzer = analyzer
    295:           @writer.add_document(doc)
    296:           @writer.analyzer = old_analyzer
    297:         else
    298:           @writer.add_document(doc)
    299:         end
    300:
    301:         flush() if @auto_flush
    302:       end
    303:     end