Post by jlg89 » Wed Mar 31, 2010 5:07 am

The default behavior of OpenCart's search function looks for the exact word or string typed into the search field. Period. If you'd rather have a more powerful search, this one's for you. Tested on OC 1.4.4 only.

With this modification, the search string is split into space- or comma- delimited substrings, and the search looks for anything containing all of the substrings. Enclosing a search string (or part of a search string) in single or double quotes treats that portion as its own substring. So, for example, if you search for chicken little it will return any item that contains both chicken and little, in any order, in any of the name, model, sku, or (optionally) description fields. If you search for "chicken little" (in quotes), it will return only items that contain the exact phrase (which is currently the default and only search action).

To enable more powerful searching:

Edit catalog/model/catalog/product.php

In the getProductsByKeyword and getTotalProductsByKeyword functions, replace the entire if ($keyword) {...} blocks (the originals are about 8 lines, I believe) with the following code:

Code: Select all

                      if ($keyword) {
                        $keywords = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $keyword, 0, PREG_SPLIT_NO_EMPTY);
                        $sql = "SELECT *, pd.name AS name, p.image, m.name AS manufacturer, ss.name AS stock, (SELECT AVG(r.rating) FROM " . DB_PREFIX . "review r WHERE p.product_id = r.product_id GROUP BY r.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "'";

                        if (!$description) {
                          foreach ( $keywords as $keywd => $kw ) {
                                $sql .= " AND (LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($kw)) . "%' OR LCASE(p.model) LIKE '%" . $this->db->escape(strtolower($kw)) . "%' OR LCASE(p.sku) LIKE '%" . $this->db->escape(strtolower($kw)) . "%')";
                          }
                        } else {
                          foreach ( $keywords as $keywd => $kw ) {
                                $sql .= " AND (LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($kw)) . "%' OR LCASE(pd.description) LIKE '%" . $this->db->escape(strtolower($kw)) . "%' OR LCASE(p.model) LIKE '%" . $this->db->escape(strtolower($kw)) . "%' OR LCASE(p.sku) LIKE '%" . $this->db->escape(strtolower($kw)) . "%')";
                          }
                        }
If you'd rather just replace the whole file, I've attached it below.

Attachments

Replacement product.php file


New member

Posts

Joined
Tue Aug 04, 2009 11:32 am

Post by Qphoria » Wed Mar 31, 2010 5:29 am

Good work. I haven't tried it, but I wondered why you chose preg_split instead of just explode ?

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by jlg89 » Wed Mar 31, 2010 5:38 am

Cuz I know exactly what I want PHP to do, I'm just not exactly sure of the best way to make PHP do it. :)

Actually, preg_split lets me split the string on more than one delimiter, treat quoted strings as one item, etc. I think explode only takes one delimiter argument.

New member

Posts

Joined
Tue Aug 04, 2009 11:32 am

Post by RonA » Sun Apr 04, 2010 1:56 pm

I tried to get this to work on 1.4.6 tonight with no luck.... (even used your product.php file) not sure what may have changed. Please let us know if you update it as the core OC search engine has very limited functionality.

Thanks anyway! ;-)

New member

Posts

Joined
Tue Mar 09, 2010 4:11 pm

Post by jlg89 » Mon Apr 05, 2010 3:11 am

Try this for 1.4.6. I haven't tested it myself, so please post here whether or not it works.

Attachments

catalog/model/catalog/product.php for 1.4.6


New member

Posts

Joined
Tue Aug 04, 2009 11:32 am

Post by RonA » Mon Apr 05, 2010 2:56 pm

Tried it out tonight Jig89 and it's still not working.

If I understand correctly, the mod should break down the search phrase into individual strings... so let's say the user enters this search phrase: candle mug.... the search engine should bring back every item with 'candle' or 'mug' in the product title.

Unfortunately, it's not returning any results.

Also... I'm not getting any results when I use quotation marks around the search phrase. If I use apostrophes, I get all kinds of results, but it doesn't make sense because the results don't have the keyword in them.

Seems weird there can be such a big difference in search functionality between 1.4.4 and 1.4.6 ???

The good news is that the mod doesn't seem to break anything... the basic search still works the way it did before I made the changes.

New member

Posts

Joined
Tue Mar 09, 2010 4:11 pm

Post by allenshea » Mon Apr 05, 2010 6:24 pm

jlg89 wrote:Try this for 1.4.6. I haven't tested it myself, so please post here whether or not it works.

this works for my shop, thanks a lot

Allen

I know nothing about PHP and SQL, but I still try my best to understand it.


Active Member

Posts

Joined
Mon Dec 14, 2009 10:01 pm

Post by jlg89 » Mon Apr 05, 2010 9:54 pm

RonA wrote: If I understand correctly, the mod should break down the search phrase into individual strings... so let's say the user enters this search phrase: candle mug.... the search engine should bring back every item with 'candle' or 'mug' in the product title.
The mod breaks down the search phrase into individual words or quote-delimited groups of words, and searches for any item that contains all of the words/groups, in any order, in any field or combination of fields. So if you don't have any items that contain both "candle" and "mug" somewhere in any of the name/model/sku/description fields, you won't get any results from this search.

New member

Posts

Joined
Tue Aug 04, 2009 11:32 am

Post by Noman » Mon Apr 05, 2010 11:32 pm

This is the best Search so far. It searches by Model, description, product name and for me, so far the best contribution to OC. This should be implemented to OC immediately.

The updated file works with 146 with no issues.

A BIG thank you sir for bringing this lovely piece of code to us!

New member

Posts

Joined
Wed Jun 24, 2009 7:44 pm

Post by Noman » Tue Apr 06, 2010 12:41 am

Now, this is cross posting ;)

Hi Guys

Is there any way to include Category and Subcategory names in text format somewhere down in Product description box?

We have a product, which belongs to many categories and subcategories [Brands and Models]. Our Search has "Search in product descriptions" enabled by default. But Search won't find anything if we don't include the full name in the Product Name

What we have now:
Acer -> Aspire 3000 - Universal 3G Laptop Modem [this modem belongs to many categories and subcats]

Search won't find anything if I try only Acer Aspire 3000.

Adding automatically Acer -> Aspire 3000 [main category and subcategory names] to the product description field in Admin would help.

I know, this one isn't easy. Thank you.

New member

Posts

Joined
Wed Jun 24, 2009 7:44 pm

Post by RonA » Tue Apr 06, 2010 6:05 am

Thanks for the clarification Jig89.... for some reason I was thinking the mod would change the 'all' keyword search to an 'any' keyword search. (Too many hours behind this computer!)

I experimented with the mod this morning and it does indeed work as advertised... bringing back results no matter which order the keywords are typed in.

But for some reason it still isn't bringing back results if I use quotation marks around my query.

Allen and/or Norman... are the quotation mark searches working for you guys in 1.4.6?

(Even if I can't get the quotation marks working, this mod provides a much smarter way of searching and I very much appreciate the work that went into it.... thank you!!)

New member

Posts

Joined
Tue Mar 09, 2010 4:11 pm

Post by powermikee » Tue Apr 06, 2010 12:10 pm

just a note on this type of search and what is already stock with opencart. Using 'LIKE %%' search can be quite slow with mysql once you get into big tables...ideally what you should use is MATCH with a full-text index this is a much faster alternative and is perfect for a commercial environment. Many websites including wikipedia use full text as their main search type and they have millions of records. With LIKE this would be impossible.

I'm optimising my search to FULL TEXT at the moment once I update the files I will post the code here :)

Newbie

Posts

Joined
Thu Mar 18, 2010 9:56 pm

Post by dramony » Fri Apr 09, 2010 3:35 pm

do you have a version for Oc 1.3.2?
Thanks!

Active Member

Posts

Joined
Sat Oct 24, 2009 12:34 pm

Post by allenshea » Mon Apr 12, 2010 9:52 am

jlg89 wrote:Try this for 1.4.6. I haven't tested it myself, so please post here whether or not it works.

The OC updated to 1.4.7, but it changed some code, I can not use this MOD in my store, Can you kindly updated it to 1.4.7? I like this MOD.

Thanks.

Allen

I know nothing about PHP and SQL, but I still try my best to understand it.


Active Member

Posts

Joined
Mon Dec 14, 2009 10:01 pm

Post by Qphoria » Mon Apr 12, 2010 11:01 am

This will definitely need to be changed for 1.4.7 as the new Product tag search has added some new lines to the normal keyword model functions

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by junstore » Mon Apr 12, 2010 12:50 pm

expecting the update can be used in 1.47

Created my future by opencart~


New member

Posts

Joined
Thu Apr 08, 2010 9:41 pm
Location - China

Post by RonA » Tue Apr 13, 2010 11:14 am

I know it's been mentioned before Q but is there a way to get this mod in the core so we don't have to worry about it working everytime there's a new version? The current OC search is primitive in comparison. (But I do like the ability to search tags! ;-)

New member

Posts

Joined
Tue Mar 09, 2010 4:11 pm

Post by Qphoria » Tue Apr 13, 2010 12:08 pm

Well I just added model search and product tag search to the core. The tag search actually uses a similar method I believe where it splits each word and searches for them individually
So "Apple Ipod" will find all words with Apple OR ipod.

But I've made a similar improved search module for keyword searching so that searching for:
"Big Red Truck" will also return things like "Big Truck" or "Big Wheel" or "Big Pony"
The issue there is that you can get too much back.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by RonA » Tue Apr 13, 2010 2:20 pm

From a customer's perspective, it's always better to return too many results than too few. (This leaves them with the impression that there is a lot to choose from and they are more likely to refine their search phrase... whereas, if their search returns too few results they get the impression you don't have what they're looking for and they'll take their search elsewhere.)

This is why I've always preferred an 'ANY KEYWORD IN ANY ORDER'' approach as opposed to an 'ALL KEYWORD IN ANY ORDER' or 'EXACT PHRASE' search. (But anything is better than a default 'exact phrase' search.)

The best scenario I think is to have a default 'ANY KEYWORD' search and then give the customer the advanced options to narrow things down with an 'ALL KEYWORD' or 'EXACT PHRASE' search.

Another thing I'd like to see (but it may be more complicated) is some 'smart' searching, so when a person types in the word 'candles', they would also get results for 'candle'.

Here's a real-world example.... my customer searches for 'cotton t-shirts'. Because the word 'cotton' is not in any of the product titles, nothing will appear. And because my product titles say 't-shirt' instead of 't-shirts' nothing will appear. So the search was pretty much useless and the customer is frustrated.

I'd actually prefer to have the default search be in titles and descriptions... but I understand this may cause server strain on a store with thousands of products. Perhaps there could be an option in the admin panel where we could choose our default method of searching - titles only or titles and descriptions.

I notice that the big boy shopping carts (like Interspire) are starting to use 'really smart' technology so that suggestions automatically pop up as the customer types in the search box. This would be the ultimate solution!!

Thanks for hearing us out Q! ;-)

~ Ron

New member

Posts

Joined
Tue Mar 09, 2010 4:11 pm

Post by jefrey1983 » Sun May 02, 2010 5:26 pm

does this work for 1.4.0? I'm having product in advanced search production description can be search

User avatar
Active Member

Posts

Joined
Sat Jan 30, 2010 6:58 pm
Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests