in reply to KinoSearch - is there a way to iterate over all documents in an index?

I know this is kinda old but I actually had occasion to do precisely what the author is asking here.

In a nutshell...
  1. Get yourself an IndexReader (of some kind) by calling IndexReader->open on your inverted index (check the API). Call it $reader.
  2. for $i = 1 to $reader->num_docs(), call $reader->fetch_doc_vec($i). The return value is a DocVector.
Here you run into trouble..
as far as I can tell. Take a look at DocVector.pm. The method "term_vector" appears to require that you specify a term for which to retrieve positional and frequency data. But if you're iterating over all the documents, presumably you didn't have any specific terms in mind. :) I decided to write my own method at this point and to add it to the DocVector.pm class. Here it is... it's kinda ugly. It returns an associative array of terms to in-document term frequencies.
sub get_term_data { my ( $self ) = @_; my @fields = $self->get_field_names(); my $termdata = {}; for my $field (@fields) { my $field_vector = $self->{field_vectors}{$field}; if ( !defined $field_vector ) { my $field_string = $self->{field_strings}{$field}; return unless defined $field_string; $field_vector = $self->{field_vectors}{$field} = _extract_tv_cache($field_string); } my @terms_for_field = keys %{$field_vector}; for my $term (@terms_for_field) { my ($positions, $starts, $ends) = _extract_posdata($fie +ld_vector- >{$term}); my $termvector = KinoSearch::Index::TermVector->new( field => $field, text => $term, positions => $positions, start_offsets => $starts, end_offsets => $ends, ); # ok.. we have the term vector. how do we get the # term frequency for this document? my $term_freq = scalar(@{$positions}); $termdata->{$term} = $term_freq; } } return $termdata; }
  • Comment on Re: KinoSearch - is there a way to iterate over all documents in an index?
  • Download Code