Here's how to do a Booleen OR Search in S-Mart

greenspun.com : LUSENET : S-Mart Shopping Cart : One Thread

After a lot of thinking and playing around with different ideas I finally figured out how to get the S-mart shopping cart script to do a Booleen-type OR search.

Ie: Currently when a customer types in the words BIG RED BOX the search would only find a match if there was a product with the EXACT name of BIG RED BOX. What if the product was actually named LARGE RED BOX? The user would not get any hits on their product search. I was finding on my site at least that users were typing in several words like they would do in a traditional search engine and would not find the product they were looking for. In my online store I've got over 5,400 products so it was not as though the user could just easily browse through the find the product they wanted.

So, I figured out a way to make the script do an OR search instead of the exact terms in the exact order the user specified.

Ie: If the user types in BIG RED BOX it will pull up ANY items that have either the word BIG or RED or BOX. It's not a perfect system, but it's a little bit closer to what I'm looking for.

Basically to make this work all you need to do is add three lines to the script like I've shown below.

First, find the line that says - elsif ($type eq 'search') { then add the following:

elsif ($type eq 'search') { ADD LINE ---> @search = split(/\s+/, $FORM{'search'}); my($matches) = 0; my($i) = $pos; while (($matches < $numtolist) && ($i < $SIZE)) { ADD LINE ---> foreach $search(@search) { if (index(lc $LINES[$i],lc $search) >= 0) { $matches++; my(%ITEM); ($ITEM{'itemid'}, $ITEM{'name'}, $ITEM{'price'}, $ITEM{'descrip'}, $ITEM{'image'}, $ITEM{'weight'}, $ITEM{'itemurl'}, $ITEM{'group'}) = split(/\|/,$LINES[$i]); &print_item(\%ITEM); } ADD LINE ---> } $i++; } print "\n";

------------------------------------- That's all there is to making it work! It's not a perfect system yet but I'm still working on it. I am going to try to implement a system where the user can specify either EXACT PHRASE match or ANY WORD match. This is fairly easy to do first the first page but gets a little bit more in-depth when it needs to work for all subsequent pages viewed after the 20 item maximum (or whatever number you have it set to). I'll be working on this portion as well and will send the update once I have it complete and tested.

Eventually, I hope to totally re-write the search process so that it functions more like a typical search engine like Yahoo or Excite but am still learning this stuff as I go along and am not sure how to do it quite yet!

Sometimes this message board scrambles code that is posted so if you want a copy of this message e-mailed to you drop me a note at bppilot@aol.com.

Any suggestions / comments are appreciated.

Hope this helps someone out!

-BP

-- BP (bppilot@aol.com), November 13, 1998

Answers

I'd just like to say that you have done a lot work! I want to thank you for posting this message on the board. I plan to use the script modifications that you posted for my own script. Thanks!

Sincerely,

JS

-- Jeremy Smith (pancakepan@usa.net), November 13, 1998.


In my original post I forgot to say that one of the lines of the code also needs to be changed besides just adding those other three lines of additional text. The variable in the one line which is currently $FORM{'search'} needs to be changed to $search. Here's the correct code with the change noted:

------------------------------------- elsif ($type eq 'search') { ADD LINE ---> @search = split(/\s+/, $FORM{'search'}); my($matches) = 0; my($i) = $pos; while (($matches < $numtolist) && ($i < $SIZE)) { ADD LINE ---> foreach $search(@search) { CHANGE THIS LINE TO -----> if (index(lc $LINES[$i],lc $search) >= 0) { $matches++; my(%ITEM); ($ITEM{'itemid'}, $ITEM{'name'}, $ITEM {'price'}, $ITEM{'descrip'}, $ITEM{'image'}, $ITEM{'weight'}, $ITEM {'itemurl'}, $ITEM{'group'}) = split(/\|/,$LINES[$i]); &print_item(\%ITEM); } ADD LINE ---> } $i++; } print "\n";

-------------------------------------

Using this format everything should work fine for you. Let me know if you have any problems.

-BP bppilot@aol.com

-- BP (bppilot@aol.com), November 14, 1998.


I was wondering how you would do a Boolean AND search? I want it so if my customer types in something like BIG RED BOX, that if it was RED BIG BOX or BOX BIG RED in my catalog, it would show up on the search results. Any help with this is appreciated.

Sincerely, JS

-- Jeremy Smith (pancakepan@usa.net), November 18, 1998.


JS - The S-mart script generates the search result pages on the fly so doing a full AND search is a little bit more complicated of an issue. If I can figure out how to send the results to some type of database and extract them from there it might work. This will probably be my next "project" and I will post the results if I'm able to figure it out.

-BP

-- bp (bppilot@aol.com), November 19, 1998.


Moderation questions? read the FAQ