Skip to content

Commit 7c1fbb4

Browse files
romainruaudAurélien FOUCRET
authored andcommitted
Adding a chapter in documentation related to data providers, and also fix a little typographic bug
Conflicts: doc/indexing.md
1 parent 2086c3d commit 7c1fbb4

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

doc/indexing.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,91 @@
11
# Indexing external content example
22

3+
## Usage of data providers to append external content to products
34

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
489

590
## Example of indexing content into Magento (ex CMS Page):
691

src/app/code/community/Smile/VirtualCategories/Model/Indexer/VirtualCategories/Product/Position.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function _processEvent(Mage_Index_Model_Event $event)
7878
*/
7979
public function reindex($category)
8080
{
81-
/** Reindex all data from search terms custom positions index */
81+
/** Reindex all data from virtual categories products positions index */
8282
$engine = Mage::helper('catalogsearch')->getEngine();
8383
$mapping = $engine->getCurrentIndex()->getMapping('product');
8484
$dataprovider = $mapping->getDataProvider('virtual_categories_products_position');

0 commit comments

Comments
 (0)