Magento2: Search weight not changing relevance of results

Created on 28 Sep 2016  Â·  37Comments  Â·  Source: magento/magento2

Magento version 2.1.1

Steps to reproduce

  1. Set name product attribute to search weight 10
  2. Set short_description product attribute to search weight 1
  3. reindex the search php bin/magento indexer:reindex, clear the cache
  4. Search for a specific product by name i.e. "Test Product Name"

    Expected result

  1. the most relevant product should be first in the list i.e. the product with name "Test Product Name"
  2. subsequent less relevant products should follow, products with a partial match in the name i.e. the product "Test Product" would come before the product "Test", then products with a full/partial match in the description field.

Actual result

  1. as per screenshot, you can see the first product doesn't have the word optimal in the name field. The product does have the word in the description field, but this is the opposite of how it should work.
    image
Search Fixed in 2.2.x Format is not valid needs update bug report

Most helpful comment

I have added order by score code after the search_result join on the collection file. Now it is working fine
vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php

    $this->getSelect()->joinInner(
        [
            'search_result' => $table->getName(),
        ],
        'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID,
        []
    );

     //sort relevance based on attribute weight
        if( isset($_GET['product_list_dir']) ){
            $_dir = $_GET['product_list_dir'];
        }else{
            $_dir = 'Desc';
        }

        if ( !isset($_GET['product_list_order']) || ($_GET['product_list_order'] === 'relevance') ) {
                $this->getSelect()->order('score '.$_dir);
        }
     //sort relevance based on attribute weight

All 37 comments

This is a known issue MAGETWO-56114, thanks for reporting

Do you have any idea when there will be a fix for this? Is there a workaround i can implement in the interim, or shall we look to purchase an extension to improve the standard search?

We're hoping to launch the site we're working on in a few weeks so it's important our search is working before then.

Thanks,

Sophie

@sma09sjb maybe you could give a try to our search engine implementation which features a better relevance than the standard Magento engine : https://github.com/Smile-SA/elasticsuite

Hello @sma09sjb - Which search engine adapter are you using?

For the Solr adapter (aka 56114 internally) we won't fix; you can adjust weighting via Solr configuration file.

For the elastic search adapter we've verified that's a bug and are tracking internally as (MAGETWO-58964) and putting it on the list for back port to 2.1.x

Hi,

I'm using the default MySQL adapter.

Thanks,

Sophie

Hi,

This is becoming a real problem for our client as the search is one of the key features that will determine the success of their site (and sales figures) in the first few weeks of launch. Our client has a large catalog and it's imperative we can accurately weight search results, and that the most relevant products are listed first. Can we get some idea of when this will be fixed and when the release will be?

We've also looked at using an extension to improve the search but this has an associated cost that we hadn't anticipated or feel we should reasonably afford given that the weighting features we require are already in situ.

These are the extensions we've looked at, both of them requiring an installation of Sphinx on our server.
http://www.mageworx.com/magento-2-search-extension.html
https://mirasvit.com/magento-2-extensions/magento-sphinx-search.html

@romainruaud unfortunately the elasticsuite you mention has a number of features we already have extensions for. We only require an extension that improves the relevance of the search results, rather than the layered navigation etc.

Does anyone have any further suggestions that could resolve this problem?

Hi @sma09sjb I cannot reproduce this issue as described
Can you provide file for product import that allow reproduce problem?.

I imported you file on clean magento 2.1.1 and repeat steps. In result I receive correct products sort order(see attach)
searchoptimal

Also I want to notice that on your screenshot sort order is reverse to default

Could you try reproduce issue on clean magento instance without custom theme and extensions

The sort button doesn't change the order of the results on our installation, the order is always the same regardless.

When we search Vitamin, we're also getting C Compete Powder as the first product in the list which is wrong, the first product should be the product with Vitamin in the title.

I'll try and replicate this on a clean installation as you suggest and get back to you.

I'm not convinced there is no issue with the way the search relevance is working by default,

i'm looking at test inbstallation here http://magento2.demo.ubertheme.com/catalogsearch/result/?q=bag and searching for "bag" and getting irrelevant results at the top of the list?

Can you please test the results that appear when you search "Liposomal Vitamin C" because again we're getting C Complete powder at the top of the list?

Hi,

is there any update on this?

Thanks,

Sophie

Can confirm the same issue.

I have a clean installation of 2.1.0, when I search for "abc 999" here as you can see the product with both words was on the second: http://m2-demo.mgt4.iggo.fi/catalogsearch/result/?q=abc+999

And it's second because there're not so many products, if there're many and that product was created late (=big product id), I'd bet it would be in a very late position. Because we're facing this problem in our real project, product which matches the best was put on the 4th/5th page in the search result, this is quite annoying.

I have the same problem. Magento 2.12 :-(

I ran into this same issue. It turns out, Magento uses the MySQL full text search functionality which has a default minimum word length 4 characters. This means that anything less than that is not at all considered in the search.

You can update this value in your mysql configuration (ft_min_word_len) and then rebuild the index. I set mine to 2 and get much better results.

I'm getting the same issue with Magento 2.1.5. The SortOrder of the search seems to be ignored.

IE this:

V1/products?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][condition_type]=like&searchCriteria[filter_groups][0][filters][0][value]=%hockey%&searchCriteria[current_page]=1&searchCriteria[page_size]=24&searchCriteria[sortOrders][0][field]=search_weight&searchCriteria[sortOrders][0][direction]=DESC

Returns the same as this:

V1/products?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][condition_type]=like&searchCriteria[filter_groups][0][filters][0][value]=%hockey%&searchCriteria[current_page]=1&searchCriteria[page_size]=24&searchCriteria[sortOrders][0][field]=search_weight&searchCriteria[sortOrders][0][direction]=ASC

Does anyone know any solutions to this?

HI @choukalos

Any update for EE elastic search ? It seems not working

Same problem. Magento Version EE 2.1.2

Same issue with Magento Version EE 2.1.8, with Elastic Search

Coming to report this exact issue in 2.1.9 CE.
Search results come in no distinguishable order.
Setting search weight Name to 10 and Description to 1 still returns mix of Name and Description matches.
Using the sort dropdown does nothing but refresh the page.

There's a bug that skips the order by clause when sorting by relevance. If I recall, the code checks for the method of ordering before it actually gets set. I fixed it in my store and reported it over a year ago. It may still be the same issue.

Me too...how to solve

You need to replace the function "buildQueries" in class "MagentoElasticsearchSearchAdapterQueryBuilderMatch"

Change it to the following. I've added a match "100%", I see more relevant results. Let me know if it does not work.

/**
 * Added boost and Match 100%
 *
 * @param array $matches
 * @param array $queryValue
 * @return array
 */
protected function buildQueries($matches, $queryValue)
{
    $conditions = [];
    foreach ($matches as $match) {

        $resolvedField = $this->fieldMapper->getFieldName(
            $match['field'],
            ['type' => FieldMapperInterface::TYPE_QUERY]
        );

        $conditions[] = [
            'condition' => $queryValue['condition'],
            'body' => [
                'match' => [
                    $resolvedField => [
                        'query' => $queryValue['value'],
                        'boost' => isset($match['boost']) ? $match['boost'] : 1,
                        "minimum_should_match" => "100%"
                    ],
                ],
            ],
        ];
    }

    return $conditions;
}

@kervin I believe that might fix it for exact matches - but in my instance I need to match partial skus. Example: I have several SKUs: ABC-VIL, ABC-BEC, ABC-TE2. The 100% matching doesn't work if the search query is "ABC". If there's a name with ABC-VIL but the sku is 34932, it can show up second. That's with SKU with a search weight of 1 and name/description with a weight of 10. This happens commonly when the product is an accessory of ABC-VIL and mentions the SKU in the name.

Magento EE 2.2.4
Elasticsearch 5.0

This issue should be reopened.

Hi @jonshipman
As of Magento 2.2 – Elasticsearch module is only included in EE. This issue tracker only relates to CE issues. Please refer to the enterprise support.

Thank you.

I have added order by score code after the search_result join on the collection file. Now it is working fine
vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php

    $this->getSelect()->joinInner(
        [
            'search_result' => $table->getName(),
        ],
        'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID,
        []
    );

     //sort relevance based on attribute weight
        if( isset($_GET['product_list_dir']) ){
            $_dir = $_GET['product_list_dir'];
        }else{
            $_dir = 'Desc';
        }

        if ( !isset($_GET['product_list_order']) || ($_GET['product_list_order'] === 'relevance') ) {
                $this->getSelect()->order('score '.$_dir);
        }
     //sort relevance based on attribute weight

@magento-engcom-team What commit fixed this in 2.2? I see it was done in the 2.3 branch but there's not reference to 2.2

HI @Ctucker9233 this already fixed in 2.3-develop branch and will be available on 2.2.8 release, but i'm unable to find relevant commit.

Any update on this?? I don't understand why it keeps getting closed.
I'm still having this issue on M2.3.1 with either MySQL or ElasticSearch 6.
If a user searches for 'skirt', the first results are tops that mention skirts in the description, even though weights for name and description fields are set correctly.
The search feature is pretty useless and is utterly counter-intuitive for our users.

@jaxtheking Completely agree.

HI @Ctucker9233 this already fixed in 2.3-develop branch and will be available on 2.2.8 release, but i'm unable to find relevant commit.

Was this fixed in 2.2.8? The search behavior is still the same on 2.2.9.

I have also the same issue in version CE 2.3.1. Weight is not being considered for relavance.

Hello @hackrat
I can't reproduce this issue on a fresh Magento 2.3-develop.
Manual testing scenario:

  1. Set name product attribute to search weight 10
  2. Set short_description product attribute to search weight 1
  3. reindex the search php bin/magento indexer:reindex, clear the cache
  4. Create product with names: "relevant first",relevant second",relevant third"(
    in the same order)
  5. Search for a specific product by name i.e. "relevant"

Actual result:
The most relevant product is first in the list
image

So i have to close this issue.

Sorry @engcom-Charlie, but how does your test demonstrate the weights on different fields actually work?? You've only searched for a single word that is only present in the title - you would've have gotten the same result even if the weight of the title was 1...

Could you at least try a more appropriate scenario? Like creating:

  1. a product with 'skirt slim fit' in the title
  2. a product with 'italian style skirt' in the title
  3. a product with 'blue top' in the title and 'goes well with a skirt' in the description
    and see if you get product 1 & 2 first by searching for 'skirt'.

Also set the description weight to 10 and title to 1 and see if you get product 3 first by searching for 'skirt'.

If you got the expected output, then it'd be fair to close this ticket... I personally don't on my site, but then again my catalogue is a little more complex than 3 products with just a title.

Hello @jaxtheking
My result with your testing scenario:
image

In my case, Mageplaza_LayeredNavigation module was creating issue with search relavance based on weight. I have disabled and search is working based on weight.

@hackrat The GitHub issue tracker is intended for Magento Core technical issues only(Mageplaza is not).

Was this page helpful?
0 / 5 - 0 ratings