1

I am trying to customize the default Magento 2 Elasticsearch 6. Right now the default Fulltext search is not giving that good result.

For Example I have product name like ADB12355 BBB, ADB3456 AAA, etc.

I am getting proper results either when adding ADB12355 BBB or ADB3456. The problem is I also want both the products if someone just search for ADB some kind of LIKE in mysql.

Also My configuration is correct and indexes are created but So far I am not able to find the correct place to customize.

1
  • go to marketing -> seo and search -> search synonyms, there you can add synonyms for the adb search that will include the product names returned by search. Also ensure the name attribute is enabled for search. – paj Mar 17 '20 at 21:40
3

Give a try.

Add plugin on the below class Builder.php by making a sample module.

File: app/code/Custom/LikeSearch/etc/di.xml

<type name="Magento\Elasticsearch\Model\Adapter\Index\Builder">
        <plugin name="custom_like_search_builder" type="Custom\LikeSearch\Plugin\Model\Adapter\Index\CustomBuilder"></plugin>
</type>

File: Custom\LikeSearch\Plugin\Model\Adapter\Index\CustomBuilder.php

<?php
namespace Custom\LikeSearch\Plugin\Model\Adapter\Index;

class CustomBuilder {

    public function afterBuild(\Magento\Elasticsearch\Model\Adapter\Index\Builder $subject, $result)
    {
        $likeToken = $this->getLikeTokenizer();
        $result['analysis']['tokenizer'] = $likeToken;
        $result['analysis']['filter']['trigrams_filter'] = [
            'type' => 'ngram',
            'min_gram' => 3,
            'max_gram' => 3
        ];
        $result['analysis']['analyzer']['my_analyzer'] = [
            'type' => 'custom',
            'tokenizer' => 'standard',
            'filter' => [
                'lowercase', 'trigrams_filter'
            ]
        ];
        return $result;
    }


    protected function getLikeTokenizer()
    {
        $tokenizer = [
            'default_tokenizer' => [
                'type' => 'ngram'
            ],
        ];
        return $tokenizer;
    }
}

Once done perform the php bin/magento indexer:reindex catalogsearch_fulltext

It should give results like below:

For search term, ADB gives results of below products names

ADB12355 BBB

ADB3456 AAA

For search term, GIT gives results of below products names

ABCGIT AAA

XYZGIT BBB

4
  • 1
    I can confirm changing the ngram min/max values to 3 greatly improves this sort of search. You can also consider increasing the search weight of the sku attribute to improve sku searches. Great answer @sandip ! – paj May 10 '20 at 10:48
  • 1
    I used this but getting all the products in search result even if I am searching any random string "shsskjkxnxs" which doesn't even exist. Please help me to achieve this. @paj – Bhupendra Bisht Feb 3 at 11:27
  • 1
    I wrote a blog post about this issue, it provides some more information blog.gaiterjones.com/… – paj Feb 3 at 13:27
  • Thanks @paj!! Great Help. – Bhupendra Bisht Feb 5 at 11:43
1

I appreciate this isn't a direct answer to the question however I'd recommend using https://github.com/Smile-SA/elasticsuite. It's open source, very customisable, and has some excellent features included such as autocomplete and search optimisers.

0

you have to set AND operator in your elastic search json queries, Override the module-elasticsearch/SearchAdapter/Query/Builder/Match.php. file in your custom module and then The code you have to edit in the build function in line no 75

if($matchKey != 'match_phrase_prefix'){$matchQuery['operator'] = "AND";}

Also make sure you are not setting the operator in match_phrase_prefix.

After this you'll get exact matches in catalog search.

This has been tested well for magento CC 2.4.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.