Enhance Keyword Search on Product Name and omit limitation of search by consecutive word

9 months ago
Problem:
If I have two product:
Product 01 Name: $50 Physical Gift Card
Product 02 Name: $100 Physical Gift Card
Then, Search by gift card give me both products in search result, but if I search by physical card then no product returns in search result. That means if I don't provide consecutive word of product name the search on product name doesn't work.
Here are some forum post point to this:
1. https://www.nopcommerce.com/en/boards/topic/92804/search-box-finds-consecutive-wording-for-multiple-words-only-can-this-be-changed#291257
2. https://www.nopcommerce.com/en/boards/topic/92462/v440-help-needed-trying-to-modify-searchproducts#289985

Proposed Solution:
I would suggest to include exclusive product name search.
An additional method,
private IQueryable<int> ExclusiveKeyWordSearch(string keywords)
{
  var products = _productRepository.Table;
  var searchstrings = keywords.Split(null).Select(x => x.ToLower()).ToArray();

  foreach (string q in searchstrings)
  {
    products = products.Where(p => p.Name.ToLower().Contains(q));
  }

  return products.Select(p=> p.Id);
}

Then on ProductService, keep existing keywords search as it is and include this as an addition.
IQueryable<int> productsByKeywords;

                productsByKeywords =
                        from p in _productRepository.Table
                        where p.Name.Contains(keywords) ||
                            (searchDescriptions &&
                                (p.ShortDescription.Contains(keywords) || p.FullDescription.Contains(keywords))) ||
                            (searchManufacturerPartNumber && p.ManufacturerPartNumber == keywords) ||
                            (searchSku && p.Sku == keywords)
                        select p.Id;

               productsByKeywords = productsByKeywords.Union(ExclusiveKeyWordSearch(keywords));

On the ExclusiveKeyWordSearch method I think it's better to skip search on product Descriptions, as this might create performance issue.
Also, I would recommend to make it configurable. As it might impact the search performance, admin can decide whether they want to enable it or not!

The outcome of this modification is:
9 months ago
Thanks for your suggestion and proposed solution, here is a work item for this.
9 months ago
Thanks for the consideration.
4 months ago
i have tried ur solution, but it works only partially, i dont know what i did wrong.

in product.cs i have added this (bold one)
public IQueryable<int> ExclusiveKeyWordSearch(string keywords)
        {
            var products = _productRepository.Table;
            var searchstrings = keywords.Split(null).Select(x => x.ToLower()).ToArray();

            foreach (string q in searchstrings)
            {
                products = products.Where(p => p.Name.ToLower().Contains(q));
            }

            return products.Select(p => p.Id);
        }

        public virtual async Task<IPagedList<Product>> SearchProductsAsync(
            int pageIndex = 0,
            int pageSize = int.MaxValue,
            IList<int> categoryIds = null,


and then (bold is what i added)
 //Set a flag which will to points need to search in localized properties. If showHidden doesn't set to true should be at least two published languages.
                var searchLocalizedValue = languageId > 0 && langs.Count >= 2 && (showHidden || langs.Count(l => l.Published) >= 2);

                IQueryable<int> productsByKeywords;

                productsByKeywords =
                        from p in _productRepository.Table
                        where p.Name.Contains(keywords) ||
                            (searchDescriptions &&
                                (p.ShortDescription.Contains(keywords) || p.FullDescription.Contains(keywords))) ||
                            (searchManufacturerPartNumber && p.ManufacturerPartNumber == keywords) ||
                            (searchSku && p.Sku == keywords)
                        select p.Id;

                productsByKeywords = productsByKeywords.Union(ExclusiveKeyWordSearch(keywords));

                //search by SKU for ProductAttributeCombination
                if (searchSku)
                {
                    productsByKeywords = productsByKeywords.Union(
                        from pac in _productAttributeCombinationRepository.Table
                        where pac.Sku == keywords
                        select pac.ProductId);
                }


it works in the drop down search, but when i hit search, it gives no result, like the 2 searches are separate.
can u help me pls?
4 months ago
noffy wrote:

it works in the drop down search, but when i hit search, it gives no result, like the 2 searches are separate.

i just checked with nopCommerce 4.50. everything works fine as expected.


i think any 3rd party plugin has modified your service while searching. can you give a try with default project?
4 months ago
don't have any third party plugin, don't know what to do.
the strange thing is that the drop down search works fine, but if i hit search, it returns search.noresultstext

any other ideas? mabye i have to put the modified code somewhere else?
2 months ago
noffy wrote:
don't have any third party plugin, don't know what to do.
the strange thing is that the drop down search works fine, but if i hit search, it returns search.noresultstext

any other ideas? mabye i have to put the modified code somewhere else?

can you please share your site URL to check?