Skip to content

Commit 2d60244

Browse files
romainruaudAurélien FOUCRET
authored andcommitted
Add an attribute to conditionnally manage category name indexation for products
1 parent 28cf8fe commit 2d60244

File tree

4 files changed

+132
-8
lines changed

4 files changed

+132
-8
lines changed

src/app/code/community/Smile/ElasticSearch/Model/Resource/Engine/Index.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class Smile_ElasticSearch_Model_Resource_Engine_Index extends Mage_CatalogSearch
3030
*/
3131
protected $_categoryNameAttribute = null;
3232

33+
/**
34+
* @var Mage_Eav_Model_Entity_Attribute_Abstract
35+
*/
36+
protected $_useNameInSearchAttribute = null;
37+
3338
/**
3439
* @var array
3540
*/
@@ -183,7 +188,7 @@ protected function _getCatalogCategoryData($storeId, $productIds, $visibility =
183188
}
184189

185190
// Append new categories into the cache of names
186-
$storeCategoryName = $this->_loadCategoryNames(array_unique($loadedCategoryIds), $storeId);
191+
$storeCategoryName = array_filter($this->_loadCategoryNames(array_unique($loadedCategoryIds), $storeId));
187192

188193
foreach ($result as &$categoriesData) {
189194
// Fill the category_name field from the cache of names
@@ -234,23 +239,35 @@ protected function _loadCategoryNames($categoryIds, $storeId)
234239
$loadCategoryIds = array_diff($categoryIds, array_keys($this->_categoryNameCache[$storeId]));
235240
}
236241

242+
$loadCategoryIds = array_map('intval', $loadCategoryIds);
243+
237244
if (!empty($loadCategoryIds)) {
238245

239246
$rootCategoryId = (int) Mage::app()->getStore($storeId)->getRootCategoryId();
240247
$this->_categoryNameCache[$storeId][$rootCategoryId] = '';
241248

242-
$adapter = $this->_getWriteAdapter();
243-
$nameAttr = $this->_getCategoryNameAttribute();
249+
$adapter = $this->_getWriteAdapter();
250+
$nameAttr = $this->_getCategoryNameAttribute();
251+
$useNameAttr = $this->_getUseNameInSearchAttribute();
244252

245253
$select = $adapter->select()
246-
->from(array('default_value' => $nameAttr->getBackendTable(), array('entity_id')))
254+
->from(array('default_value' => $nameAttr->getBackendTable()), array('entity_id'))
247255
->where('default_value.entity_id != ?', $rootCategoryId)
248256
->where('default_value.store_id = ?', 0)
249257
->where('default_value.attribute_id = ?', (int) $nameAttr->getAttributeId())
250258
->where('default_value.entity_id IN (?)', $loadCategoryIds);
251259

260+
$joinUseNameCond = sprintf(
261+
"default_value.entity_id = use_name_default_value.entity_id" .
262+
" AND use_name_default_value.attribute_id = %d AND use_name_default_value.store_id = %d",
263+
(int) $useNameAttr->getAttributeId(),
264+
0
265+
);
266+
$select->joinLeft(array('use_name_default_value' => $useNameAttr->getBackendTable()), $joinUseNameCond, array());
267+
252268
if (Mage::app()->isSingleStoreMode()) {
253269
$select->columns(array('name' => 'default_value.value'));
270+
$select->columns(array('use_name' => 'COALESCE(use_name_default_value.value,1)'));
254271
} else {
255272
$joinStoreNameCond = sprintf(
256273
"default_value.entity_id = store_value.entity_id AND store_value.attribute_id = %d AND store_value.store_id = %d",
@@ -259,11 +276,24 @@ protected function _loadCategoryNames($categoryIds, $storeId)
259276
);
260277
$select->joinLeft(array('store_value' => $nameAttr->getBackendTable()), $joinStoreNameCond, array())
261278
->columns(array('name' => 'COALESCE(store_value.value,default_value.value)'));
279+
280+
$joinUseNameStoreCond = sprintf(
281+
"default_value.entity_id = use_name_store_value.entity_id" .
282+
" AND use_name_store_value.attribute_id = %d AND use_name_store_value.store_id = %d",
283+
(int) $useNameAttr->getAttributeId(),
284+
(int) $storeId
285+
);
286+
$select->joinLeft(array('use_name_store_value' => $useNameAttr->getBackendTable()), $joinUseNameStoreCond, array())
287+
->columns(array('use_name' => 'COALESCE(use_name_store_value.value,use_name_default_value.value,1)'));
288+
262289
}
263290

264291
foreach ($adapter->fetchAll($select) as $row) {
265292
$categoryId = (int) $row['entity_id'];
266-
$this->_categoryNameCache[$storeId][$categoryId] = $row['name'];
293+
$this->_categoryNameCache[$storeId][$categoryId] = '';
294+
if ((bool) $row['use_name']) {
295+
$this->_categoryNameCache[$storeId][$categoryId] = $row['name'];
296+
}
267297
}
268298
}
269299

@@ -314,7 +344,6 @@ protected function _getCatalogProductPriceData($productIds = null)
314344
return $result;
315345
}
316346

317-
318347
/**
319348
* Returns category name attribute
320349
*
@@ -328,4 +357,19 @@ protected function _getCategoryNameAttribute()
328357

329358
return $this->_categoryNameAttribute;
330359
}
360+
361+
/**
362+
* Returns category "use name in product search" attribute
363+
*
364+
* @return Mage_Eav_Model_Entity_Attribute_Abstract
365+
*/
366+
protected function _getUseNameInSearchAttribute()
367+
{
368+
if ($this->_useNameInSearchAttribute === null) {
369+
$this->_useNameInSearchAttribute = Mage::getModel('eav/entity_attribute')
370+
->loadByCode('catalog_category', 'used_in_product_search');
371+
}
372+
373+
return $this->_useNameInSearchAttribute;
374+
}
331375
}

src/app/code/community/Smile/ElasticSearch/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<config>
2222
<modules>
2323
<Smile_ElasticSearch>
24-
<version>1.2.2</version>
24+
<version>1.3.0</version>
2525
</Smile_ElasticSearch>
2626
</modules>
2727
<global>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Append an attribute to all categories, to decide if their name should be used for fulltext indexation or not
4+
*
5+
* DISCLAIMER
6+
*
7+
* Do not edit or add to this file if you wish to upgrade Smile Searchandising Suite to newer
8+
* versions in the future.
9+
*
10+
* This work is a fork of Johann Reinke <[email protected]> previous module
11+
* available at https://github.com/jreinke/magento-elasticsearch
12+
*
13+
* @category Smile
14+
* @package Smile_ElasticSearch
15+
* @author Romain RUAUD <[email protected]>
16+
* @copyright 2016 Smile
17+
* @license Apache License Version 2.0
18+
*/
19+
20+
/**
21+
* @var Mage_Catalog_Model_Resource_Setup $this
22+
*/
23+
$installer = $this;
24+
$installer->startSetup();
25+
26+
$entityTypeId = $installer->getEntityTypeId('catalog_category');
27+
$defaultAttributeSetId = $this->getDefaultAttributeSetId($entityTypeId);
28+
$defaultGroup = $this->getAttributeGroup($entityTypeId, $defaultAttributeSetId, 'General Information');
29+
30+
$installer->addAttribute(
31+
$entityTypeId,
32+
'used_in_product_search',
33+
array(
34+
'type' => 'int',
35+
'label' => 'Use category name in product search',
36+
'input' => 'select',
37+
'source' => 'eav/entity_attribute_source_boolean',
38+
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
39+
'required' => true,
40+
'default' => 1,
41+
'enabled' => true,
42+
'is_enabled' => true,
43+
'visible' => true,
44+
'note' => "If the category name is used for fulltext search on products.",
45+
'sort_order' => 150
46+
)
47+
);
48+
49+
$installer->addAttributeToSet(
50+
$entityTypeId,
51+
$defaultAttributeSetId,
52+
$defaultGroup['attribute_group_id'],
53+
'used_in_product_search'
54+
);
55+
56+
$attributeId = $installer->getAttributeId($entityTypeId, 'used_in_product_search');
57+
58+
$select = $installer->getConnection()->select();
59+
60+
$select->from(
61+
$installer->getTable('catalog_category_entity'),
62+
array(
63+
new Zend_Db_Expr("{$entityTypeId} as entity_type_id"),
64+
new Zend_Db_Expr("{$attributeId} as attribute_id"),
65+
'entity_id',
66+
new Zend_Db_Expr("1 as value")
67+
)
68+
);
69+
70+
$insert = $installer->getConnection()->insertFromSelect(
71+
$select,
72+
$installer->getTable('catalog_category_entity_int'),
73+
array('entity_type_id', 'attribute_id', 'entity_id', 'value')
74+
);
75+
76+
$installer->getConnection()->query($insert);
77+
78+
$installer->endSetup();

src/app/locale/fr_FR/Smile_ElasticSearch.csv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ Ex: Brand facet will be displayed only if 90% of the product have a brand.,Ex: L
8080
"Computes custom positions for products by search terms.","Indexe les positions attribuées aux produits dans les résultats de recherche."
8181
"Search Terms products positions Index","Index des positions des produits dans les résultats de recherche"
8282
"Custom results positions","Positionnement manuel des produits"
83-
"Search Term has been saved.","Le terme de recherche a été sauvegardé"
83+
"Search Term has been saved.","Le terme de recherche a été sauvegardé"
84+
"If the category name is used for fulltext search on products.","Si cette option est placée à oui, le nom de la catégorie sera utilisé pour rechercher des produits"
85+
"Use category name in product search","Utiliser le nom de la catégorie dans la recherche"

0 commit comments

Comments
 (0)