DocumentFactory.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\SearchAdapter;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Search\EntityMetadata;
  9. use Magento\Framework\Api\AttributeInterface;
  10. use Magento\Framework\Api\AttributeValue;
  11. use Magento\Framework\Api\CustomAttributesDataInterface;
  12. use Magento\Framework\Api\Search\Document;
  13. use Magento\Framework\Api\Search\DocumentInterface;
  14. /**
  15. * Document Factory
  16. * @api
  17. * @since 100.1.0
  18. */
  19. class DocumentFactory
  20. {
  21. /**
  22. * Object Manager instance
  23. *
  24. * @var ObjectManagerInterface
  25. * @deprecated 100.1.0
  26. * @since 100.1.0
  27. */
  28. protected $objectManager;
  29. /**
  30. * @var EntityMetadata
  31. */
  32. private $entityMetadata;
  33. /**
  34. * @param ObjectManagerInterface $objectManager
  35. * @param EntityMetadata $entityMetadata
  36. */
  37. public function __construct(ObjectManagerInterface $objectManager, EntityMetadata $entityMetadata)
  38. {
  39. $this->objectManager = $objectManager;
  40. $this->entityMetadata = $entityMetadata;
  41. }
  42. /**
  43. * Create Search Document instance
  44. *
  45. * @param array $rawDocument
  46. * @return Document
  47. * @since 100.1.0
  48. */
  49. public function create($rawDocument)
  50. {
  51. /** @var AttributeValue[] $fields */
  52. $attributes = [];
  53. $documentId = null;
  54. $entityId = $this->entityMetadata->getEntityId();
  55. foreach ($rawDocument as $fieldName => $value) {
  56. if ($fieldName === $entityId) {
  57. $documentId = $value;
  58. } elseif ($fieldName === '_score') {
  59. $attributes['score'] = new AttributeValue(
  60. [
  61. AttributeInterface::ATTRIBUTE_CODE => $fieldName,
  62. AttributeInterface::VALUE => $value,
  63. ]
  64. );
  65. }
  66. }
  67. return new Document(
  68. [
  69. DocumentInterface::ID => $documentId,
  70. CustomAttributesDataInterface::CUSTOM_ATTRIBUTES => $attributes,
  71. ]
  72. );
  73. }
  74. }