Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making Perl Script Make Two Calls to Search Engine

Status
Not open for further replies.

kyoko

Technical User
Mar 3, 2000
4
0
0
US
I have a perl script that parses an index for search results.<br><br>Is there a way to make the script make a call to two separate indices and display the results as one search.&nbsp;&nbsp;I want to avoid merging the indices, but instead want to&nbsp;&nbsp;have a divider between the two sets of results.&nbsp;&nbsp;( I have done this with another script, but this one eludes me.)<br><br><br>The script:<br><br><br>#!/usr/local/bin/perl<br><br># REQUIRED<br><br># Firstly check the path to PERL executable in the first line of the script.<br><br>$swish_path =&nbsp;&nbsp;'/path/to/swish-e'; # path to SWISH executable<br><br><br># You should carefully set configuration options in your `swish.conf' file.<br># Search imports `IndexName' and `IndexPointer' values and<br># its behaviour considerably depends on `ReplaceRules' settings.<br><br>#################################################################################<br><br># YOUR CHOICE<br><br>%form = ( 'ipath', '/path/index.swish');#default `Search path'<br><br><br>$bgcolor =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&quot;#ffffff&quot;'; # background color<br>$textcolor =&nbsp;&nbsp;&nbsp;'black'; # text color on `bgcolor' background<br>$atextcolor =&nbsp;&nbsp;'white'; # text color on `linkcolor' background and<br> # activ link color<br>$headercolor = 'maroon'; # headers color<br>$linkcolor =&nbsp;&nbsp;&nbsp;'blue'; # links color and alternate background<br>$alinkcolor =&nbsp;&nbsp;'lime'; # links color on `linkcolor' background<br>$vlinkcolor =&nbsp;&nbsp;'navy'; # visited links color<br><br>#################################################################################<br><br># YOU SHOULD NOT ALTER THE REST OF THE SCRIPT<br><br>read( STDIN, $buffer, $ENV{ 'CONTENT_LENGTH' } );<br>@pairs = split( /&/, $buffer );<br><br>foreach $pair( @pairs )<br>{<br>&nbsp;&nbsp;( $name, $value ) = split( /=/, $pair );<br><br>&nbsp;&nbsp;$value =~ tr/+/ /;<br>&nbsp;&nbsp;$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg;<br><br>&nbsp;&nbsp;$form{ $name } = $value;<br>}<br><br>open( RES, ( $form{ 'terms' } ) ?<br>&nbsp;&nbsp;&quot;$swish_path -f $form{ 'ipath' } -m 50 -t HBthe -w $form{ 'terms' } ¦&quot; :<br>&nbsp;&nbsp;&quot;$swish_path -f $form{ 'ipath' } -m 1 -w 0 ¦&quot; );<br><br>_1: while( &lt;RES&gt; )<br>{<br>&nbsp;&nbsp;$Name = $1 if /^# Name: (.*)$/o;<br>&nbsp;&nbsp;$Pointer = $1 if /^# Pointer: (.*)$/o;<br>&nbsp;&nbsp;last _1 if defined( $Name ) && defined( $Pointer );<br>&nbsp;&nbsp;$error = $1 if /^err: (.*)$/o;<br>&nbsp;&nbsp;$error = 'Invalid search words' if /^options:/o;<br>}<br><br>$hc = &quot;FONT COLOR=$headercolor&quot;;<br>$atc = &quot;FONT COLOR=$atextcolor&quot;;<br>$alc = &quot;FONT COLOR=$alinkcolor&quot;;<br><br>$_ = &lt;&lt;EOP;<br>Content-type: text/html<br><br>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 3.2//EN&quot;&gt;<br>&lt;html&gt;<br><br>&lt;head&gt;<br>&lt;title&gt;&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;/head&gt;<br>&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;&gt;<br>&nbsp;&nbsp;&lt;tr&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;50%&quot; valign=&quot;top&quot;&gt;&lt;img SRC=&quot;<A HREF=" TARGET="_new"> ALT=&quot; &quot; width=&quot;442&quot; height=&quot;103&quot; BORDER=&quot;0&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;50%&quot; style=&quot;padding-left: 0&quot; bordercolor=&quot;#808080&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p align=&quot;center&quot;&gt;&lt;font size=&quot;5&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;br&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/i&gt;&lt;/b&gt;&lt;/font&gt;&lt;b&gt;&lt;font face=&quot;Arial&quot;&gt;Tell Your Suppliers You Found Them on<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PetroPages&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;<br>&nbsp;&nbsp;&lt;/tr&gt;<br>&lt;/table&gt;<br><br>EOP<br><br>s/VALUE=.$form{ 'ipath' }./$& CHECKED/o;<br>s/VALUE=.$form2{ 'ipath' }./$& CHECKED/o;<br>print;<br><br>if( $error )<br>{<br>&nbsp;&nbsp;print &quot;&lt;BR&gt;&lt;$hc&gt;&lt;B&gt;Error:&lt;/B&gt; $error&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;\n&quot;;<br>}<br>elsif( $form{ 'terms' } )<br>{<br>&nbsp;&nbsp;print &quot;&lt;BR&gt;&lt;$hc&gt;&lt;B&gt;Documents found:&lt;/B&gt;   &lt;SMALL&gt;(red &lt;FONT COLOR=red&gt;¶&lt;/FONT&gt; sign shows confidence in the match, best match first)&lt;/SMALL&gt;&lt;BR&gt;&lt;BR&gt;\n&quot;;<br><br>&nbsp;&nbsp;while( &lt;RES&gt; )<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;&lt;FONT SIZE=\&quot;+0\&quot; COLOR=\&quot;&quot;.( $1 &gt; 99 ? 'red' : 'black' ).<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;\&quot;&gt;&lt;B&gt;¶&lt;/B&gt;&lt;/FONT&gt; &lt;A HREF=\&quot;$2\&quot;&gt;$3&lt;/A&gt; &lt;BR&gt;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if /^([0-9]*) +([^ ]*) +.([^\&quot;]*). +([0-9]*)/o;<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;No documents found&lt;BR&gt;\n&quot; if /^err/o;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;print &quot;&lt;BR&gt;\n&quot;;<br>}<br>else<br>{<br>&nbsp;&nbsp;print &lt;&lt;EOP;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;UL&gt;<br>EOP<br>}<br><br>print &lt;&lt;EOP;<br>&lt;p align=&quot;left&quot;&gt;<br><br>*<br><br><br><br>&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;&gt;<br>&nbsp;&nbsp;&lt;tr&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;100%&quot; colspan=&quot;2&quot;&gt;&lt;font color=&quot;maroon&quot; face=&quot;Arial&quot;&gt;TEXT Display:&lt;/font&gt; &lt;/td&gt;<br>&nbsp;&nbsp;&lt;/tr&gt;<br>&nbsp;&lt;/table&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&lt;table&gt;<br>&nbsp;&lt;tr&gt;<br>&nbsp;&lt;td width=&quot;50%&quot;&gt;&lt;form METHOD=&quot;POST&quot; ACTION=&quot;<A HREF=" TARGET="_new"> TYPE=&quot;text&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NAME=&quot;terms&quot; SIZE=&quot;21&quot; VALUE&gt;&lt;input TYPE=&quot;submit&quot; VALUE=&quot;&nbsp;&nbsp;Product Categories &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ALIGN=&quot;top&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style=&quot;background-color: #000000; color: #FFFFFF; font-family: sans-serif; font-size: 10pt; font-weight: normal; background-image: url('/images/DoSearch.jpg')&quot;&gt;&lt;input TYPE=&quot;text&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NAME=&quot;terms&quot; SIZE=&quot;21&quot; VALUE&gt;&lt;font COLOR=&quot;blue&quot;&gt;&lt;input TYPE=&quot;submit&quot; VALUE=&quot;&nbsp;&nbsp;Product Categories &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ALIGN=&quot;top&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style=&quot;background-color: #000000; color: #FFFFFF; font-family: sans-serif; font-size: 10pt; font-weight: normal; background-image: url('/images/DoSearch.jpg')&quot;&gt;&lt;/td&gt;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&lt;/tr&gt;<br>&lt;/table&gt;<br><br><br>EOP<br><br>__END__<br>
 
a not very sophisticated approach would be simply duplicate the sections which query the index and print the results.&nbsp;&nbsp;A better approach would be to catch, from the calling process, a list of indeces to be queried and loop through them.&nbsp;&nbsp;This above code is fairly straight forward.&nbsp;&nbsp;Consequently, I will assume you are new to PERL (or you would not be asking).&nbsp;&nbsp;So, here are a few notes about the code.<br><br><br># This opens a pipe or handle to another process which&nbsp;&nbsp;querys your index.<br># roughly translated......<br># open HANDLE, someName, if one of these following things works (returns non-zero)<FONT FACE=monospace><br>open( RES, ( $form{ 'terms' } ) ?<br>&nbsp;&nbsp;&quot;$swish_path -f $form{ 'ipath' } -m 50 -t HBthe -w $form{ 'terms' } ¦&quot; :<br>&nbsp;&nbsp;&quot;$swish_path -f $form{ 'ipath' } -m 1 -w 0 ¦&quot; );</font><br><br>#&nbsp;&nbsp;the 'while (&lt;RES&gt;)' is listening to the handle opened above<br># while it gets stuff from the index via the pipe/handle 'RES', catch some stuff<br># or jump out of the while.<FONT FACE=monospace><br>_1: while( &lt;RES&gt; )<br>{<br>&nbsp;&nbsp;$Name = $1 if /^# Name: (.*)$/o;<br>&nbsp;&nbsp;$Pointer = $1 if /^# Pointer: (.*)$/o;<br>&nbsp;</font># bail out if you have name&nbsp;&nbsp;and pointer<FONT FACE=monospace><br>&nbsp;&nbsp;last _1 if defined( $Name ) && defined( $Pointer ); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;$error = $1 if /^err: (.*)$/o;<br>&nbsp;&nbsp;$error = 'Invalid search words' if /^options:/o;<br>}</font><br><br># while you get results, in '$_', back from the opened handle (RES)<br># print them to the client via STDOUT<br># note that the conditional statement follows the action taken<br># if the condition is true.&nbsp;&nbsp;PERL lets you turn this around and it can<br># be confusing if you are not used to looking at it.<br><FONT FACE=monospace>&nbsp;&nbsp;while( &lt;RES&gt; )<br>&nbsp;&nbsp;{ <br>&nbsp;&nbsp;&nbsp;&nbsp;# print contents of '$_' with a little tweaking on the fly<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;&lt;FONT SIZE=\&quot;+0\&quot; COLOR=\&quot;&quot;.( $1 &gt; 99 ? 'red' : 'black' ).<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;\&quot;&gt;&lt;B&gt;¶&lt;/B&gt;&lt;/FONT&gt; &lt;A HREF=\&quot;$2\&quot;&gt;$3&lt;/A&gt; &lt;BR&gt;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if /^([0-9]*) +([^ ]*) +.([^\&quot;]*). +([0-9]*)/o;<br> # print error statement&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if error is returned.<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;No documents found&lt;BR&gt;\n&quot; if /^err/o;<br>&nbsp;&nbsp;}</font><br><br><br>close RES; # do this prior to next iteration<br><br># do next iteration and then print the end stuff for the html.
 
Thanks!&nbsp;&nbsp;I got it figured out.<br>&nbsp;%form=('ipath1', '/path/index', 'ipath2', '/path/index);<br><br>Plus duplicating the sections.&nbsp;&nbsp;That was what I wanted, even if apparently unsophisticated.<br><br>But out of curiosity, can you give me a clue as what this is about? <br><br>&quot;A better approach would be to catch, from the calling process, a list of indeces to be queried and loop through them.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top