1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>SWISH-Enhanced: SWISH::API - Perl interface to the Swish-e C Library </title>
<link href="./style.css" rel=stylesheet type="text/css" title="refstyle">
</head>
<body>
<h1 class="banner">
<a href="http://swish-e.org"><img border=0 src="images/swish.gif" alt="Swish-E Logo"></a><br>
<img src="images/swishbanner1.gif"><br>
<img src="images/dotrule1.gif"><br>
SWISH::API - Perl interface to the Swish-e C Library
</h1>
<hr>
<p>
<div class="navbar">
<a href="./SWISH-LIBRARY.html">Prev</a> |
<a href="./index.html">Contents</a> |
<a href="./swish.html">Next</a>
</div>
<p>
<div class="toc">
<A NAME="toc"></A>
<P><B>Table of Contents:</B></P>
<UL>
<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
<LI><A HREF="#DEPENDENCIES">DEPENDENCIES</A>
<LI><A HREF="#OVERVIEW">OVERVIEW</A>
<LI><A HREF="#METHODS">METHODS</A>
<UL>
<LI><A HREF="#SWISH_API_Swish_Handle_Object">SWISH::API - Swish Handle Object</A>
<UL>
<LI><A HREF="#Error_Handling">Error Handling</A>
<LI><A HREF="#Generating_Search_and_Result_Objects">Generating Search and Result Objects</A>
</UL>
<LI><A HREF="#SWISH_API_Search_Search_Objects">SWISH::API::Search - Search Objects</A>
<LI><A HREF="#SWISH_API_Results_Generating_and_accessing_results">SWISH::API::Results - Generating and accessing results</A>
<LI><A HREF="#Results_Methods">Results Methods</A>
<LI><A HREF="#SWISH_API_Result_Result_Methods">SWISH::API::Result - Result Methods</A>
<LI><A HREF="#Utility_Methods">Utility Methods</A>
</UL>
<LI><A HREF="#NOTES">NOTES</A>
<LI><A HREF="#COPYRIGHT">COPYRIGHT</A>
<LI><A HREF="#AUTHOR">AUTHOR</A>
<LI><A HREF="#SUPPORT">SUPPORT</A>
</UL>
</div>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<P>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> use SWISH::API;</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> my $swish = SWISH::API->new( 'index.swish-e' );</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $swish->AbortLastError
if $swish->Error;</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # A short-cut way to search</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> my $results = $swish->Query( "foo OR bar" );</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # Or more typically
my $search = $swish->New_Search_Object;</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # then in a loop
my $results = $search->Execute( $query );</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # always check for errors (but aborting is not always necessary)</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $swish->AbortLastError
if $swish->Error;</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # Display a list of results</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> my $hits = $results->Hits;
if ( !$hits ) {
print "No Results\n";
return; /* for example *.
}</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> print "Found ", $results->Hits, " hits\n";</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # Seek to a given page - should check for errors
$results->SeekResult( ($page-1) * $page_size );</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> while ( my $result = $results->NextResult ) {
printf("Path: %s\n Rank: %lu\n Size: %lu\n Title: %s\n Index: %s\n Modified: %s\n Record #: %lu\n File #: %lu\n\n",
$result->Property( "swishdocpath" ),
$result->Property( "swishrank" ),
$result->Property( "swishdocsize" ),
$result->Property( "swishtitle" ),
$result->Property( "swishdbfile" ),
$result->ResultPropertyStr( "swishlastmodified" ),
$result->Property( "swishreccount" ),
$result->Property( "swishfilenum" )
);
}</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # display properties and metanames</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> for my $index_name ( $swish->IndexNames ) {
my @metas = $swish->MetaList( $index_name );
my @props = $swish->PropertyList( $index_name );</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> for my $m ( @metas ) {
my $name = $m->Name;
my $id = $m->ID;
my $type = $m->Type;
}
# (repeat above for @props)
}</pre>
</td>
</tr>
</table>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
This module provides a Perl interface to the Swish-e search engine. This
module allows embedding the swish-e search code into your application
avoiding the need to fork to run the swish-e binary and to keep an index
file open when running multiple queries. This results in increased search
performance.
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H1><A NAME="DEPENDENCIES">DEPENDENCIES</A></H1>
<P>
You must have installed Swish-e version 2.4 before building this module.
Download from:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> <A HREF="http://swish-e.org">http://swish-e.org</A></pre>
</td>
</tr>
</table>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H1><A NAME="OVERVIEW">OVERVIEW</A></H1>
<P>
This module includes a number of classes.
<P>
Searching consists of connecting to a swish-e index (or indexes), and then
running queries against the open index. Connecting to the index creates a
swish object blessed into the SWISH::API class.
<P>
A SWISH::API::Search object is created from the SWISH::API object. The
SWISH::API::Search object can have associated parameters (e.g. result sort
order).
<P>
The SWISH::API::Search object is used to query the associated index file or
files. A query on a search object returns a results object of the class
SWISH::API::Results. Then individual results of the SWISH::API::Result
class can be fetched by calling a method of the results object.
<P>
Finally, a result's properties can be accessed by calling methods on the
result object.
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H1><A NAME="METHODS">METHODS</A></H1>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="SWISH_API_Swish_Handle_Object">SWISH::API - Swish Handle Object</A></H2>
<P>
To begin using Swish you must first create a Swish Handle object. This
object makes the connection to one or more index files and is used to
create objects used for searching the associated index files.
<DL>
<P><DT><STRONG><A NAME="item__swish">$swish = SWISH::API->new( $index_files );</A></STRONG><DD>
<P>
This method returns a swish handle object blessed into the SWISH::API
class. <CODE>$index_files</CODE> is a space separated list of index files
to open. This always returns an object, even on errors. Caller must check
for errors (see below).
<P><DT><STRONG><A NAME="item__indexes">@indexes = $swish->IndexNames;</A></STRONG><DD>
<P>
Returns a list of index names associated with the swish handle. These were
the indexes specified as a parameter on the SWISH::API->new call. This
can be used in calls below that require specifying the index file name.
<P><DT><STRONG><A NAME="item__header_names">@header_names = $swish->HeaderNames;</A></STRONG><DD>
<P>
Returns a list of possible header names. These can be used to lookup header
values. See <CODE>SwishHeaderValue</CODE> method below.
<P><DT><STRONG><A NAME="item__values">@values = $swish->HeaderValue( $index_file, $header_name );</A></STRONG><DD>
<P>
A swish-e index has data associated with it stored in the index header.
This method provides access to that data.
<P>
Returns the header value for the header and index file specified. Most
headers are a single item, but some headers (e.g. "Stopwords")
return a list.
<P>
The list of possible header names can be obtained from the SwishHeaderNames
method.
<P><DT><STRONG><A NAME="item__swish_RankScheme_">$swish->RankScheme( 0|1 );</A></STRONG><DD>
<P>
Similar to the -R option with the swish-e command line tool. The default
ranking scheme is 0. Set it to 1 to experiment with other ranking features.
See the SWISH-CONFIG documentation for more on ranking schemes.
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H3><A NAME="Error_Handling">Error Handling</A></H3>
<P>
All errors are stored in and accessed via the SWISH::API object (the Swish
Handle). That is, even an error that occurs when calling a method on a
result (SWISH::API::Result) object will store the error in the parent
SWISH:API object.
<P>
Check for errors after every method call. Some errors are critical errors
and will require destruction of the SWISH::API object. Critical errors will
typically only happen when attaching to the database and are errors such as
an invalid index file name, permissions errors, or passing invalid objects
to calls.
<P>
Typically, if you receive an error when attaching to an index file or files
you should assume that the error is critical and let the swish object fall
out of scope (and destroyed). Otherwise, if an error is detected you should
check if it is a critical error. If the error is not critical you may
continue using the objects that have been created (for example, an invalid
meta name will generate a non-critical error, so you may continue searching
using the same search object).
<P>
Error state is cleared upon a new query.
<P>
Again, all error methods need to be called on the parent swish object
<DL>
<P><DT><STRONG><A NAME="item__swish_Error">$swish->Error</A></STRONG><DD>
<P>
Returns true if an error occurred on the last operation. On errors the
value returned is the internal Swish-e error number (which is less than
zero).
<P><DT><STRONG><A NAME="item__swish_CriticalError">$swish->CriticalError</A></STRONG><DD>
<P>
Returns true if the last error was a critical error
<P><DT><STRONG><A NAME="item__swish_AbortLastError">$swish->AbortLastError</A></STRONG><DD>
<P>
Aborts the running program and prints an error message to STDERR.
<P><DT><STRONG><A NAME="item__str">$str = $swish->ErrorString</A></STRONG><DD>
<P>
Returns the string description of the current error (based on the value
returned by $swish->Error). This is a generic error string.
<P><DT><STRONG><A NAME="item__msg">$msg = $swish->LastErrorMsg</A></STRONG><DD>
<P>
Returns a string with specific information about the last error, if any.
For example, if a query of:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> badmeta=foo</pre>
</td>
</tr>
</table>
<P>
and "badmeta" is an invalid metaname $swish->ErrorString might
return "Unknown metaname", but $swish->LastErrorMsg might
return "badmeta".
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H3><A NAME="Generating_Search_and_Result_Objects">Generating Search and Result Objects</A></H3>
<DL>
<P><DT><STRONG><A NAME="item__search">$search = $swish->New_Search_Object( $query );</A></STRONG><DD>
<P>
This creates a new search object blessed into the SWISH::API::Search class.
The optional <CODE>$query</CODE> parameter is a query string to store in
the search object.
<P>
See the section on <CODE>SWISH::API::Search</CODE> for methods available on the returned object.
<P>
The advantage of this method is that a search object can be used for
multiple queries:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $search = $swish->New_Search_Objet;
while ( $query = next_query() ) {
$results = $search->Execute( $query );
...
}</pre>
</td>
</tr>
</table>
<P><DT><STRONG><A NAME="item__results">$results = $swish->Query( $query );</A></STRONG><DD>
<P>
This is a short-cut which avoids the step of creating a separate search
object. It returns a results object blessed into the SWISH::API::Results
class described below.
<P>
This method basically is the equivalent of
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $results = $swish->New_Search_Object->Execute( $query );</pre>
</td>
</tr>
</table>
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="SWISH_API_Search_Search_Objects">SWISH::API::Search - Search Objects</A></H2>
<P>
A search object holds the parameters used to generate a list of results.
These methods are used to adjust these parameters and to create the list of
results for the current set of search parameters.
<DL>
<P><DT><STRONG><A NAME="item__search_SetQuery_">$search->SetQuery( $query );</A></STRONG><DD>
<P>
This will set (or replace) the query string associated with a search
object. This method is typically not used as the query can be set when
executing the actual query or when creating a search object.
<P><DT><STRONG><A NAME="item__search_SetStructure_">$search->SetStructure( $structure_bits );</A></STRONG><DD>
<P>
This method may change in the future.
<P>
A "structure" is a bit-mapped flag used to limit search results
to specific parts of an HTML document, such as the title or in H tags. The
possible bits are:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> IN_FILE = 1 This is the default
IN_TITLE = 2 In <title> tag
IN_HEAD = 4 In <head> tag
IN_BODY = 8 In <body>
IN_COMMENTS = 16 In html comments
IN_HEADER = 32 In <h*>
IN_EMPHASIZED = 64 In <em>, <b>, <strong>, <i>
IN_META = 128 In a meta tag (e.g. not swishdefault)</pre>
</td>
</tr>
</table>
<P>
So if you wish to limit your searches to words in heading tags (e.g. <H1>) or in the <title> tag use:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $search->SetStructure( IN_HEAD | IN_TITLE );</pre>
</td>
</tr>
</table>
<P><DT><STRONG><A NAME="item__search_PhraseDelimiter_">$search->PhraseDelimiter( $char );</A></STRONG><DD>
<P>
Sets the character used as the phrase delimiter in searches. The default is
double-quotes (").
<P><DT><STRONG><A NAME="item__search_SetSearchLimit_">$search->SetSearchLimit( $property, $low, $high );</A></STRONG><DD>
<P>
Sets a range from <CODE>$low</CODE> to <CODE>$high</CODE> inclusive that
the give <CODE>$property</CODE> must be in to be selected as a result. Call
multiple times to set more than one limit on different properties. Limits
are ANDed, that is, a result must be within the range of all limits
specified to be included in a list of results.
<P>
For example to limit searches to documents modified in the last 48 hours:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> my $start = time - 48 * 60 * 60;
$search->SetSearchLimit( 'swishlastmodified', $start, time() );</pre>
</td>
</tr>
</table>
<P>
An error will be set if the property has already been specified or if
<CODE>$high</CODE> < $low.
<P>
Other errors may not be reported until running the query, such as the
property name is invalid or if <CODE>$low</CODE> or <CODE>$high</CODE> are
not numeric and the property specified is a numeric property.
<P>
Once a query is run you cannot change the limit settings without calling
the ResetSearchLimit method first.
<P><DT><STRONG><A NAME="item__search_ResetSearchLimit_">$search->ResetSearchLimit;</A></STRONG><DD>
<P>
Clears the limit parameters for the given object. This must be called if
the limit parameters need to be changed.
<P><DT><STRONG><A NAME="item__search_SetSort_">$search->SetSort( $sort_string );</A></STRONG><DD>
<P>
Sets the sort order of search results. The string is a space separated list
of valid document properties. Each property may contain a qualifier that
sets the direction of the sort.
<P>
For example, to sort the results by path name in ascending order and by
rank in descending order:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $search->SetSort( 'swishdocpath asc swishrank desc' );</pre>
</td>
</tr>
</table>
<P>
The "asc" and "desc" qualifiers are optional, and if
omitted ascending is assumed.
<P>
Currently, errors (e.g invalid property name) are not detected on this
call, but rather when executing a query. This may change in the future.
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="SWISH_API_Results_Generating_and_accessing_results">SWISH::API::Results - Generating and accessing results</A></H2>
<P>
Searching generates a results object blessed into the SWISH::API::Results
class.
<DL>
<P><DT><STRONG>$results = $search->Execute( $query );</STRONG><DD>
<P>
Executes a query based on the parameters in the search object.
<CODE>$query</CODE> is an optional query string to use for the search
($query replaces the set query string in the search object).
<P>
A typical use would be to create a search object once and then call this
method for each query using the same search object changing only the passed
in $query.
<P>
The caller should check for errors after making this all.
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="Results_Methods">Results Methods</A></H2>
<P>
A query creates a results object that contains information about the query
(e.g. number of hits) and access to the individual results.
<DL>
<P><DT><STRONG><A NAME="item__hits">$hits = $results->Hits;</A></STRONG><DD>
<P>
Returns the number of results for the query. If zero and no errors were
reported after calling $search->Execute then the query returned zero
results.
<P><DT><STRONG><A NAME="item__parsed_words">@parsed_words = $results->ParsedWords( $index_name );</A></STRONG><DD>
<P>
Returns an array of tokenized words and operators with stopwords removed.
This is the array of tokens used by swish for the query.
<P>
<CODE>$index_name</CODE> must match one of the index files specified on the
creation of the swish object (via the SWISH::API->new call).
<P>
The parsed words are useful for highlighting search terms in associated
documents.
<P><DT><STRONG><A NAME="item__removed_stopwords">@removed_stopwords = $results->RemovedStopwords( $index_name) ;</A></STRONG><DD>
<P>
Returns an array of stopwords removed from a query, if any, for the index
specified.
<P>
<CODE>$index_name</CODE> must match one of the index files specified on the
creation of the swish object (via the SWISH::API->new call).
<P><DT><STRONG><A NAME="item__results_SeekResult_">$results->SeekResult( $position );</A></STRONG><DD>
<P>
Seeks to the position specified in the result list. Zero is the first
position and $results->Hits-1 is the last position. Seeking past the end
of results sets a non-critical error condition.
<P>
Useful for seeking to a specific "page" of results.
<P><DT><STRONG><A NAME="item__result">$result = $results->NextResult;</A></STRONG><DD>
<P>
Fetches the next result from the list of results. Returns undef if no more
results are available. <CODE>$result</CODE> is an object blessed into the
SWISH::API::Result class.
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="SWISH_API_Result_Result_Methods">SWISH::API::Result - Result Methods</A></H2>
<P>
The follow methods provide access to data related to an individual result.
<DL>
<P><DT><STRONG><A NAME="item__prop">$prop = $result->Property( $prop_name );</A></STRONG><DD>
<P>
Fetches the property specified for the current result. An invalid property
name will cause an exception (which can be caught by wrapping the call in
an eval block).
<P>
Can return undefined.
<P>
Date properties are returned as a timestamp. Use something like
Date::Format to format the strings (or just call scalar
<CODE>localtime(</CODE> <CODE>$prop</CODE> ) ).
<P><DT><STRONG>$prop = $result->ResultPropertyStr( $prop_name );</STRONG><DD>
<P>
Fetches and formats the property. Unlike above, invalid property names
return the string "(null)" -- this will likely change to match
the above (i.e. throw an exception).
<P>
Undefined values are returned at the null string ("").
<P><DT><STRONG><A NAME="item__value">$value = $result->ResultIndexValue( $header_name );</A></STRONG><DD>
<P>
Returns the header value specified. This is similar to
$swish->HeaderValue(), but the index file is not specified (it is
determined by the result).
</DL>
<P>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<H2><A NAME="Utility_Methods">Utility Methods</A></H2>
<DL>
<P><DT><STRONG><A NAME="item__metas">@metas = $swish->MetaList( $index_name );</A></STRONG><DD>
<P>
Swish-e has "MetaNames" which allow searching by fields in the
index. This method returns information about the Metanames.
<P>
Pass in the name of an open index file name and returns a list of
SWISH::API::MetaName objects. Three methods are currently defined on these
objects:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $meta->Name;
$meta->ID;
$meta->Type;</pre>
</td>
</tr>
</table>
<P>
Name returns the name of the meta as defined in the MetaNames config option
when the index was created.
<P>
The ID is the internal ID number used to represent the meta name.
<P>
Type is the type of metaname. Currently only one type exists and its value
is zero.
<P><DT><STRONG><A NAME="item__props">@props = $swish->PropertyList( $index_name );</A></STRONG><DD>
<P>
Swish-e can store content or "properties" in the index and return
this data when running a query. A document's path, URL, title, size, date
or summary are examples of properites. Each property is accessed via its
PropertyName. This method returns information about the PropertNames stored
in the index.
<P>
Pass in the name of an open index file name and returns a list of
SWISH::API::MetaName objects. Three methods are currently defined on these
objects:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> $prop->Name;
$prop->ID;
$prop->Type;</pre>
</td>
</tr>
</table>
<P>
Name returns the name of the meta as defined in the MetaNames config option
when the index was created.
<P>
The ID is the internal ID number used to represent the meta name.
<P>
Type is the type of metaname. Currently only one type exists and its value
is zero.
<P><DT><STRONG><A NAME="item__propes">@propes = $result->PropertyList;</A></STRONG><DD>
<P><DT><STRONG><A NAME="item__meta">@meta = $result->MetaList;</A></STRONG><DD>
<P>
These also return a list of Property or Metaname description objects, but
are accessed via a result record. Since the result comes from a specific
index file there's no need to specify the index file name.
<P><DT><STRONG><A NAME="item__stemmed_word">$stemmed_word = $swish->StemWord( $word );</A></STRONG><DD>
<P>
*Deprecated*
<P>
Returns the stemmed version of the passed in word.
<P>
Deprecated because only stems using the original Porter Stemmer and uses a
shared memory location in the SW_HANDLE object to store the stemmed word.
See below for other stemming options.
<P><DT><STRONG><A NAME="item__fuzzy_word">$fuzzy_word = $swish->Fuzzy( $indexname, $word );</A></STRONG><DD>
<P>
Like StemWord used to work, only it uses whatever stemmer is named in
$indexname. Returns the same kind of fuzzy_word object as the
<CODE>FuzzyWord()</CODE> method.
<P><DT><STRONG><A NAME="item__mode_string">$mode_string = $result->FuzzyMode;</A></STRONG><DD>
<P>
Returns the string (e.g. "Stemming_en", "Soundex",
"None" ) indicating the stemming method used while indexing the
given document.
<P><DT><STRONG>$fuzzy_word = $result->FuzzyWord( $word );</STRONG><DD>
<P>
Converts <CODE>$word</CODE> using the same fuzzy mode used to index the
$result. Returns a SWISH::API::FuzzyWord object. Methods on the object are
used to access the converted words and other data as shown below.
<P><DT><STRONG><A NAME="item__count">$count = $fuzzy_word->WordCount;</A></STRONG><DD>
<P>
Returns the number of output words. Normally this is the value one, but may
be more depending on the stemmer used. DoubleMetaphone can return two
strings for a single input string.
<P><DT><STRONG><A NAME="item__status">$status = $fuzzy_word->WordError;</A></STRONG><DD>
<P>
Returns any error code that the stemmer might set. Normally, this return
value is zero, indicating that the stemming/fuzzy operation succedded. The
values returned are defined in the swish-e source file /src/stemmer.h.
<P><DT><STRONG><A NAME="item__words">@words = $fuzzy_word->WordList;</A></STRONG><DD>
<P>
Returns the converted words from the stemming/fuzzy operation. Normally,
the array will contain a single element, although may contain more (i.e. if
DoubleMetaphone is used and the input word returns two strings).
<P>
In the event that a word does not stem (e.g. trying to stem a number), this
method will return the original input word specified when
$result->FuzzyWord( <CODE>$word</CODE> ) was called.
<P><DT><STRONG>@parsed_words = $swish->SwishWords( $string, $index_file );</STRONG><DD>
<P>
* Not implemented *
<P>
Splits up the input string into tokens of swish words and operators.
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
Perl's garbage collection makes it easy to write code for searching with
Swish-e, but care must be taken not to keep objects around too long which
can use up memory.
<P>
Here's an example of a potential problem. Say you have a very large number
of documents indexed and you want to find the first hit for a number of
popular keywords (error checking omitted in this bad example):
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> sub first_hit {
my $query = shift;
my $handle = SWISH::API->new( 'index.swish-e');
my $results = $handle->Query( $query );
my $first_hit = $results->NextResult;
return $first_hit;
}</pre>
</td>
</tr>
</table>
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> my @first_hit_list;
for ( @keywords )
push @first_hit_list, $first_hit($_);
}</pre>
</td>
</tr>
</table>
<P>
The <CODE>first_hit()</CODE> subroutine is returning a SWISH::Result
object. That makes it easy to access properties:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> # print file names
for my $result ( @first_hit_list ) {
print $result->Property('swishdocpath'),"\n";
}</pre>
</td>
</tr>
</table>
<P>
But as long as a SWISH::API::Result object is around, so is the entire list
of results generated by the $handle->Query() call, and the index file is
still open (because a SWISH::API::Result depends on a SWISH::API::Results
object, which depends on a SWISH::API object).
<P>
In this case it would be better to return from <CODE>first_hit()</CODE>
just the properties you need:
<P>
<table>
<tr>
<td bgcolor="#eeeeee" width="1">
</td>
<td>
<pre> ...
my $first_hit = $results->NextResult;
return $first_hit->Property('swishdocpath');
}</pre>
</td>
</tr>
</table>
<P>
Then when <CODE>first_hit()</CODE> sub ends the result list will be freed,
and the index file closed, thanks to Perl's reference count tracking.
<P>
Note: the other problem with the above code is that the same index file is
opened for each call to the function. Don't do that, instead open the index
file once.
<H1><A NAME="COPYRIGHT">COPYRIGHT</A></H1>
<P>
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
<H1><A NAME="AUTHOR">AUTHOR</A></H1>
<P>
Bill Moseley <A HREF="mailto:moseley@hank.org.">moseley@hank.org.</A>
2002/2003/2004
<H1><A NAME="SUPPORT">SUPPORT</A></H1>
<P>
Please contact the Swish-e discussion email list for support with this
module or with Swish-e. Please do not contact the developers directly.
</DL>
[ <B><FONT SIZE=-1><A HREF="#toc">TOC</A></FONT></B> ]
<HR>
<p>
<div class="navbar">
<a href="./SWISH-LIBRARY.html">Prev</a> |
<a href="./index.html">Contents</a> |
<a href="./swish.html">Next</a>
</div>
<p>
<P ALIGN="CENTER">
<IMG ALT="" WIDTH="470" HEIGHT="10" SRC="images/dotrule1.gif"></P>
<P ALIGN="CENTER">
<div class="footer">
<BR>SWISH-E is distributed with <B>no warranty</B> under the terms of the
<A HREF="http://www.fsf.org/copyleft/gpl.html">GNU Public License</A>,<BR>
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA<BR>
Public questions may be posted to
the <A HREF="http://swish-e.org/Discussion/">SWISH-E Discussion</A>.
</div>
</body>
</html>
|