title: can't delete document with lucene IndexWriter.deleteDocuments(term)
body: <p>Have been struggling for this two days now, just can't delete the document  with <code>indexWriter.deleteDocuments(term)</code></p>
  
  <p>Here I will put the code which will do a test, hopefully someone could point out what I have done wrong, things that have been tried:</p>
  
  <ol>
  <li>Updating the lucene version from <code>2.x</code> to <code>5.x</code></li>
  <li>Using <code>indexWriter.deleteDocuments()</code> instead of <code>indexReader.deleteDocuments()</code></li>
  <li>Tring the <code>indexOption</code> configured as <code>NONE</code> or <code>DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS</code></li>
  </ol>
  
  <p>here the code:</p>
  
  <pre><code>import org.apache.lucene.analysis.core.SimpleAnalyzer;
  import org.apache.lucene.document.Document;
  import org.apache.lucene.document.Field;
  import org.apache.lucene.document.FieldType;
  import org.apache.lucene.index.*;
  import org.apache.lucene.queryparser.classic.ParseException;
  import org.apache.lucene.queryparser.classic.QueryParser;
  import org.apache.lucene.search.IndexSearcher;
  import org.apache.lucene.search.Query;
  import org.apache.lucene.search.ScoreDoc;
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.FSDirectory;
  
  import java.io.IOException;
  import java.nio.file.Paths;
  
  public class TestSearch {
      static SimpleAnalyzer analyzer = new SimpleAnalyzer();
  
      public static void main(String[] argvs) throws IOException, ParseException {
          generateIndex("5836962b0293a47b09d345f1");
          query("5836962b0293a47b09d345f1");
          delete("5836962b0293a47b09d345f1");
          query("5836962b0293a47b09d345f1");
  
      }
  
      public static void generateIndex(String id) throws IOException {
          Directory directory = FSDirectory.open(Paths.get("/tmp/test/lucene"));
          IndexWriterConfig config = new IndexWriterConfig(analyzer);
          IndexWriter iwriter = new IndexWriter(directory, config);
          FieldType fieldType = new FieldType();
          fieldType.setStored(true);
          fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
          Field idField = new Field("_id", id, fieldType);
          Document doc = new Document();
          doc.add(idField);
          iwriter.addDocument(doc);
          iwriter.close();
  
      }
  
      public static void query(String id) throws ParseException, IOException {
          Query query = new QueryParser("_id", analyzer).parse(id);
          Directory directory = FSDirectory.open(Paths.get("/tmp/test/lucene"));
          IndexReader ireader  = DirectoryReader.open(directory);
          IndexSearcher isearcher = new IndexSearcher(ireader);
          ScoreDoc[] scoreDoc = isearcher.search(query, 100).scoreDocs;
          for(ScoreDoc scdoc: scoreDoc){
              Document doc = isearcher.doc(scdoc.doc);
              System.out.println(doc.get("_id"));
          }
      }
  
      public static void delete(String id){
          try {
               Directory directory = FSDirectory.open(Paths.get("/tmp/test/lucene"));
              IndexWriterConfig config = new IndexWriterConfig(analyzer);
              IndexWriter iwriter = new IndexWriter(directory, config);
              Term term = new Term("_id", id);
              iwriter.deleteDocuments(term);
              iwriter.commit();
              iwriter.close();
          }catch (IOException e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>First <code>generateIndex()</code> will generate a index in <code>/tmp/test/lucene</code>, and  <code>query()</code> will show that <code>id</code> will be successfully queried, then <code>delete()</code> was hopefully to deleting the document, but <code>query()</code> again will prove that the deleting action failed.</p>
  
  <p>Here is the pom dependency in case someone may need for a test</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.apache.lucene&lt;/groupId&gt;
          &lt;artifactId&gt;lucene-core&lt;/artifactId&gt;
          &lt;version&gt;5.5.4&lt;/version&gt;
          &lt;type&gt;jar&lt;/type&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.lucene&lt;/groupId&gt;
          &lt;artifactId&gt;lucene-analyzers-common&lt;/artifactId&gt;
          &lt;version&gt;5.5.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.lucene&lt;/groupId&gt;
          &lt;artifactId&gt;lucene-queryparser&lt;/artifactId&gt;
          &lt;version&gt;5.5.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.lucene&lt;/groupId&gt;
          &lt;artifactId&gt;lucene-analyzers-smartcn&lt;/artifactId&gt;
          &lt;version&gt;5.5.4&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>desperated for an answer.</p>