Magento2 Enabling Elasticsearch Empties All Categories – How to Fix

catalogsearchelasticsearchmagento2search

I installed elasticsearch and the Magento backend connection test to elasticsearch is successful.
If I save settings, clear the cache and reindex as described in the Magento devdocs, all product cateogries are empty and my search function has no result, whatever I type in there. I can still access the products with the direct url, thats all.
If I switch back to MYSQL as search engine and clear the cache all products are back.
Both Magento and Elasticsearch are Ubuntu18.04 servers, Magento 2.3.2 and Elasticsearch 6.8.3.
Any idea where I can take a deeper look or how this problem can be solved?

EDIT: Some product attributes were not enabled for search (but not all! So there must have been results before). I reindexed and now the ES shows very good results! But as before, product pages are empty.

EDIT 2: Its a Multistore. The website id 1 works with ES, all other websites have no search results – and everywhere empty categories.

Best Answer

Some issues are still with sorting when an elastic search is using as a catalog search engine. To resolve it, you can apply below patch specifically to Magento 2.3.2 and Elasticsearch 6+.

Go to the below file and add a plugin on the below class.

Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplier.php

Find apply() method and replace it with the below function. This is hotfix introduced by Magento recently by adding $this->collection->setPageSize(null); to the SearchResultApplier.php class.

    public function apply()
    {
        if (empty($this->searchResult->getItems())) {
            $this->collection->getSelect()->where('NULL');
            return;
        }
        $ids = [];
        foreach ($this->searchResult->getItems() as $item) {
            $ids[] = (int)$item->getId();
        }
        $this->collection->setPageSize(null);
        $this->collection->getSelect()->where('e.entity_id IN (?)', $ids);
        $orderList = join(',', $ids);
        $this->collection->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);
        $this->collection->getSelect()->order("FIELD(e.entity_id,$orderList)");
    }
Related Topic