As many of you guys know Nop_Commerce doesn't have Full-Text search ability on the Products Search. This means that when you have a product named as "Nike Barcelona Away Jersey" and you type to search "Barcelona Jersey" it will return nothing!
So...I found that implementing SQL Full-Text search was pretty easy!
I did this on a GoDaddy Windows Shared Hosting, nop 1.9, and I implemented this only on the Product Name column of the Nop_Product table since in my scenario all searches are mostly based on the product name. You could also do the same to search on the localized product name, short description, specs, etc.
Steps:
1) Connect to your DB with SQL Management Studio
2) Open a new query window
3) Paste and run the following t-SQL code:
USE DatabaseName;
GO
CREATE FULLTEXT INDEX ON dbo.Nop_Product
(Name
Language 1033)
KEY INDEX Nop_Product_PK ;
GO
Note: Language 1033 is English, search on google for other languages or how to set to default.
4) Now that we have the Full-Text enabled on that column we need to modify the SP Nop_ProductLoadAllPaged
After the line: SET @Keywords = '%' + rtrim(ltrim(@Keywords)) + '%'
Add the following:
DECLARE @FTS_Keywords nvarchar(400)
SET @FTS_Keywords = @Keywords
SET @FTS_Keywords = '"' + LTRIM(RTRIM(@FTS_Keywords)) + '*"'
-- This method cleans multiple spaces between words
SET @FTS_Keywords = REPLACE(@FTS_Keywords, ' ', ' þ')
SET @FTS_Keywords = REPLACE(@FTS_Keywords, 'þ ', '')
SET @FTS_Keywords = REPLACE(@FTS_Keywords, 'þ', '')
--SET SEARCH WILDCARDS
SET @FTS_Keywords = REPLACE(@FTS_Keywords, ' ', '*" AND "')
...at this point an input search text like 'barcelona away jersey'
is being copied to variable @FTS_Keywords and looks like ' "barcelona*" AND "away*" AND "jersey*" '
The * is to be able to type 'jersey' and get 'jerseys'
The 'AND' is to only get results that contains all parameters. If I put 'OR' instead of 'AND' when I type 'barcelona jersey' I will get all jerseys + the barcelona jerseys. (don't want that in my case)
5) Finally replace this line:
patindex(@Keywords, p.name) > 0
by:
CONTAINS(p.name, @FTS_Keywords)
6) WOALA! ...now you can search something like 'barcelona away jersey' or 'away jersey barcelona' or 'jersey barcelona away' and you'll get the results you want!
Happy coding!
Damiano