Page 1 of 1

Adjust search function

Posted: Fri Apr 12, 2019 4:28 pm
by Jippe
Good morning all
I want to adjust the search function in such a way that one has to enter an exact word / code which is processed in a sentence of the product e.g. if you fill in canon1 then "canon1 camera" is found, if they only fill in canon then it should not be found.
Now I have been working on this for a while, but I cannot really find how and where this data can be processed / modified
Gr. Jippe

Re: Adjust search function

Posted: Fri Apr 12, 2019 8:01 pm
by D3MO
==============================================
in catalog/model/catalog/product.php FIND: (2 locations)
==============================================

$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";

==============
REPLACE WITH:
=============

$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . " %'";



What it does is it will match exact words fallowed by space if you see the difference i have added _% (_ means space)

so it will work like this: if you search canon it will return nothing (because it will be looking for "canon " (with a spacee and later all other matching words with space

if you enter canon1 it will find your canon1 camera if you search just camera it will show also canon1 camera as it contains % in the front if you want to search just for first word canon1 and not allow it to be find by camera word remove first% from query:

$implode[] = "pd.name LIKE '" . $this->db->escape($word) . " %'";

Jippe wrote:
Fri Apr 12, 2019 4:28 pm
Good morning all
I want to adjust the search function in such a way that one has to enter an exact word / code which is processed in a sentence of the product e.g. if you fill in canon1 then "canon1 camera" is found, if they only fill in canon then it should not be found.
Now I have been working on this for a while, but I cannot really find how and where this data can be processed / modified
Gr. Jippe

Re: Adjust search function

Posted: Fri Apr 12, 2019 8:32 pm
by letxobnav
you are forgetting some possibilities for exact word match:

$implode[] = "(
pd.name LIKE '" . $this->db->escape($word) . "S%' or
pd.name LIKE '%S" . $this->db->escape($word) . "' or
pd.name LIKE '%S" . $this->db->escape($word) . "S%' or
pd.name LIKE '%S" . $this->db->escape($word) . ",%' or
pd.name LIKE '" . $this->db->escape($word) . ",%' or
pd.name = '" . $this->db->escape($word) . "'
)";

S = space

Re: Adjust search function

Posted: Sun Apr 14, 2019 5:07 am
by Jippe
thanks for your reply.
I'm going to try this on Monday and let you know how it goes

Re: Adjust search function

Posted: Sun Apr 14, 2019 8:33 am
by letxobnav
you could also use:

$implode[] = "( pd.name regexp '[[:<:]]" . $this->db->escape($word) . "[[:>:]]')";

with a full text index on pd.name

Re: Adjust search function

Posted: Wed Apr 17, 2019 10:47 pm
by Jippe
good afternoon
I have now tried some things including $implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "S%'";
It has the result that I am looking for, but 2 things are not working properly.
* space shows all products this may not happen.
* when entering a number such as 1 (product_id) the corresponding product is displayed.
I think I need a letter combination such as "FRL" for the search query but just don't know how

Re: Adjust search function

Posted: Wed Apr 17, 2019 11:53 pm
by letxobnav
in catalog/controller/product/search.php you could change:

Code: Select all

		if (isset($this->request->get['search'])) {
			$search = $this->request->get['search'];
		} else {
			$search = '';
		}

to

Code: Select all

		$search = '';
		if (isset($this->request->get['search'])) {
			// replace multiple spaces with one space
			$this->request->get['search'] = trim(preg_replace('/\s+/', ' ', $this->request->get['search']));
			if ($this->request->get['search'] != ' ') {
				$search = $this->request->get['search'];
			} else {
				// space so no search
				unset ($this->request->get['search']);
			}
		}
search does not look in product id's, it will only find 1 of you have that in your product name.

Re: Adjust search function

Posted: Sat Apr 20, 2019 3:31 am
by Jippe
Good evening all
After some experimenting and combining code provided by letxobnav, I have almost found the solution.
Now I have seen that despite everything the "%" sign in the search engine shows all products. :-\
I used a variant of "= trim(preg_replace('/\s+/', ' ', $this->" to remove the space, can I also supplement it with the "%" sign?

Re: Adjust search function

Posted: Sat Apr 20, 2019 10:33 am
by letxobnav
the % sign is part of the constructed mysql "LIKE" query statement, not of your search term.

and

that regex I provided replaces multiple spaces with one space, you would not want it to remove all spaces or you have no more word separation.

Re: Adjust search function

Posted: Thu Apr 30, 2020 6:51 pm
by oliversuc
Hi all,

Please advise on my the following issue for an OC search for a technical shop.
Example: product name contains "Change" other product name contains "Exchange".

The search for "Change" returns first products with the name "Exchange" witch is not good in my case.

Code: Select all

$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
Is there a way to match this, while not limit all to the exact word?

Thank You!

Re: Adjust search function

Posted: Thu Dec 03, 2020 9:35 pm
by awaix
Hi,
Looks like this was resolved.. i have one question of my own if someone can help..
how can i make my default search to work in a way that it only search in the title with exact search instead of loose search.
i want the search to only look into titles and exactly what is typed.. for instance if someone searches for 'iphone 12' it should only show iphone 12 and not iphone 12 pro or max.
i hope someone can help me out here.
thanks.