You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by gagan_goku <ga...@gmail.com> on 2013/03/30 20:12:36 UTC

Re: How to get Term Positions?

I tried the same thing today, am happy to share a snippet with you:


    SchemaField field = req.getSchema().getFields().get("field_name");

    AtomicReader ar = req.getSearcher().getAtomicReader();
    AtomicReaderContext context = ar.getContext();
    final Fields fields = context.reader().fields();
    final Terms terms = fields.terms("field_name");
    final TermsEnum termsEnum = terms.iterator(null);

    Bits acceptDocs = new Bits.MatchAllBits(10);

    BytesRef bytes;
    while ((bytes = termsEnum.next()) != null) {
      CharsRef chars = new CharsRef();
      field.getType().indexedToReadable(bytes, chars);

      final DocsAndPositionsEnum postings =
termsEnum.docsAndPositions(acceptDocs, null,
DocsAndPositionsEnum.FLAG_PAYLOADS);
      assertNotNull(postings);

      List<Integer> docIds = new ArrayList<Integer>();
      int docId;
      while ((docId = postings.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS)
{
        docIds.add(docId);
        int freq = postings.freq();
        for (int i = 0; i < freq; i++) {
          int nextPosition = postings.nextPosition();

          String str = docId + "\t" + chars.toString() + "\t" +
nextPosition;
          System.out.println(str);
        }
      }
    }




--
View this message in context: http://lucene.472066.n3.nabble.com/How-to-get-Term-Positions-tp477519p4052608.html
Sent from the Solr - User mailing list archive at Nabble.com.