title: Lucene bulk Udate issue
body: <p>Below code is only updating the last item sent in the documentFields. Because writer.Dispose() is called after the foreach loop. 
  Note: documentDto.isUpdate = true;</p>
  
  <pre><code>private void AddFieldsToDocuments(IEnumerable&lt;LuceneDocumentFieldsDto&gt; documentFields)
      {
  
          //  var analyzer = new CustomAnalyzer();
          // ReSharper disable once LocalVariableHidesMember
          using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
          {
              foreach (var documentDto in documentFields)
              {
                  // add data to lucene search index (replaces older entry if any)
                  var document = documentDto.document;
                  if (documentDto.isUpdate &amp;&amp; document == null)
                  {
                      document = GetDocument(documentDto.documentId.ToString(), documentDto.documentKeyName);
                  }
  
                  if (documentDto.fields?.Count &gt; 0)
                  {
                      foreach (var field in documentDto.fields)
                      {
                          document.RemoveField(field.Key);
                          // add lucene fields mapped to db fields
                          // add lucene fields mapped to db fields
                          document = GetDocumentToAdd(document, field);
                      }
                  }
  
                  if (document != null)
                  {
                      if (!documentDto.isUpdate)
                      {
                          document.Add(new Field(documentDto.documentKeyName, documentDto.documentId.ToString(),
                              Field.Store.YES, Field.Index.ANALYZED));
                          // add entry to index
                          writer.AddDocument(document);
                      }
                      else
                      {
                          var term = new Term(documentDto.documentKeyName, documentDto.documentId.ToString());
                          writer.UpdateDocument(term, document);
                      }
                  }
  
              }
  
              // close handles
              //   analyzer.Close();
              writer.Dispose();
          }
  
      }
  </code></pre>
  
  <p>To solve above problem, I moved writer inside the foreach loop and called writer.dispose() on every loop and problem solved with a cost. Now it is not bulk insert. Can anyone please point out what I am doing wrong so I can call writer.dispose() at the end to achieve bulk update.</p>
  
  <pre><code>      private void AddFieldsToDocuments(IEnumerable&lt;LuceneDocumentFieldsDto&gt; documentFields)
      {
          foreach (var documentDto in documentFields)
          {
              // ReSharper disable once LocalVariableHidesMember
              using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
              {
                  // add data to lucene search index (replaces older entry if any)
                  var document = documentDto.document;
                  if (documentDto.isUpdate &amp;&amp; document == null)
                  {
                      document = GetDocument(documentDto.documentId.ToString(), documentDto.documentKeyName);
                  }
  
                  if (documentDto.fields?.Count &gt; 0)
                  {
                      foreach (var field in documentDto.fields)
                      {
                          document.RemoveField(field.Key);
                          // add lucene fields mapped to db fields
                          // add lucene fields mapped to db fields
                          document = GetDocumentToAdd(document, field);
                      }
                  }
  
                  if (document != null)
                  {
                      if (!documentDto.isUpdate)
                      {
                          document.Add(new Field(documentDto.documentKeyName, documentDto.documentId.ToString(),
                              Field.Store.YES, Field.Index.ANALYZED));
                          // add entry to index
                          writer.AddDocument(document);
                      }
                      else
                      {
                          var term = new Term(documentDto.documentKeyName, documentDto.documentId.ToString());
                          writer.UpdateDocument(term, document);
                      }
                  }
                  writer.Dispose();
              }
          }
      }
  </code></pre>
  
  <p>For further detail, I am posting the code which is called from the above function:</p>
  
  <pre><code> protected Document GetDocument(string searchQuery, string searchField = "")
      {
          // set up lucene searcher
          // ReSharper disable once LocalVariableHidesMember
          using (var searcher = new IndexSearcher(directory, true))
          {
              //   var analyzer = new CustomAnalyzer();
              // search by single field
              if (!string.IsNullOrEmpty(searchField))
              {
                  var filters = new List&lt;LuceneFilterDto&gt;
                  {
                      new LuceneFilterDto
                      {
                          FieldName = searchField,
                          FieldValue = searchQuery,
                          Occur = Occur.MUST,
                          SearchType = LuceneSearchType.WildCardSearch
                      }
                  };
  
                  var luceneQueryFilters = new List&lt;LuceneQueryFiltersDto&gt; { new LuceneQueryFiltersDto
                  {
                      Condition = Occur.MUST,
                      Filters = filters
                  }
                  };
  
                  var query = GenerateSearchQuery(luceneQueryFilters, analyzer);
  
                  var hits = searcher.Search(query, int.MaxValue).ScoreDocs;
                  if (hits.Length &gt; 0)
                  {
                      var docs = hits.Select(elem =&gt; searcher.Doc(elem.Doc)).FirstOrDefault();
                      // analyzer.Close();
                      searcher.Dispose();
                      return docs;
                  }
              }
          }
  
          return null;
      }
  
  private Document GetDocumentToAdd(Document document, LuceneFieldDto field)
      {
          // add lucene fields mapped to db fields
          switch (field.FieldTypeId)
          {
              //case (int)Common.FieldType.Number:
              //    if (!string.IsNullOrEmpty(field.Value))
              //        document.Add(new NumericField(field.Key, Field.Store.YES, true).SetIntValue(Convert.ToInt32(field.Value)));
              //    else
              //        document.Add(new Field(field.Key, field.Value, Field.Store.YES, Field.Index.ANALYZED));
              //    break;
              // ReSharper disable once RedundantNameQualifier
              case (int)Common.FieldType.Date:
                  if (!string.IsNullOrEmpty(field.Value))
                  {
                      if (field.Value != Constants.InvalidDate)
                      {
                          var date = CommonMethods.GetDateTime(field.Value);
  
                          document.Add(new Field(field.Key,
                              DateTools.DateToString(date, DateTools.Resolution.MILLISECOND),
                              Field.Store.YES, Field.Index.NOT_ANALYZED));
                      }
                  }
                  else
                      document.Add(new Field(field.Key, field.Value, Field.Store.YES, Field.Index.ANALYZED));
                  break;
              default:
                  if (field.Value == null)
                  {
                      field.Value = string.Empty;
                  }
                  document.Add(new Field(field.Key, field.Value, Field.Store.YES, Field.Index.ANALYZED));
                  break;
          }
  
          return document;
      }        
  </code></pre>