title: Accessing custom Lucene attribute from DirectoryReader
body: <p>I added a custom attribute to my Lucene pipeline like described <a href="https://lucene.apache.org/core/4_0_0/core/org/apache/lucene/analysis/package-summary.html" rel="nofollow">here</a> (in the "Adding a custom Attribute" section).</p>
  
  <p>Now, after I built my index (by adding all the documents via <code>IndexWriter</code>) I want to be able to assess this attribute when reading the index directory. How do I do this? </p>
  
  <p>What I'm doing now is the following: </p>
  
  <pre class="lang-java prettyprint-override"><code>DirectoryReader reader = DirectoryReader.open(index);
  TermsEnum iterator = null;
  for (int i = 0; i &lt; r.maxDoc(); i++) {
      Terms terms = r.getTermVector(i, "content");
      iterator = terms.iterator(iterator);
      AttributeSource attributes = iterator.attributes();
      SentenceAttribute sentence = attributes.addAttribute(SentenceAttribute.class);
  
      while (true) {
          BytesRef term = iterator.next();
          if (term == null) {
              break;
          }
  
          System.out.println(term.utf8ToString());
          System.out.println(sentence.getStringSentenceId());
      }
  }
  </code></pre>
  
  <p>It doesn't seem to work: I get the same <code>sentenceId</code> all the time.</p>
  
  <p>I use Lucene 4.9.1.</p>