Elasticsearch Regular Search Returns No Results – Magento2 Configuration Guide

catalogsearchconfigurationelasticsearchmagento2server-setup

I've recently upgraded my Magento to 2.4 which necessitates the use of Elasticsearch.

Using the search function always shows the message "Your search returned no results". However using "Advanded Search" returns results as normal.

I've tested the connection as per this screenshot and there seems to be no issues.

enter image description here

I've checked another question regarding this (309450) but the solution there (turning off modules) doesn't seem to have fixed the issue.

I've also run the command below in SSL

curl http://localhost:9200/_cluster/health?pretty

This got the following result.

{
  "cluster_name" : "my-application",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 4,
  "active_shards" : 4,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 4,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 50.0
}

I have also looked in the exception log and found the following error which seems to relate to elasticsearch:

[2021-11-26 11:05:07] main.CRITICAL: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"magento2_product_1_v31","node":"r9_ZsNBiTqiP7FJDre9MmA","reason":{"type":"illegal_argument_exception","reason":"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}}],"caused_by":{"type":"illegal_argument_exception","reason":"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory.","caused_by":{"type":"illegal_argument_exception","reason":"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}}},"status":400} {"exception":"[object] (Elasticsearch\\Common\\Exceptions\\BadRequest400Exception(code: 400): {\"error\":{\"root_cause\":[{\"type\":\"illegal_argument_exception\",\"reason\":\"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory.\"}],\"type\":\"search_phase_execution_exception\",\"reason\":\"all shards failed\",\"phase\":\"query\",\"grouped\":true,\"failed_shards\":[{\"shard\":0,\"index\":\"magento2_product_1_v31\",\"node\":\"r9_ZsNBiTqiP7FJDre9MmA\",\"reason\":{\"type\":\"illegal_argument_exception\",\"reason\":\"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory.\"}}],\"caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory.\",\"caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [links_title] in order to load field data by uninverting the inverted index. Note that this can use significant memory.\"}}},\"status\":400} at /var/www/vhosts/sitename.co.uk/httpdocs/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:632)"} []
[2021-11-26 11:05:12] main.CRITICAL: {"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: field:[sku] was indexed without position data; cannot run PhraseQuery","index_uuid":"qI8SDvZQSFGVhINcjyR_cw","index":"magento2_product_1_v31"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"magento2_product_1_v31","node":"r9_ZsNBiTqiP7FJDre9MmA","reason":{"type":"query_shard_exception","reason":"failed to create query: field:[sku] was indexed without position data; cannot run PhraseQuery","index_uuid":"qI8SDvZQSFGVhINcjyR_cw","index":"magento2_product_1_v31","caused_by":{"type":"illegal_state_exception","reason":"field:[sku] was indexed without position data; cannot run PhraseQuery"}}}]},"status":400} {"exception":"[object] (Elasticsearch\\Common\\Exceptions\\BadRequest400Exception(code: 400): {\"error\":{\"root_cause\":[{\"type\":\"query_shard_exception\",\"reason\":\"failed to create query: field:[sku] was indexed without position data; cannot run PhraseQuery\",\"index_uuid\":\"qI8SDvZQSFGVhINcjyR_cw\",\"index\":\"magento2_product_1_v31\"}],\"type\":\"search_phase_execution_exception\",\"reason\":\"all shards failed\",\"phase\":\"query\",\"grouped\":true,\"failed_shards\":[{\"shard\":0,\"index\":\"magento2_product_1_v31\",\"node\":\"r9_ZsNBiTqiP7FJDre9MmA\",\"reason\":{\"type\":\"query_shard_exception\",\"reason\":\"failed to create query: field:[sku] was indexed without position data; cannot run PhraseQuery\",\"index_uuid\":\"qI8SDvZQSFGVhINcjyR_cw\",\"index\":\"magento2_product_1_v31\",\"caused_by\":{\"type\":\"illegal_state_exception\",\"reason\":\"field:[sku] was indexed without position data; cannot run PhraseQuery\"}}}]},\"status\":400} at /var/www/vhosts/sitename.co.uk/httpdocs/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:632)"} []

I'd appreciate any advice on this topic.

UPDATE: ISSUE RESOLUTION

Thanks to Edgarth for his assistance, there were a few extra steps I had to perform but he pointed me in the right direction.

Essentially I had to disable the "Search" and "filter" options for some of the product attributes (I presume that SKU was the troublesome one given the error) and run the following commands in SQL:

1:

UPDATE catalog_eav_attribute 
SET is_filterable_in_search = 0 
WHERE attribute_id IN (SELECT attribute_id FROM eav_attribute WHERE entity_type_id = 4 AND backend_type = 'varchar')

2:

UPDATE catalog_eav_attribute AS cea
INNER JOIN eav_attribute AS ea
ON ea.attribute_id = cea.attribute_id
SET cea.is_filterable = 0, cea.is_filterable_in_search = 0
WHERE (cea.is_filterable = 1 OR cea.is_filterable_in_search = 1)
AND frontend_input NOT IN ('boolean', 'multiselect', 'select', 'price');

After doing this and reindexing, the search function seems to be working properly now.

Best Answer

Check in your env.php file if you have

  'catalog' => [
            'search' => [
                'enable_eav_indexer' => '1',
                'engine' => 'elasticsearch7',
                'elasticsearch7_server_hostname' => 'elasticsearch'
            ]
        ],
Related Topic