Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function __construct()
'label' => 'Label',
'style' => 'width: 100px;',
),
'searchable' => array(
'label' => 'Searchable?',
'options' => array(
'1' => 'Yes',
'2' => 'No'
),
'rowMethod' => 'getSearchable',
),
),
'buttonLabel' => 'Add Facet',
'addAfter' => false,
Expand Down
23 changes: 20 additions & 3 deletions app/code/community/Algolia/Algoliasearch/Helper/Algoliahelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,20 @@ public function copyQueryRules($fromIndexName, $toIndexName)
$this->lastTaskId = $res['taskID'];
}

public function waitLastTask()
public function waitLastTask($lastUsedIndexName = null, $lastTaskId = null)
{
if (!isset($this->lastUsedIndexName) || !isset($this->lastTaskId)) {
if ($lastUsedIndexName === null && isset($this->lastUsedIndexName)) {
$lastUsedIndexName = $this->lastUsedIndexName;
}

if ($lastTaskId === null && isset($this->lastTaskId)) {
$lastTaskId = $this->lastTaskId;
}
if (!$lastUsedIndexName || !$lastTaskId) {
return;
}

$this->client->initIndex($this->lastUsedIndexName)->waitTask($this->lastTaskId);
$this->client->initIndex($lastUsedIndexName)->waitTask($lastTaskId);
}

private function prepareRecords(&$objects, $indexName)
Expand Down Expand Up @@ -347,4 +354,14 @@ private function getLongestAttribute($object)

return $longestAttribute;
}

public function getLastIndexName()
{
return $this->lastUsedIndexName;
}

public function getLastTaskId()
{
return $this->lastTaskId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ public function setSettings($storeId, $saveToTmpIndicesToo = false)
$attributesForFaceting[] = $facet['attribute'];
}
} else {
$attributesForFaceting[] = $facet['attribute'];
$attribute = $facet['attribute'];
if (array_key_exists('searchable', $facet) && $facet['searchable'] === '1') {
$attribute = 'searchable('.$attribute.')';
}

$attributesForFaceting[] = $attribute;
}
}

Expand Down Expand Up @@ -294,9 +299,9 @@ public function setSettings($storeId, $saveToTmpIndicesToo = false)
*/
$sorting_indices = $this->config->getSortingIndices($storeId);

if (count($sorting_indices) > 0) {
$replicas = array();
$replicas = array();

if (count($sorting_indices) > 0) {
foreach ($sorting_indices as $values) {
if ($this->config->isCustomerGroupsEnabled($storeId) && $values['attribute'] === 'price') {
foreach ($groups = Mage::getModel('customer/group')->getCollection() as $group) {
Expand All @@ -316,6 +321,7 @@ public function setSettings($storeId, $saveToTmpIndicesToo = false)
}

$this->algolia_helper->setSettings($this->getIndexName($storeId), array('replicas' => $replicas));
$setReplicasTaskId = $this->algolia_helper->getLastTaskId();

/** @var Mage_Core_Model_Store $store */
$store = Mage::getModel('core/store')->load($storeId);
Expand Down Expand Up @@ -369,8 +375,14 @@ public function setSettings($storeId, $saveToTmpIndicesToo = false)
}
}
}
} else {
$this->algolia_helper->setSettings($this->getIndexName($storeId), array('replicas' => $replicas));
$setReplicasTaskId = $this->algolia_helper->getLastTaskId();
}

$this->deleteUnusedReplicas($indexName, $replicas, $setReplicasTaskId);


if ($this->config->isEnabledSynonyms($storeId) === true) {
if ($synonymsFile = $this->config->getSynonymsFile($storeId)) {
$synonymsToSet = json_decode(file_get_contents($synonymsFile));
Expand Down Expand Up @@ -1083,4 +1095,28 @@ private function clearNoValues($customData, $rootLevel = true)

return $customData;
}

private function deleteUnusedReplicas($indexName, $replicas, $setReplicasTaskId)
{
$indicesToDelete = array();

$allIndices = $this->algolia_helper->listIndexes();
foreach ($allIndices['items'] as $indexInfo) {
if (strpos($indexInfo['name'], $indexName) !== 0 || $indexInfo['name'] === $indexName) {
continue;
}
// Do not delete tmp indices which are used for indexing jobs
if (strpos($indexInfo['name'], '_tmp') === false && in_array($indexInfo['name'], $replicas) === false) {
$indicesToDelete[] = $indexInfo['name'];
}
}

if (count($indicesToDelete) > 0) {
$this->algolia_helper->waitLastTask($indexName, $setReplicasTaskId);

foreach ($indicesToDelete as $indexToDelete) {
$this->algolia_helper->deleteIndex($indexToDelete);
}
}
}
}
2 changes: 1 addition & 1 deletion app/code/community/Algolia/Algoliasearch/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
<instant>
<replace_categories>1</replace_categories>
<instant_selector>.main</instant_selector>
<facets>a:3:{s:18:"_1432907948596_596";a:3:{s:9:"attribute";s:5:"price";s:4:"type";s:6:"slider";s:5:"label";s:5:"Price";}s:18:"_1432907963376_376";a:3:{s:9:"attribute";s:10:"categories";s:4:"type";s:11:"conjunctive";s:5:"label";s:10:"Categories";}s:17:"_1447846054032_32";a:3:{s:9:"attribute";s:5:"color";s:4:"type";s:11:"disjunctive";s:5:"label";s:6:"Colors";}}</facets>
<facets>a:3:{s:18:"_1432907948596_596";a:4:{s:9:"attribute";s:5:"price";s:4:"type";s:6:"slider";s:5:"label";s:5:"Price";s:10:"searchable";s:1:"2";}s:18:"_1432907963376_376";a:4:{s:9:"attribute";s:10:"categories";s:4:"type";s:11:"conjunctive";s:5:"label";s:10:"Categories";s:10:"searchable";s:1:"2";}s:17:"_1447846054032_32";a:4:{s:9:"attribute";s:5:"color";s:4:"type";s:11:"disjunctive";s:5:"label";s:6:"Colors";s:10:"searchable";s:1:"1";}}</facets>
<max_values_per_facet>10</max_values_per_facet>
<sorts>a:3:{s:18:"_1432908018844_844";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:3:"asc";s:5:"label";s:12:"Lowest price";}s:18:"_1432908022539_539";a:3:{s:9:"attribute";s:5:"price";s:4:"sort";s:4:"desc";s:5:"label";s:13:"Highest price";}s:18:"_1433768597454_454";a:3:{s:9:"attribute";s:10:"created_at";s:4:"sort";s:4:"desc";s:5:"label";s:12:"Newest first";}}</sorts>
<add_to_cart_enable>1</add_to_cart_enable>
Expand Down
5 changes: 4 additions & 1 deletion app/code/community/Algolia/Algoliasearch/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@
<![CDATA[
Configure here the filtering options you want to display on your search results page.
Choose Disjunctive to allow the selection of different values of a facet (e.g. hotels with 4 OR 5 stars).
Choose Conjunctive to allow the selection of only one value of a facet (e.g. only "Size M").<br>
Choose Conjunctive to allow the selection of only one value of a facet (e.g. only "Size M").
<br>
Set facet as searchable if you want your customers to be able to <a target="_blank" href="https://www.algolia.com/doc/api-reference/api-methods/search-for-facet-values/">search for facet values</a>. It's handy when a facet has a lot of values.
<br>
Facets' documentation: <a target="_blank" href="https://community.algolia.com/magento/doc/m1/indexing/?utm_source=magento&utm_medium=extension&utm_campaign=magento_1&utm_term=shop-owner&utm_content=doc-link#facets">https://community.algolia.com/magento/doc/m1/indexing/#facets</a>
]]>
</comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ $algoliaJsConfig = array(
'products' => $this->__('Products'),
'searchBy' => $this->__('Search by'),
'showMore' => $this->__('Show more products'),
'searchForFacetValuesPlaceholder' => $this->__('Search for other ...'),
),
);

Expand Down
3 changes: 2 additions & 1 deletion app/locale/en_US/Algolia_Algoliasearch.csv
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@
"Input", "Input"
"Synonyms", "Synonyms"
"One-way Synonyms", "One-way Synonyms"
"Synonyms File", "Synonyms File"
"Synonyms File", "Synonyms File"
"Search for other ...","Search for other ..."
27 changes: 23 additions & 4 deletions js/algoliasearch/instantsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
}

if (facet.type === 'conjunctive') {
return ['refinementList', {
var refinementListOptions = {
container: facet.wrapper.appendChild(createISWidgetContainer(facet.attribute)),
attributeName: facet.attribute,
limit: algoliaConfig.maxValuesPerFacet,
Expand All @@ -393,11 +393,14 @@ document.addEventListener("DOMContentLoaded", function (event) {
cssClasses: {
root: 'facet conjunctive'
}
}];
};

refinementListOptions = addSearchForFacetValues(facet, refinementListOptions);
return ['refinementList', refinementListOptions];
}

if (facet.type === 'disjunctive') {
return ['refinementList', {
var refinementListOptions = {
container: facet.wrapper.appendChild(createISWidgetContainer(facet.attribute)),
attributeName: facet.attribute,
limit: algoliaConfig.maxValuesPerFacet,
Expand All @@ -406,7 +409,10 @@ document.addEventListener("DOMContentLoaded", function (event) {
cssClasses: {
root: 'facet disjunctive'
}
}];
};

refinementListOptions = addSearchForFacetValues(facet, refinementListOptions);
return ['refinementList', refinementListOptions];
}

if (facet.type === 'slider') {
Expand Down Expand Up @@ -544,4 +550,17 @@ document.addEventListener("DOMContentLoaded", function (event) {

search.addWidget(algoliaBundle.instantsearch.widgets[type](config));
}

function addSearchForFacetValues(facet, options) {
if (facet.searchable === '1') {
options['searchForFacetValues'] = {
placeholder: algoliaConfig.translations.searchForFacetValuesPlaceholder,
templates: {
noResults: '<div class="sffv-no-results">' + algoliaConfig.translations.noResults + '</div>'
}
};
}

return options;
}
});
40 changes: 40 additions & 0 deletions skin/frontend/base/default/algoliasearch/algoliasearch.css
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,46 @@ a.ais-current-refined-values--link:hover
color: #666;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__wrapper {
width: 94%;
margin: 0 auto 5px;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__submit {
display: none;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__reset {
width: 10px;
margin-top: -30px;
margin-right: 7px;
background: none;
border: none;
box-shadow: none;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__reset:hover {
background: none;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__reset svg {
width: 15px;
height: 15px;
fill: #636363;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__input {
border : 1px solid silver;
}

#algolia_instant_selector .ais-refinement-list--list .sbx-sffv__input:focus {
border : 1px solid #54A5CD;
}

#algolia_instant_selector .ais-refinement-list--list .sffv-no-results {
padding-left: 3%;
}

/* RANGE SLIDER */

.ais-range-slider--target {
Expand Down
2 changes: 2 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function testFacets()
$attributesMatched++;
} elseif ($facet['attribute'] === $indexFacet) {
$attributesMatched++;
} elseif ($facet['attribute'] === 'color' && 'searchable(color)' === $indexFacet) {
$attributesMatched++;
}
}
}
Expand Down