Skip to content

Commit

Permalink
Merge branch '2.10.x' into 2.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Sep 16, 2024
2 parents 69d88ba + 0c91e43 commit 7c4820f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
'current_page' => $searchResult->getCurrentPage(),
'total_pages' => $searchResult->getTotalPages(),
'is_spellchecked' => $searchResult->isSpellchecked(),
'query_id' => $searchResult->getQueryId(),
],
'search_result' => $searchResult,
'layer_type' => $layerType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function getResult(array $args, ResolveInfo $info, ContextInterface $cont
'currentPage' => $searchCriteria->getCurrentPage(),
'totalPages' => $maxPages,
'isSpellchecked' => $searchResults->__toArray()['is_spellchecked'] ?? false,
'queryId' => $searchResults->__toArray()['query_id'] ?? null,
'suggestions' => $suggestions,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public function isSpellchecked()
{
return (bool) $this->data['isSpellchecked'] ?? false;
}

/**
* @return ?int
*/
public function getQueryId()
{
return $this->data['queryId'] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ type ViewMoreResult @doc(description: "The Products object is the top-level obje
type SearchResultPageInfo
{
is_spellchecked: Boolean
query_id: Int
}
40 changes: 28 additions & 12 deletions src/module-elasticsuite-core/Model/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

namespace Smile\ElasticsuiteCore\Model;

use Magento\Framework\Search\SearchEngineInterface;
use Magento\Framework\Search\SearchResponseBuilder;
use Smile\ElasticsuiteCore\Api\Search\ContextInterface;
use Smile\ElasticsuiteCore\Model\Search\RequestBuilder;

/**
* SearchInterface implementation using elasticsuite.
*
Expand All @@ -24,35 +29,43 @@
class Search implements \Magento\Search\Api\SearchInterface
{
/**
* @var \Smile\ElasticsuiteCore\Model\Search\RequestBuilder
* @var RequestBuilder
*/
private $searchRequestBuilder;

/**
* @var \Magento\Framework\Search\SearchEngineInterface
* @var SearchEngineInterface
*/
private $searchEngine;

/**
* @var \Magento\Framework\Search\SearchResponseBuilder
* @var SearchResponseBuilder
*/
private $searchResponseBuilder;

/**
* @var ContextInterface
*/
private $searchContext;

/**
* Constructor.
*
* @param \Magento\Framework\Search\SearchEngineInterface $searchEngine Search engine.
* @param \Smile\ElasticsuiteCore\Model\Search\RequestBuilder $searchRequestBuilder Search request builder.
* @param \Magento\Framework\Search\SearchResponseBuilder $searchResponseBuilder Search response builder.
* @param SearchEngineInterface $searchEngine Search engine.
* @param RequestBuilder $searchRequestBuilder Search request builder.
* @param SearchResponseBuilder $searchResponseBuilder Search response builder.
* @param ContextInterface $searchContext Search context.
*/
public function __construct(
\Magento\Framework\Search\SearchEngineInterface $searchEngine,
\Smile\ElasticsuiteCore\Model\Search\RequestBuilder $searchRequestBuilder,
\Magento\Framework\Search\SearchResponseBuilder $searchResponseBuilder
SearchEngineInterface $searchEngine,
RequestBuilder $searchRequestBuilder,
SearchResponseBuilder $searchResponseBuilder,
ContextInterface $searchContext
) {
$this->searchRequestBuilder = $searchRequestBuilder;
$this->searchEngine = $searchEngine;
$this->searchResponseBuilder = $searchResponseBuilder;
$this->searchRequestBuilder = $searchRequestBuilder;
$this->searchEngine = $searchEngine;
$this->searchResponseBuilder = $searchResponseBuilder;
$this->searchContext = $searchContext;
}

/**
Expand All @@ -68,10 +81,13 @@ public function search(\Magento\Framework\Api\Search\SearchCriteriaInterface $se
$searchResponse = $this->searchEngine->search($searchRequest);
$searchResult = $this->searchResponseBuilder->build($searchResponse);

$query = $this->searchContext->getCurrentSearchQuery();

$totalCount = $searchResponse->count();
$searchResult->setTotalCount($totalCount);
$searchResult->setSearchCriteria($searchCriteria);
$searchResult->setData('is_spellchecked', (bool) $searchRequest->isSpellchecked());
$searchResult->setData('query_id', ($query && $query->getId()) ? (int) $query->getId() : null);

return $searchResult;
}
Expand Down
6 changes: 4 additions & 2 deletions src/module-elasticsuite-core/Test/Unit/Model/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
*/
namespace Smile\ElasticsuiteCore\Test\Unit\Model;

use Smile\ElasticsuiteCore\Model\Search;
use Smile\ElasticsuiteCore\Api\Search\ContextInterface;

/**
* Search API unit testing.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
* @category Smile
* @package Smile\ElasticsuiteCore
Expand All @@ -39,8 +40,9 @@ public function testSearch($documents, $docCount)
$searchEngine = $this->getSearchEngine($documents, $docCount);
$searchRequestBuilder = $this->getSearchRequestBuilder();
$searchResponseBuilder = $this->getSearchResponseBuilder();
$searchContext = $this->createMock(ContextInterface::class);

$searchApi = new \Smile\ElasticsuiteCore\Model\Search($searchEngine, $searchRequestBuilder, $searchResponseBuilder);
$searchApi = new \Smile\ElasticsuiteCore\Model\Search($searchEngine, $searchRequestBuilder, $searchResponseBuilder, $searchContext);

$searchCriteria = $this->createMock(\Smile\ElasticsuiteCore\Api\Search\SearchCriteriaInterface::class);
$searchResponse = $searchApi->search($searchCriteria);
Expand Down

0 comments on commit 7c4820f

Please sign in to comment.