<commit_before><?php

if (class_exists('AlgoliaSearch\Client', false) == false)
{
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/Version.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/AlgoliaException.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/ClientContext.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/Client.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/Index.php';
}

class Algolia_Algoliasearch_Helper_Data extends Mage_Core_Helper_Abstract
{
    const COLLECTION_PAGE_SIZE = 100;

    protected $algolia_helper;

    protected $page_helper;
    protected $category_helper;
    protected $product_helper;

    protected $config;

    public function __construct()
    {
        \AlgoliaSearch\Version::$custom_value = " Magento (1.4.4)";

        $this->algolia_helper               = Mage::helper('algoliasearch/algoliahelper');

        $this->page_helper                  = Mage::helper('algoliasearch/entity_pagehelper');
        $this->category_helper              = Mage::helper('algoliasearch/entity_categoryhelper');
        $this->product_helper               = Mage::helper('algoliasearch/entity_producthelper');
        $this->suggestion_helper            = Mage::helper('algoliasearch/entity_suggestionhelper');
        $this->additionalsections_helper    = Mage::helper('algoliasearch/entity_additionalsectionshelper');

        $this->config                       = Mage::helper('algoliasearch/config');
    }

    public function deleteProductsAndCategoriesStoreIndices($storeId = null)
    {
        $this->algolia_helper->deleteIndex($this->product_helper->getIndexName($storeId));
        $this->algolia_helper->deleteIndex($this->category_helper->getIndexName($storeId));
    }

    public function saveConfigurationToAlgolia($storeId = null)
    {
        $this->algolia_helper->resetCredentialsFromConfig();

        $this->algolia_helper->setSettings($this->category_helper->getIndexName($storeId), $this->category_helper->getIndexSettings($storeId));
        $this->algolia_helper->setSettings($this->page_helper->getIndexName($storeId), $this->page_helper->getIndexSettings($storeId));
        $this->algolia_helper->setSettings($this->suggestion_helper->getIndexName($storeId), $this->suggestion_helper->getIndexSettings($storeId));

        foreach ($this->config->getAutocompleteAdditionnalSections() as $section)
            $this->algolia_helper->setSettings($this->additionalsections_helper->getIndexName($storeId).'_'.$section['attribute'], $this->additionalsections_helper->getIndexSettings($storeId));

        $this->product_helper->setSettings($storeId);
    }

    public function getSearchResult($query, $storeId)
    {
        $resultsLimit = $this->config->getResultsLimit($storeId);

        $index_name = $this->product_helper->getIndexName($storeId);

        $answer = $this->algolia_helper->query($index_name, $query, array(
            'hitsPerPage' => max(5, min($resultsLimit, 1000)), // retrieve all the hits (hard limit is 1000)
            'attributesToRetrieve' => 'objectID',<commit_msg>improve performance for backend search when instant enabled<commit_after><?php

if (class_exists('AlgoliaSearch\Client', false) == false)
{
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/Version.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/AlgoliaException.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/ClientContext.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/Client.php';
    require_once Mage::getBaseDir('lib').'/AlgoliaSearch/Index.php';
}

class Algolia_Algoliasearch_Helper_Data extends Mage_Core_Helper_Abstract
{
    const COLLECTION_PAGE_SIZE = 100;

    protected $algolia_helper;

    protected $page_helper;
    protected $category_helper;
    protected $product_helper;

    protected $config;

    public function __construct()
    {
        \AlgoliaSearch\Version::$custom_value = " Magento (1.4.4)";

        $this->algolia_helper               = Mage::helper('algoliasearch/algoliahelper');

        $this->page_helper                  = Mage::helper('algoliasearch/entity_pagehelper');
        $this->category_helper              = Mage::helper('algoliasearch/entity_categoryhelper');
        $this->product_helper               = Mage::helper('algoliasearch/entity_producthelper');
        $this->suggestion_helper            = Mage::helper('algoliasearch/entity_suggestionhelper');
        $this->additionalsections_helper    = Mage::helper('algoliasearch/entity_additionalsectionshelper');

        $this->config                       = Mage::helper('algoliasearch/config');
    }

    public function deleteProductsAndCategoriesStoreIndices($storeId = null)
    {
        $this->algolia_helper->deleteIndex($this->product_helper->getIndexName($storeId));
        $this->algolia_helper->deleteIndex($this->category_helper->getIndexName($storeId));
    }

    public function saveConfigurationToAlgolia($storeId = null)
    {
        $this->algolia_helper->resetCredentialsFromConfig();

        $this->algolia_helper->setSettings($this->category_helper->getIndexName($storeId), $this->category_helper->getIndexSettings($storeId));
        $this->algolia_helper->setSettings($this->page_helper->getIndexName($storeId), $this->page_helper->getIndexSettings($storeId));
        $this->algolia_helper->setSettings($this->suggestion_helper->getIndexName($storeId), $this->suggestion_helper->getIndexSettings($storeId));

        foreach ($this->config->getAutocompleteAdditionnalSections() as $section)
            $this->algolia_helper->setSettings($this->additionalsections_helper->getIndexName($storeId).'_'.$section['attribute'], $this->additionalsections_helper->getIndexSettings($storeId));

        $this->product_helper->setSettings($storeId);
    }

    public function getSearchResult($query, $storeId)
    {
        $resultsLimit = $this->config->getResultsLimit($storeId);

        $index_name = $this->product_helper->getIndexName($storeId);

        $number_of_results = 1000;

        if ($this->config->isInstantEnabled())
            $number_of_results = min($this->config->getNumberOfProductResults($storeId), 1000);

        $answer = $this->algolia_helper->query($index_name, $query, array(
            'hitsPerPage' => $number_of_results, // retrieve all the hits (hard limit is 1000)
            'attributesToRetrieve' => 'objectID',
{'fraction_non_alphanumeric': 0.1393188854489164, 'fraction_numerical': 0.005108359133126935, 'mean_word_length': 4.140015910898966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3700780', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1821, 'n_words': 215}