freento/fast-search-autocomplete

There is no license information available for the latest version (1.0.0) of this package.

Freento Fast Search Autocomplete

1.0.0 2023-09-01 10:36 UTC

This package is auto-updated.

Last update: 2024-10-05 11:40:54 UTC


README

Magento's autocomplete feature works quite slowly. Users expect almost instant responses when entering text in the search input. Additionally, Magento does not display products as a result of search queries.

Module Goal - Achieve near-instant input, around 50ms.

Additional goal - display products as a result of search queries.

Plan to achieve the goals:

  • Abandon auxiliary SQL queries, implement only with Elastic.
  • Avoid additional network interactions (Redis, SQL).
  • Avoid Magento bootstrap.

Installation

  1. Install module: composer require freento/fast-search-autocomplete
  2. Run magento commands:
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:clean
  1. To populate the attribute required for operation take an action of your choice:
    1. Run CLI command bin/magento freento:fsa:fillname.
    2. Wait for the "freento_fastsearchautocomplete_fill_original_name" cron job execution (it runs once a day).
  2. Reindex or wait for Magento to do it automatically.

Module Operation Modes

Fast Mode (Direct Elastic)

All Magento AJAX controller queries are quite slow because they do not trigger FPC and:

  1. Go through Magento bootstrap, composer autoload.
  2. Require caching data retrieval from Redis or the file system.
  3. Perform about a dozen MySQL queries.

For example, the simplest native Magento search query for autocomplete makes about 30 Redis and 10 MySQL queries and takes 135 milliseconds.

Our module has a Fast mode that makes a direct request to Elasticsearch without invoking Magento's bootstrap. This is achieved by uploading a simple script to the server's pub folder responsible for high-speed operation. The response from Magento comes in tens of milliseconds.

Demo: Fast Search Autocomplete Demo

Basic module setup:

  1. Switch the module to "Direct Elastic" mode. (Stores > Configuration > Freento > Fast Search Autocomplete > General > Mode)
  2. Generate a configuration file. (Stores > Configuration > Freento > Fast Search Autocomplete > General > Generate Search Configuration File)
  3. Copy the basic file from the misc folder of the extension to the pub folder (pub/search_result.php), which does not bootstrap Magento but works directly with Elasticsearch.
  4. Adjust the server configuration to allow the module to access the file from step 3. Example from nginx.conf:
...
# PHP entry point for main application
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|search_result)\.php$ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
...

Native Magento mode

It operates similarly to the "Direct Elastic" mode but involves Magento's bootstrap, which is not very fast and also makes additional MySQL and Redis queries. To switch to this mode, simply select "Native Magento" under Stores - Configuration - Fast Search Autocomplete.

Technical Notes

Attribute

Since Elasticsearch stores names for configurable and bundle products as a string containing the names of all included simples, a separate attribute was added to display normal product names in the results. This attribute is filled in when a product is saved or through the cron job freento_fastsearchautocomplete_fill_original_name or by running the CLI command

bin/magento freento:fsa:fillname.

Without filling this attribute, the search will not work correctly.

Redirect to Product Page

To navigate to a product page, the product's ID is used instead of a direct link. When a result is clicked, it redirects to a controller where the product's page URL is generated based on the product's ID (or a noroute page if the product cannot be found by ID), and then redirects to the correct page.

Fast mode specifics

The search occurs in the pub/search_result.php file. If you need more flexible rules for the Elastic request, you can modify the file, but usually it is not required and autogenerated file is fine. Magento's indexer does not store product URLs. In the popup, there are links to an intermediate controller, and upon navigating to it, the correct product URL is determined, and a redirect to the correct page occurs.

The file uses a generated config file with the following fields. If there are any errors when connecting to Elasticsearch, check this file (var/freento-elastic-config.json).

Configuration file sample:
{
    "hostname": "localhost",
    "port": "9200",
    "index": "magento2",
    "enableAuth": "0",
    "username": "",
    "password": "",
    "timeout": "15",
    "engine": "opensearch",
    "max_results_count": "10"
}