XAO-Indexer
view release on metacpan or search on metacpan
lib/XAO/DO/Data/Index.pm view on Meta::CPAN
Returns corresponding indexer object, its name taken from
'indexer_objname' property.
=cut
# TODO: there is something inherently wrong with all this passing around
# of two different objects. Should be done differently. (am@, 3/21/2005)
sub indexer ($$) {
my ($self,$indexer_objname)=@_;
$indexer_objname||=$self->get('indexer_objname');
$indexer_objname || throw $self "init - no 'indexer_objname'";
return XAO::Objects->new(
objname => $indexer_objname,
index_id => $self->container_key,
index_obj => $self,
) || throw $self "init - can't load object '$indexer_objname'";
}
###############################################################################
=item search_by_string ($)
Most widely used method - parses string into keywords and performs a
search on them. Honors double quotes to mark words that have to be
together in a specific order.
Returns a reference to the list of collection IDs. IDs are not checked
against real collection. If index is not in sync with the content of the
actual data collection IDs of objects that don't exist any more can be
returned as well as irrelevant results.
Example:
my $keywords=$cgi->param('keywords');
my $cn_index=$odb->fetch('/Indexes/customer_names');
my $sr=$cn_index->search_by_string('name',$keywords);
Optional third argument can refer to a hash. If it is present, the hash
will be filled with some internal information. Most useful of which is
the list of ignored words from the query, stored as 'ignored_words' in
the hash.
Example:
my %sd;
my $sr=$cn_index->search_by_string('name',$keywords,\%sd);
if(keys %{$sd{ignored_words}}) {
print "Ignored words:\n";
foreach my $word (sort keys %{$sd{ignored_words}}) {
print " * $word ($sd{ignored_words}->{$word}\n";
}
}
=cut
sub search_by_string ($$$;$) {
my ($self,$ordering,$str,$rcdata)=@_;
return $self->indexer->search(
index_object => $self,
search_string => $str,
ordering => $ordering,
rcdata => $rcdata,
);
}
###############################################################################
=item search_by_string_oid ($)
The same as search_by_string() method, but translates results from
collection IDs to object IDs. Use it with care, on large result sets it
may take significant time!
=cut
sub search_by_string_oid ($$$;$) {
my ($self,$ordering,$str,$rcdata)=@_;
my $cids=$self->indexer->search(
index_object => $self,
search_string => $str,
ordering => $ordering,
rcdata => $rcdata,
);
my $coll_obj=$self->get_collection_object;
my @oids;
foreach my $cid (@$cids) {
try {
push(@oids,$coll_obj->get($cid)->container_key);
}
otherwise {
my $e=shift;
dprint "Stale cid=$cid while search for '$str': $e";
};
}
return \@oids;
}
###############################################################################
=item suggest_alternative ($$$)
Returns an alternative search string by trying words found during
search_by_string and stored in the returned data array.
EXPERIMENTAL UNSTABLE API.
=cut
sub suggest_alternative ($$$$;$) {
my ($self,$ordering,$str,$rcdata,$need_results)=@_;
return $self->indexer->suggest_alternative(
index_object => $self,
search_string => $str,
ordering => $ordering,
rcdata => $rcdata,
need_results => $need_results,
);
}
###############################################################################
=item update ($)
Updates the index with the current data. Exactly what data it is based
on depends entirely on the corresponding indexer object.
With drivers that support transactions the update is wrapped into a
transaction, so that index data is consistent while being updated.
=cut
sub update ($) {
my $self=shift;
$self->indexer->update(
index_object => $self,
);
}
###############################################################################
=item build_dictionary (%)
Updates the dictionary of words stored in this index. Actual
implementation depends on the specific spellchecker, as configured for
the project.
=cut
sub build_dictionary ($) {
my $self=shift;
return $self->indexer->build_dictionary(
index_object => $self,
);
}
###############################################################################
1;
__END__
=back
=head1 AUTHORS
Copyright (c) 2003 XAO Inc.
Andrew Maltsev <am@xao.com>.
=head1 SEE ALSO
Recommended reading:
L<XAO::Indexer>,
L<XAO::DO::Indexer::Base>,
L<XAO::FS>,
( run in 1.117 second using v1.01-cache-2.11-cpan-f56aa216473 )