|
1 | 1 | # Indexing external content example
|
2 | 2 |
|
| 3 | +## Usage of data providers to append external content to products |
3 | 4 |
|
| 5 | +### Main concept |
| 6 | + |
| 7 | +You can append external data/content to products during indexation via the usage of Data Providers |
| 8 | + |
| 9 | +You can declare your Data provider in your configuration file like this : |
| 10 | + |
| 11 | +``` xml |
| 12 | + <global> |
| 13 | + <smile_elasticsearch> |
| 14 | + <mapping> |
| 15 | + <product> |
| 16 | + <data_providers> |
| 17 | + <search_terms_position>smile_elasticsearch/engine_elasticsearch_mapping_dataProvider_terms_position</search_terms_position> |
| 18 | + </data_providers> |
| 19 | + </product> |
| 20 | + </mapping> |
| 21 | + </smile_elasticsearch> |
| 22 | + </global> |
| 23 | +``` |
| 24 | + |
| 25 | +Your DataProvider class must extend *Smile_ElasticSearch_Model_Resource_Engine_Elasticsearch_Mapping_DataProvider_Abstract* and implement following methods : |
| 26 | + |
| 27 | +* getMappingProperties() : will return custom fields defined by your module to append to ES mapping |
| 28 | +* getEntitiesData($storeId, $entityIds) : will return your custom data for products, and a given store |
| 29 | + |
| 30 | +### Exemple of a dataProvider which append a custom field on products : |
| 31 | + |
| 32 | +First step is to add this field into the mapping via getMappingProperties() |
| 33 | + |
| 34 | +```php |
| 35 | + public function getMappingProperties() |
| 36 | + { |
| 37 | + $mapping = array( |
| 38 | + "properties" => array( |
| 39 | + "my_custom_field" => array('type' => 'long', 'doc_values' => true) |
| 40 | + ) |
| 41 | + ); |
| 42 | + |
| 43 | + return $mapping; |
| 44 | + } |
| 45 | +``` |
| 46 | + |
| 47 | +Then implement the getEntitiesData() method. You must return an array indexed by the product entityId(s) |
| 48 | + |
| 49 | +```php |
| 50 | +public function getEntitiesData($storeId, $entityIds) |
| 51 | + { |
| 52 | + $result = array(); |
| 53 | + |
| 54 | + // This piece of code is totally dummy, and is here as an exemple |
| 55 | + // We suppose there is an external data source with logic to retrieve product data |
| 56 | + $externalDataSource = $this->_getExternalDataSource(); |
| 57 | + $externalData = $externalDataSource->getDataForProducts($entityIds); |
| 58 | + |
| 59 | + foreach ($externalData as $data) { |
| 60 | + $result[$data->entityId] = array("my_custom_field" => $data->externalCustomField); |
| 61 | + } |
| 62 | + |
| 63 | + return $result; |
| 64 | + } |
| 65 | +``` |
| 66 | + |
| 67 | +### Custom indexing of data coming from Data Providers |
| 68 | + |
| 69 | +You can process partial or full indexation of external data added by your data providers, for a given bunch of product Ids, and eventually a store. |
| 70 | + |
| 71 | + |
| 72 | +```php |
| 73 | +$engine = Mage::helper('catalogsearch')->getEngine(); |
| 74 | +$mapping = $engine->getCurrentIndex()->getMapping('product'); |
| 75 | +$dataProvider = $mapping->getDataProvider('virtual_categories_products_position'); // This is the internal code of the data provider |
| 76 | + |
| 77 | +// $storeId can be null |
| 78 | +// $productIds is an array of product Ids, if empty, data will be reindexed for all products |
| 79 | +$dataProvider->updateAllData($storeId, $productIds); |
| 80 | +``` |
| 81 | + |
| 82 | +### Other examples |
| 83 | + |
| 84 | +Here are dataProviders already existing into the Elastic Suite : |
| 85 | + |
| 86 | +* Custom positions for products in search results |
| 87 | +* Custom positions for products in virtual categories |
| 88 | +* Popularity data added to products from external source |
4 | 89 |
|
5 | 90 | ## Example of indexing content into Magento (ex CMS Page):
|
6 | 91 |
|
|
0 commit comments