From 76f4a9652644edbdf268b53f0b60161592186a0f Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Wed, 19 Aug 2020 13:06:31 +0200 Subject: [PATCH] Sort results by size (only working with integer values) --- .../ModelKeySearchQueryExpanderPlugin.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/FondOfSpryker/Client/ProductPageSearchExpander/Plugin/Elasticsearch/QueryExpander/ModelKeySearchQueryExpanderPlugin.php b/src/FondOfSpryker/Client/ProductPageSearchExpander/Plugin/Elasticsearch/QueryExpander/ModelKeySearchQueryExpanderPlugin.php index 51ceb1d..88e2c88 100644 --- a/src/FondOfSpryker/Client/ProductPageSearchExpander/Plugin/Elasticsearch/QueryExpander/ModelKeySearchQueryExpanderPlugin.php +++ b/src/FondOfSpryker/Client/ProductPageSearchExpander/Plugin/Elasticsearch/QueryExpander/ModelKeySearchQueryExpanderPlugin.php @@ -5,8 +5,10 @@ use Elastica\Query; use Elastica\Query\BoolQuery; use FondOfSpryker\Shared\ProductPageSearchExpander\ProductPageSearchExpanderConstants; +use Generated\Shared\Search\PageIndexMap; use InvalidArgumentException; use Spryker\Client\Kernel\AbstractPlugin; +use Spryker\Client\SearchElasticsearch\Config\SortConfig; use Spryker\Client\SearchExtension\Dependency\Plugin\QueryExpanderPluginInterface; use Spryker\Client\SearchExtension\Dependency\Plugin\QueryInterface; @@ -43,10 +45,26 @@ public function expandQuery(QueryInterface $searchQuery, array $requestParameter ); $boolQuery->addMust($matchQuery); + $this->addSort($searchQuery->getSearchQuery()); return $searchQuery; } + /** + * @param \Elastica\Query $searchQuery + * + * @return void + */ + protected function addSort(Query $searchQuery): void + { + $searchQuery->addSort([ + PageIndexMap::INTEGER_SORT . '.' . ProductPageSearchExpanderConstants::SIZE => [ + 'order' => SortConfig::DIRECTION_ASC, + 'mode' => 'min', + ], + ]); + } + /** * @param \Elastica\Query $query *