3

I am using below script to fetch data from Elastic search, I want to know how the data is being fetched from Magento to ES.

This script get me product details in Output, but for some product the data is incorrect i need to check for it. I am not sure from where product is being pass to ES, so please advise me or let me know how it works actually.

require_once '../mageless/api_bootstrap.php';

$app = new LightApi();
$app->setOutput([]);

$currentEnv = 'prod';
$currentSite = 11;
$pid = preg_replace('[^0-9,]', '', $_GET['pid']);
$productIds = array_diff(array_map('intval', explode(',', $pid)), [0])
$index = 'magento2_product_11_v14';
$host = 'localhost:9200';
$query = [
    'query' => [
        'ids' => [
          'values' => $productIds
        ]
    ]
];

$ch = curl_init('http://' . $host . '/' . $index . '/_search?pretty');
echo 'http://' . $host . '/' . $index . '/_search?pretty'; exit;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     json_encode($query));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
1
2
+25

Product data builder that sends to Elasticsearch is (vendor/magento/module-elasticsearch/Model/Adapter/BatchDataMapper/ProductDataMapper.php):

Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper

Open that class and check the map method.

public function map(array $documentData, $storeId, array $context = [])
{
    $documents = [];

    foreach ($documentData as $productId => $indexData) {
        $this->builder->addField('store_id', $storeId);
        $productIndexData = $this->convertToProductData($productId, $indexData, $storeId);
        foreach ($productIndexData as $attributeCode => $value) {
            // Prepare processing attribute info
            if (strpos($attributeCode, '_value') !== false) {
                $this->builder->addField($attributeCode, $value);
                continue;
            }

            $this->builder->addField(
                $this->fieldMapper->getFieldName(
                    $attributeCode,
                    $context
                ),
                $value
            );
        }
        $documents[$productId] = $this->builder->build();
    }

    $productIds = array_keys($documentData);
    foreach ($this->additionalFieldsProvider->getFields($productIds, $storeId) as $productId => $fields) {
        $documents[$productId] = array_merge_recursive(
            $documents[$productId],
            $this->builder->addFields($fields)->build()
        );
    }

    return $documents;
}

If you print the $documents then you can see the data that you send to Elasticsearch.

10
  • How to test this ? I have created a script which has the code which i had mentioned in script. So when i run that script it output product data. if i add print_r and exit in file Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper then it can be tested with the script ? Jul 23 at 6:27
  • Magento sends products data to ES during indexing. So, You need to run "bin/magento indexer:reindex catalogsearch_fulltext" for checking data.
    – Sohel Rana
    Jul 23 at 7:58
  • ok & print_r & exit will work there ? and data will be seen in terminal ? Jul 23 at 10:07
  • Maybe, never tried. You can log that data and check from log file.
    – Sohel Rana
    Jul 23 at 10:28
  • I have added a log just before return $documents; & executed bin/magento indexer:reindex catalogsearch_fulltext Jul 23 at 10:58

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.