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