/*
   *  listPostings displays the first n postings for a term in a
   *  field in an index (specified by reader).  Set n to MAX_VALUE
   *  to display all postings.
   */
  static void listPostings(IndexReader reader, String termString, String field, Integer n)
      throws IOException {

    System.out.println("\nPostings:  " + termString + " " + field);

    /*
     *  Prepare to access the index.
     */
    BytesRef termBytes = new BytesRef(termString);
    Term term = new Term(field, termBytes);
    Bits liveDocs = MultiFields.getLiveDocs(reader);

    /*
     *  Lookup the collection term frequency (ctf).
     */
    long df = reader.docFreq(term);
    System.out.println("\tdf:  " + df);

    long ctf = reader.totalTermFreq(term);
    System.out.println("\tctf:  " + ctf);

    if (df < 1) return;

    /*
     *  Lookup the inverted list.
     */
    DocsAndPositionsEnum postings =
        MultiFields.getTermPositionsEnum(reader, liveDocs, field, termBytes);

    /*
     *  Iterate through the first n postings.
     */
    long count = 0;

    while ((count < n) && (postings.nextDoc() != DocIdSetIterator.NO_MORE_DOCS)) {

      System.out.println("\tdocid: " + postings.docID());
      int tf = postings.freq();
      System.out.println("\ttf: " + tf);
      System.out.print("\tPositions: ");

      for (int j = 0; j < tf; j++) {
        int pos = postings.nextPosition();
        System.out.print(pos + " ");
      }

      System.out.println("");

      count++;
    }
    ;

    return;
  }
示例#2
0
 /** test that when freqs are omitted, that totalTermFreq and sumTotalTermFreq are -1 */
 public void testStats() throws Exception {
   Directory dir = newDirectory();
   RandomIndexWriter iw =
       new RandomIndexWriter(
           random(), dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
   Document doc = new Document();
   FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
   ft.setIndexOptions(IndexOptions.DOCS_ONLY);
   ft.freeze();
   Field f = newField("foo", "bar", ft);
   doc.add(f);
   iw.addDocument(doc);
   IndexReader ir = iw.getReader();
   iw.close();
   assertEquals(-1, ir.totalTermFreq(new Term("foo", new BytesRef("bar"))));
   assertEquals(-1, ir.getSumTotalTermFreq("foo"));
   ir.close();
   dir.close();
 }