Class | Ferret::Index::TermDocEnum
In: | ext/r_index.c
Parent: | Object

## Summary

Use a TermDocEnum to iterate through the documents that contain a particular term. You can also iterate through the positions which the term occurs in a document.

## Example

      tde = index_reader.term_docs_for(:content, "fox")

      tde.each do |doc_id, freq|
        puts "fox appeared #{freq} times in document #{doc_id}:"
        positions = []
        tde.each_position {|pos| positions << pos}
        puts "  #{positions.join(', ')}"
      end

      # or you can do it like this;
      tde.seek(:title, "red")
      while tde.next?
        puts "red appeared #{tde.freq} times in document #{tde.doc}:"
        positions = []
        while pos = tde.next_position
          positions << pos
        end
        puts "  #{positions.join(', ')}"
      end

### Methods

doc each each_position freq next? next_position seek seek_term_enum skip_to to_json

### Public Instance methods

term_doc_enum.doc → doc_id

Returns the current document number pointed to by the `term_doc_enum`.

term_doc_enum.each {|doc_id, freq| do_something() } → doc_count

Iterate through the documents and document frequencies in the `term_doc_enum`.

NOTE: this method can only be called once after each seek. If you need to call +each+ again then you should call +seek+ again too.

term_doc_enum.each_position {|pos| do_something } → term_doc_enum

Iterate through each of the positions occupied by the current term in the current document. This can only be called once per document. It can be used within the each method. For example, to print the terms documents and positions;

      tde.each do |doc_id, freq|
        puts "term appeared #{freq} times in document #{doc_id}:"
        positions = []
        tde.each_position {|pos| positions << pos}
        puts "  #{positions.join(', ')}"
      end

term_doc_enum.doc → doc_id

Returns the frequency of the current document pointed to by the `term_doc_enum`.

term_doc_enum.doc → doc_id

Move forward to the next document in the enumeration. Returns `true` if there is another document or `false` otherwise.

term_doc_enum.doc → doc_id

Move forward to the next document in the enumeration. Returns `true` if there is another document or `false` otherwise.

term_doc_enum.seek(field, term) → self

Seek the term `term` in the index for `field`. After you call this method you can call next or each to skip through the documents and positions of this particular term.

term_doc_enum.seek_term_enum(term_enum) → self

Seek the current term in `term_enum`. You could just use the standard seek method like this;

      term_doc_enum.seek(term_enum.term)

However the `seek_term_enum` method saves an index lookup so should offer a large performance improvement.

term_doc_enum.skip_to(target) → bool

Skip to the required document number `target` and return true if there is a document >= `target`.

term_doc_enum.to_json() → string

Returns a json representation of the term doc enum. It will also add the term positions if they are available. You can speed this up by having the method return arrays instead of objects, simply by passing an argument to the to_json method. For example;

      term_doc_enum.to_json() #=>
      # [
      #   {"document":1,"frequency":12},
      #   {"document":11,"frequency":1},
      #   {"document":29,"frequency":120},
      #   {"document":30,"frequency":3}
      # ]

      term_doc_enum.to_json(:fast) #=>
      # [
      #   [1,12],
      #   [11,1],
      #   [29,120],
      #   [30,3]
      # ]

[Validate]