Ejemplo n.º 1
0
 /**
  * Create search dcument instance from a ES hit.
  *
  * @param array $rawDocument ES raw hit.
  *
  * @return Document
  */
 public function create($rawDocument)
 {
     /** @var \Magento\Framework\Search\DocumentField[] $fields */
     $entityIdFieldName = $this->entityMetadata->getEntityId();
     $rawDocument[Document::ID] = $rawDocument[$entityIdFieldName];
     return $this->objectManager->create($this->instanceName, ['data' => $rawDocument]);
 }
Ejemplo n.º 2
0
 /**
  * @param array $documents
  * @return int[]
  */
 private function getEntityIds($documents)
 {
     $fieldName = $this->entityMetadata->getEntityId();
     $entityIds = [];
     foreach ($documents as $document) {
         $entityIds[] = $document[$fieldName];
     }
     return $entityIds;
 }
Ejemplo n.º 3
0
 /**
  * Test for method "build"
  */
 public function testBuild()
 {
     $fetchResult = ['name' => ['some', 'result']];
     $documents = [['product_id' => 1, 'sku' => 'Product']];
     $this->bucket->expects($this->once())->method('getName')->willReturn('name');
     $this->entityMetadata->expects($this->once())->method('getEntityId')->willReturn('product_id');
     $this->request->expects($this->once())->method('getAggregation')->willReturn([$this->bucket]);
     $this->request->expects($this->once())->method('getDimensions')->willReturn([]);
     $this->bucketBuilder->expects($this->once())->method('build')->willReturn($fetchResult['name']);
     $result = $this->builder->build($this->request, $documents);
     $this->assertEquals($result, $fetchResult);
 }
Ejemplo n.º 4
0
 /**
  * Create Search Document instance
  *
  * @param mixed $rawDocument
  * @return \Magento\Framework\Api\Search\Document
  */
 public function create($rawDocument)
 {
     $documentId = null;
     $entityId = $this->entityMetadata->getEntityId();
     $attributes = [];
     foreach ($rawDocument as $fieldName => $value) {
         if ($fieldName === $entityId) {
             $documentId = $value;
         } else {
             $attributes[$fieldName] = new AttributeValue([AttributeInterface::ATTRIBUTE_CODE => $fieldName, AttributeInterface::VALUE => $value]);
         }
     }
     return new Document([DocumentInterface::ID => $documentId, CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => $attributes]);
 }
Ejemplo n.º 5
0
 /**
  * Create Search Document instance
  *
  * @param mixed $rawDocument
  * @return \Magento\Framework\Search\Document
  */
 public function create($rawDocument)
 {
     /** @var \Magento\Framework\Search\DocumentField[] $fields */
     $fields = [];
     $documentId = null;
     $entityId = $this->entityId->getEntityId();
     foreach ($rawDocument as $rawField) {
         if ($rawField['name'] == $entityId) {
             $documentId = $rawField['value'];
         } else {
             $fields[] = $this->objectManager->create('\\Magento\\Framework\\Search\\DocumentField', $rawField);
         }
     }
     return $this->objectManager->create('\\Magento\\Framework\\Search\\Document', ['documentFields' => $fields, 'documentId' => $documentId]);
 }
Ejemplo n.º 6
0
 /**
  * @param RequestInterface $request
  * @param MatchContainer[] $matchQueries
  * @param ScoreBuilder $scoreBuilder
  * @param Select $select
  * @param IndexBuilderInterface $indexBuilder
  * @return Select
  * @internal param QueryContainer $queryContainer
  */
 private function addMatchQueries(RequestInterface $request, array $matchQueries, ScoreBuilder $scoreBuilder, Select $select, IndexBuilderInterface $indexBuilder)
 {
     if (!$matchQueries) {
         $select->columns($scoreBuilder->build());
         $select = $this->createAroundSelect($select, $scoreBuilder);
     } elseif (count($matchQueries) === 1) {
         $matchContainer = reset($matchQueries);
         $this->matchBuilder->build($scoreBuilder, $select, $matchContainer->getRequest(), $matchContainer->getConditionType());
         $select->columns($scoreBuilder->build());
         $select = $this->createAroundSelect($select, $scoreBuilder);
     } elseif (count($matchQueries) > 1) {
         $select->columns($scoreBuilder->build());
         $select = $this->createAroundSelect($select, $scoreBuilder);
         $subSelect = $select;
         $select = $this->resource->getConnection(Resource::DEFAULT_READ_RESOURCE)->select();
         $tables = array_merge(array_keys($matchQueries), ['main_select.relevance']);
         $relevance = implode('.relevance + ', $tables);
         $select->from(['main_select' => $subSelect], [$this->entityMetadata->getEntityId() => 'entity_id', 'relevance' => sprintf('(%s)', $relevance)]);
         foreach ($matchQueries as $matchName => $matchContainer) {
             $matchSelect = $indexBuilder->build($request);
             $matchScoreBuilder = $this->scoreBuilderFactory->create();
             $matchSelect = $this->matchBuilder->build($matchScoreBuilder, $matchSelect, $matchContainer->getRequest(), $matchContainer->getConditionType());
             $matchSelect->columns($matchScoreBuilder->build());
             $select->join([$matchName => $this->createAroundSelect($matchSelect, $scoreBuilder)], $matchName . '.entity_id = main_select.entity_id', []);
         }
     }
     return $select;
 }
Ejemplo n.º 7
0
 /**
  * @param Select $select
  * @param ScoreBuilder $scoreBuilder
  * @return Select
  */
 private function createAroundSelect(
     Select $select,
     ScoreBuilder $scoreBuilder
 ) {
     $parentSelect = $this->resource->getConnection(Resource::DEFAULT_READ_RESOURCE)->select();
     $parentSelect
         ->from(
             ['main_select' => $select],
             [
                 $this->entityMetadata->getEntityId() => 'entity_id',
                 'relevance' => sprintf('MAX(%s)', $scoreBuilder->getScoreAlias())
             ]
         )
         ->group($this->entityMetadata->getEntityId());
     return $parentSelect;
 }