getAggregations = $getAggregations; $this->getRequestQuery = $getRequestQuery; $this->getElasticResponse = $getElasticResponse; $this->clientRepository = $clientRepository; $this->registry = $registry; $this->logger = $logger; } /** * @param RequestInterface $request * @return \Magento\Framework\Search\Response\QueryResponse|mixed * @throws \Elasticsearch\Common\Exceptions\Missing404Exception * @throws \Magento\Framework\Exception\LocalizedException */ public function query(RequestInterface $request) { $client = $this->clientRepository->get(); if (!$client->ping()) { return $this->getElasticResponse->execute([], [], 0); } try { $requestQuery = $this->getRequestQuery->execute($request); $elasticResponse = $client->search($requestQuery); } catch (BadRequest400Exception $e) { $this->logger->logError($e->getMessage()); return $this->getElasticResponse->execute([], [], 0); } catch (\Exception $e) { return $this->getElasticResponse->execute([], [], 0); } $elasticDocuments = $elasticResponse['hits']['hits'] ?? []; $elasticTotal = $elasticResponse['hits']['total']['value'] ?? $elasticResponse['hits']['total'] ?? 0; $this->registry->unregister(self::REQUEST_QUERY); $this->registry->register(self::REQUEST_QUERY, $requestQuery['body']['query']); $aggregations = $this->getAggregations->execute($request, $elasticResponse); $this->registry->unregister(self::REQUEST_QUERY); $responseQuery = $this->getElasticResponse->execute($elasticDocuments, $aggregations, $elasticTotal); $this->logger->log($request, $responseQuery, $requestQuery, $elasticResponse); return $responseQuery; } /** * @param RequestInterface $request * @return array * @throws \Elasticsearch\Common\Exceptions\Missing404Exception * @throws \Magento\Framework\Exception\LocalizedException */ public function queryAdvancedSearchProduct(RequestInterface $request) { $client = $this->clientRepository->get(); $requestQuery = $this->getRequestQuery->execute($request); unset($requestQuery['aggregations']); $requestQuery['body']['_source'] = ['amasty_xsearch_fulltext']; $elasticResponse = $client->search($requestQuery); $products = []; if (!empty($elasticResponse['hits']['hits'])) { foreach ($elasticResponse['hits']['hits'] as $index => $product) { if (!empty($product['_source']['amasty_xsearch_fulltext'])) { $id = $product['_source']['amasty_xsearch_fulltext']['entity_id'] ?? $product['_id']; $products[$id] = $product['_source']['amasty_xsearch_fulltext']; } } } $hits = $elasticResponse['hits']['total']['value'] ?? $elasticResponse['hits']['total'] ?? 0; return [self::HITS => $hits, self::PRODUCTS => $products]; } }