         # File lib/ferret/index.rb, line 251
    251:     def add_document(doc, analyzer = nil)
    252:       @dir.synchronize do
    253:         if doc.is_a?(String) or doc.is_a?(Array)
    254:           doc = {@default_input_field => doc}
    255:         end
    256:
    257:         # delete existing documents with the same key
    258:         if @key
    259:           if @key.is_a?(Array)
    260:             query = @key.inject(BooleanQuery.new()) do |bq, field|
    261:               bq.add_query(TermQuery.new(field, doc[field].to_s), :must)
    262:               bq
    263:             end
    264:             query_delete(query)
    265:           else
    266:             id = doc[@key].to_s
    267:             if id
    268:               ensure_writer_open()
    269:               @writer.delete(@key, id)
    270:               @writer.commit
    271:             end
    272:           end
    273:         end
    274:         ensure_writer_open()
    275:
    276:         old_analyzer = @writer.analyzer if analyzer
    277:         @writer.add_document(doc)
    278:         @writer.analyzer = old_analyzer if analyzer
    279:
    280:         flush() if @auto_flush
    281:       end
    282:     end