Skip to content

Commit

Permalink
Merge pull request #3380 from Smile-SA/fix-boostAutocomplete
Browse files Browse the repository at this point in the history
[Tracking] Add query id in graphQL search response
  • Loading branch information
rbayet authored Sep 16, 2024
2 parents 2edcc07 + 4199c48 commit 0c91e43
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 @@ -77,6 +77,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 @@ -114,6 +114,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,
]);
}

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(\Magento\Framework\Api\Search\SearchCriteriaInterface::class);
$searchResponse = $searchApi->search($searchCriteria);
Expand Down

0 comments on commit 0c91e43

Please sign in to comment.