<commit_before><?php

use Elasticsearch\Client;

/**
 * phpMyFAQ Elasticsearch based search classes.
 *
 * PHP Version 5.5
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @category  phpMyFAQ
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015-2016 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2015-12-25
 */
if (!defined('IS_VALID_PHPMYFAQ')) {
    exit();
}

/**
 * PMF_Search_Elasticsearch.
 *
 * @category  phpMyFAQ
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015-2016 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2015-12-25
 */
class PMF_Search_Elasticsearch extends PMF_Search_Abstract implements PMF_Search_Interface
{
    /** @var Client */
    private $client = null;

    /** @var array */
    private $esConfig = [];

    /** @var string */
    private $language = '';

    /** @var array */
    private $categoryIds = [];

    /**
     * Constructor.
     *
     * @param PMF_Configuration
     */
    public function __construct(PMF_Configuration $config)
    {
        parent::__construct($config);

        $this->client = $this->_config->getElasticsearch();
        $this->esConfig = $this->_config->getElasticsearchConfig();
    }

    /**
     * Prepares the search and executes it.
     *
     * @param string $searchTerm Search term
     *
     * @throws PMF_Search_Exception
     *
     * @return array
     */
    public function search($searchTerm)
    {
        $searchParams = [
            'index' => $this->esConfig['index'],
            'type' => $this->esConfig['type'],
            'size' => 1000,
            'body' => [
                'query' => [
                    'filtered' => [
                        'filter' => [
                            'bool' => [
                                'must' => [
                                    [
                                        'terms' => ['category_id' => $this->getCategoryIds()]
                                    ]
                                ]
                            ]
                        ],
                        'query' => [
                            'multi_match' => [
                                'fields' => [
                                    'question', 'answer', 'keywords'
                                ],
                                'query' => $searchTerm,
                                'fuzziness' => 'AUTO'
                            ]
                        ]
                    ]
                ]
            ]
        ];

        $result = $this->client->search($searchParams);

        if (0 !== $result['hits']['total']) {

            foreach ($result['hits']['hits'] as $hit) {
                $resultSet = new stdClass();
                $resultSet->id = $hit['_source']['id'];
                $resultSet->lang = $hit['_source']['lang'];
                $resultSet->question = $hit['_source']['question'];
                $resultSet->answer = $hit['_source']['answer'];
                $resultSet->keywords = $hit['_source']['keywords'];
                $resultSet->category_id = $hit['_source']['category_id'];
                $resultSet->score = $hit['_score'];

                $this->resultSet[] = $resultSet;
            }

        } else {
            $this->resultSet = [];
        }

        return $this->resultSet;
    }

    /**
     * Prepares the autocomplete search and executes it.
     *
     * @param string $searchTerm Search term for autocompletion
     *
     * @throws PMF_Search_Exception
     *
     * @return array
     */
    public function autoComplete($searchTerm)
    {
        $searchParams = [
            'index' => $this->esConfig['index'],
            'type' => $this->esConfig['type'],
            'size' => 1000,
            'body' => [
                'query' => [
                    'filtered' => [
                        'filter' => [
                            'term' => [
                                'lang' => $this->getLanguage()
                            ]
                        ],
                        'query' => [
                            'multi_match' => [
                                'fields' => [
                                    'question', 'answer', 'keywords'
                                ],
                                'query' => $searchTerm,<commit_msg>Reduced Elasticsearch result size<commit_after><?php

use Elasticsearch\Client;

/**
 * phpMyFAQ Elasticsearch based search classes.
 *
 * PHP Version 5.5
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @category  phpMyFAQ
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015-2016 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2015-12-25
 */
if (!defined('IS_VALID_PHPMYFAQ')) {
    exit();
}

/**
 * PMF_Search_Elasticsearch.
 *
 * @category  phpMyFAQ
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015-2016 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      http://www.phpmyfaq.de
 * @since     2015-12-25
 */
class PMF_Search_Elasticsearch extends PMF_Search_Abstract implements PMF_Search_Interface
{
    /** @var Client */
    private $client = null;

    /** @var array */
    private $esConfig = [];

    /** @var string */
    private $language = '';

    /** @var array */
    private $categoryIds = [];

    /**
     * Constructor.
     *
     * @param PMF_Configuration
     */
    public function __construct(PMF_Configuration $config)
    {
        parent::__construct($config);

        $this->client = $this->_config->getElasticsearch();
        $this->esConfig = $this->_config->getElasticsearchConfig();
    }

    /**
     * Prepares the search and executes it.
     *
     * @param string $searchTerm Search term
     *
     * @throws PMF_Search_Exception
     *
     * @return array
     */
    public function search($searchTerm)
    {
        $searchParams = [
            'index' => $this->esConfig['index'],
            'type' => $this->esConfig['type'],
            'size' => 100,
            'body' => [
                'query' => [
                    'filtered' => [
                        'filter' => [
                            'bool' => [
                                'must' => [
                                    [
                                        'terms' => ['category_id' => $this->getCategoryIds()]
                                    ]
                                ]
                            ]
                        ],
                        'query' => [
                            'multi_match' => [
                                'fields' => [
                                    'question', 'answer', 'keywords'
                                ],
                                'query' => $searchTerm,
                                'fuzziness' => 'AUTO'
                            ]
                        ]
                    ]
                ]
            ]
        ];

        $result = $this->client->search($searchParams);

        if (0 !== $result['hits']['total']) {

            foreach ($result['hits']['hits'] as $hit) {
                $resultSet = new stdClass();
                $resultSet->id = $hit['_source']['id'];
                $resultSet->lang = $hit['_source']['lang'];
                $resultSet->question = $hit['_source']['question'];
                $resultSet->answer = $hit['_source']['answer'];
                $resultSet->keywords = $hit['_source']['keywords'];
                $resultSet->category_id = $hit['_source']['category_id'];
                $resultSet->score = $hit['_score'];

                $this->resultSet[] = $resultSet;
            }

        } else {
            $this->resultSet = [];
        }

        return $this->resultSet;
    }

    /**
     * Prepares the auto complete search and executes it.
     *
     * @param string $searchTerm Search term for autoc ompletion
     *
     * @throws PMF_Search_Exception
     *
     * @return array
     */
    public function autoComplete($searchTerm)
    {
        $searchParams = [
            'index' => $this->esConfig['index'],
            'type' => $this->esConfig['type'],
            'size' => 100,
            'body' => [
                'query' => [
                    'filtered' => [
                        'filter' => [
                            'term' => [
                                'lang' => $this->getLanguage()
                            ]
                        ],
                        'query' => [
                            'multi_match' => [
                                'fields' => [
                                    'question', 'answer', 'keywords'
                                ],
                                'query' => $searchTerm,
{'fraction_non_alphanumeric': 0.1388412017167382, 'fraction_numerical': 0.011587982832618025, 'mean_word_length': 1.2224606580829758, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4320407', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2420, 'n_words': 490}