3

I am creating custom search API using elastic search functionality but can not able to create an instance of \Magento\Framework\Search\RequestInterface $requestInterface, it is giving an error like can not instantiate instance of \Magento\Framework\Search\RequestInterface here is my code

<?php 
namespace Magneto\Ecommerceapi\Controller\Search;

 use Magento\Framework\App\CsrfAwareActionInterface;
 use Magento\Framework\App\Request\InvalidRequestException;
 use Magento\Framework\App\RequestInterface;


 class Index extends \Magento\Framework\App\Action\Action implements 
CsrfAwareActionInterface{

const REQUEST_QUERY = 'amasty_elastic_query';
const HITS = 'hits';
const PRODUCTS = 'products';

/**
 * @var GetRequestQuery
 */
protected $getRequestQuery;

/**
 * @var GetResponse
 */
protected $getElasticResponse;

/**
 * @var GetAggregations
 */
protected $getAggregations;

/**
 * @var ClientRepositoryInterface
 */
protected $clientRepository;

/**
 * @var RelevanceRuleRepositoryInterface
 */
protected $relevanceRuleRepository;

/**
 * @var \Magento\Framework\Registry
 */
protected $registry;

protected $reqInterface;

protected $objectManager;

public function __construct(
    \Amasty\ElasticSearch\Model\Client\ClientRepositoryInterface $clientRepository,
    \Amasty\ElasticSearch\Model\Search\GetResponse\GetAggregations $getAggregations,
    \Amasty\ElasticSearch\Model\Search\GetRequestQuery $getRequestQuery,
    \Amasty\ElasticSearch\Model\Search\GetResponse $getElasticResponse,
    \Amasty\ElasticSearch\Api\RelevanceRuleRepositoryInterface $relevanceRuleRepository,
    \Magento\Framework\Registry $registry,

    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Search\RequestInterface $reqInterface,
    \Magento\Framework\ObjectManagerInterface $objectManager
){
    $this->getAggregations = $getAggregations;
    $this->getRequestQuery = $getRequestQuery;
    $this->getElasticResponse = $getElasticResponse;
    $this->clientRepository = $clientRepository;
    $this->relevanceRuleRepository = $relevanceRuleRepository;
    $this->registry = $registry;

    $this->reqInterface = $reqInterface;
    $this->objectManager = $objectManager;
    parent::__construct($context);
}

public function createCsrfValidationException(RequestInterface 
 $request): ?InvalidRequestException {
 return null;
}

 public function validateForCsrf(RequestInterface $request): ?bool{
 return true;
 } 
public function execute()  
{
    $productName = $_REQUEST["productName"];
    // $split = str_split($productName,5);
    // $langId = $_POST["langId"];
    // $currencyId = $_POST["curId"];
    // $customerId = $_POST["customerId"];

    $productCollection = $this->objectManager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');   //custom

    $client = $this->clientRepository->get();
    $requestQuery = $this->getRequestQuery->execute($reqInterface);
    $elasticResponse = $client->search($requestQuery);
    $elasticDocuments = isset($elasticResponse['hits']['hits']) ? $elasticResponse['hits']['hits'] : [];
    $this->registry->unregister(self::REQUEST_QUERY);
    $this->registry->register(self::REQUEST_QUERY, $requestQuery['body']['query']);
    $aggregations = $this->getAggregations->execute($reqInterface, $elasticResponse);
    $this->registry->unregister(self::REQUEST_QUERY);
    $responseQuery = $this->getElasticResponse->execute($elasticDocuments, $aggregations);
    if (in_array($reqInterface->getName(), ['quick_search_container', 'catalogsearch_fulltext'], true)) {
        $productIds = array_map(function ($item) {
            return (int)$item['_id'];
        }, $elasticResponse['hits']['hits']);
        $responseQuery = $this->applyRelevanceRules($responseQuery, $productIds);
    }

    // custom
    $collection = $productCollection->create()
        ->addAttributeToSelect('*')->addAttributeToFilter('status',array('like'=>1))->addIdFilter($productIds)->load();

    foreach ($collection as $product) {
        echo $product->getName()." ";
        echo $product->getFinalPrice()."<br/>";
    }
}

/**
 * @param \Magento\Framework\Search\Response\QueryResponse $responseQuery
 * @param int[] $productIds
 * @return \Magento\Framework\Search\Response\QueryResponse
 */
private function applyRelevanceRules(\Magento\Framework\Search\Response\QueryResponse $responseQuery, $productIds)
{
    if ($responseQuery->count()) {
        $boostMultipliers = $this->relevanceRuleRepository->getProductBoostMultipliers($productIds);
        foreach ($responseQuery->getIterator() as $document) {
            if (isset($boostMultipliers[$document->getId()])) {
                $score = $boostMultipliers[$document->getId()] * $document->getCustomAttribute('score')->getValue();
                $document->getCustomAttribute('score')->setValue($score);
            }
        }
    }

    return $responseQuery;
} 
}

any help will be appriacted

  • you are overriding a class , can you include your di.xml file – fmsthird May 14 at 6:19
  • i can't understand which class you are talking about – Ashish Ramchandani May 14 at 6:29

Your Answer

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

Browse other questions tagged or ask your own question.