Builder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\SearchAdapter\Aggregation;
  7. use Magento\Elasticsearch\SearchAdapter\QueryContainer;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Search\RequestInterface;
  10. use Magento\Framework\Search\Dynamic\DataProviderInterface;
  11. use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder\BucketBuilderInterface;
  12. class Builder
  13. {
  14. /**
  15. * @var DataProviderInterface[]
  16. */
  17. protected $dataProviderContainer;
  18. /**
  19. * @var BucketBuilderInterface[]
  20. */
  21. protected $aggregationContainer;
  22. /**
  23. * @var DataProviderFactory
  24. */
  25. private $dataProviderFactory;
  26. /**
  27. * @var QueryContainer
  28. */
  29. private $query = null;
  30. /**
  31. * @param DataProviderInterface[] $dataProviderContainer
  32. * @param BucketBuilderInterface[] $aggregationContainer
  33. * @param DataProviderFactory|null $dataProviderFactory
  34. */
  35. public function __construct(
  36. array $dataProviderContainer,
  37. array $aggregationContainer,
  38. DataProviderFactory $dataProviderFactory = null
  39. ) {
  40. $this->dataProviderContainer = array_map(
  41. function (DataProviderInterface $dataProvider) {
  42. return $dataProvider;
  43. },
  44. $dataProviderContainer
  45. );
  46. $this->aggregationContainer = array_map(
  47. function (BucketBuilderInterface $bucketBuilder) {
  48. return $bucketBuilder;
  49. },
  50. $aggregationContainer
  51. );
  52. $this->dataProviderFactory = $dataProviderFactory
  53. ?: ObjectManager::getInstance()->get(DataProviderFactory::class);
  54. }
  55. /**
  56. * Builds aggregations from the search request.
  57. *
  58. * This method iterates through buckets and builds all aggregations one by one, passing buckets and relative
  59. * data into bucket aggregation builders which are responsible for aggregation calculation.
  60. *
  61. * @param RequestInterface $request
  62. * @param array $queryResult
  63. * @return array
  64. * @throws \LogicException thrown by DataProviderFactory for validation issues
  65. * @see \Magento\Elasticsearch\SearchAdapter\Aggregation\DataProviderFactory
  66. */
  67. public function build(RequestInterface $request, array $queryResult)
  68. {
  69. $aggregations = [];
  70. $buckets = $request->getAggregation();
  71. $dataProvider = $this->dataProviderFactory->create(
  72. $this->dataProviderContainer[$request->getIndex()],
  73. $this->query
  74. );
  75. foreach ($buckets as $bucket) {
  76. $bucketAggregationBuilder = $this->aggregationContainer[$bucket->getType()];
  77. $aggregations[$bucket->getName()] = $bucketAggregationBuilder->build(
  78. $bucket,
  79. $request->getDimensions(),
  80. $queryResult,
  81. $dataProvider
  82. );
  83. }
  84. $this->query = null;
  85. return $aggregations;
  86. }
  87. /**
  88. * Sets the QueryContainer instance to the internal property in order to use it in build process
  89. *
  90. * @param QueryContainer $query
  91. * @return $this
  92. */
  93. public function setQuery(QueryContainer $query)
  94. {
  95. $this->query = $query;
  96. return $this;
  97. }
  98. }