root/Swishetest/trunk/DoSearch.pm

Revision 2038, 2.8 kB (checked in by joshr, 4 months ago)

don't store all the props in memory: just the swishdocpath.

Line 
1 package DoSearch;
2
3 use SWISH::API;
4 use strict;
5 use warnings;
6
7 my %swishes = ();   # map of filename -> SWISH::API
8
9 # given an index filename and a query, opens the index if needed,
10 #  performs the search, and
11 # returns the results as a list of hashrefs
12
13 sub open_index {
14     my $index = shift;
15     if (!exists($swishes{$index})) {
16         my $swish = $swishes{$index} = SWISH::API->new( $index );
17         die "$0: index $index could not be opened.\n" unless $swish;
18         print STDERR "Index $index opened\n" if $ENV{TEST_VERBOSE};
19     }
20     return 1;
21 }
22
23 sub close_index {
24     my $index = shift;
25     if (exists($swishes{$index})) {
26         delete $swishes{$index};    # remove from the hash, should close it
27     } else {
28         die "$0: index $index was not open.\n";
29     }
30 }
31 sub do_search {
32     my ($index, $query) = @_;   
33     my @r = ();
34     #return @r unless $query;
35     my $swish;
36     eval {
37         if (exists($swishes{$index})) {
38             $swish = $swishes{$index};
39         } else {
40             #$swish = $swishes{$index} = SWISH::API->new( $index );
41             die "$0: index $index was not opened.\n";
42         }
43         #print STDERR "Searching for $query\n" if $ENV{TEST_VERBOSE};
44         my $results = $swish->Query( $query );
45         my @props = map { $_->Name } ($swish->PropertyList( $index ) );
46         if ($swish->Error()) {
47             #print STDERR "$0: Error searching for $query: " . $swish->ErrorString();
48             return @r;
49         }
50
51         while ( my $result = $results->NextResult() ) {
52             my %h;
53             #for my $p (@props) { $h{$p} = $result->Property($p); }
54             for my $p (@props) { if($p eq "swishdocpath") { $h{$p} = $result->Property($p);}  }
55             push(@r, \%h);
56             #push( @r, {swishdocpath=>$result->Property( "swishdocpath" );
57         }
58     };  # end eval{}
59     if ($@) {
60         my $str = "$0: test failed: $@";
61         if ($swish && $swish->Error()) {
62             $str .= " (" . $swish->ErrorString() . ")";
63         }
64     }
65     return @r;
66 }
67
68 1;
69
70 __END__
71
72 =head1 NAME
73
74 Swishetest - Library routines for the Swishetest tool
75
76 =head1 SYNOPSIS
77
78 See tests in t/
79
80 =head1 DESCRIPTION
81
82 build_index() builds an index given a directory and a target index, and returns
83 some data about the index (from 'swish-e -v 1 ...') in a hash. It takes four parameters:
84
85  my %opts = build_index( $input_dir, $index, [$configfile], [$extraswisheoptions] );
86  open_index( $index );
87  my @r = do_search( $index, $query );
88  close_index( $index );
89
90 do_search() returns a list of hashrefs of the rows returned from the search.
91
92 =head2 EXPORT
93
94 None by default. 
95
96 =head1 AUTHOR
97
98 Josh Rabinowitz, E<lt>joshrE<gt>
99
100 =head1 COPYRIGHT AND LICENSE
101
102 Copyright 2004-2007 by Josh Rabinowitz
103
104 This library is free software; you can redistribute it and/or modify
105 it under the same terms as Perl itself.
106
107 =cut
Note: See TracBrowser for help on using the browser.