<commit_before><?php

namespace Imbo\MetadataSearch\Backend;

use Imbo\MetadataSearch\Dsl\Transformations\ElasticSearchDsl,
    Imbo\MetadataSearch\Interfaces\SearchBackendInterface,
    Imbo\MetadataSearch\Interfaces\DslAstInterface,
    Imbo\MetadataSearch\Model\ElasticsearchResponse,
    Elasticsearch\Client as ElasticsearchClient,
    Imbo\Exception\RuntimeException;

/**
 * Elasticsearch search backend for metadata search
 *
 * @author <NAME> <<EMAIL>>
 */
class ElasticSearch implements SearchBackendInterface {
    /**
     * @var Elasticsearch\Client
     */
    protected $client;

    /**
     * @var array
     */
    protected $options = [
        'index' => ['name' => 'imbo_metadata'],
        'sort' => [],
    ];

    public function __construct(ElasticsearchClient $client, $options = null) {
        $this->client = $client;

        if (is_string($options)) {
            $this->options['index']['name'] = $options;
        } else if (is_array($options)) {
            $this->options = array_replace_recursive($this->options, $options);
        }

        if (empty($this->getIndexName())) {
            throw new RuntimeException(
                'Index name for elasticsearch metadata search backend must be given',
                503
            );
        }
    }

    /**<commit_msg>Fix use of `empty()` with return value (PHP 5.4)<commit_after><?php

namespace Imbo\MetadataSearch\Backend;

use Imbo\MetadataSearch\Dsl\Transformations\ElasticSearchDsl,
    Imbo\MetadataSearch\Interfaces\SearchBackendInterface,
    Imbo\MetadataSearch\Interfaces\DslAstInterface,
    Imbo\MetadataSearch\Model\ElasticsearchResponse,
    Elasticsearch\Client as ElasticsearchClient,
    Imbo\Exception\RuntimeException;

/**
 * Elasticsearch search backend for metadata search
 *
 * @author <NAME> <<EMAIL>>
 */
class ElasticSearch implements SearchBackendInterface {
    /**
     * @var Elasticsearch\Client
     */
    protected $client;

    /**
     * @var array
     */
    protected $options = [
        'index' => ['name' => 'imbo_metadata'],
        'sort' => [],
    ];

    public function __construct(ElasticsearchClient $client, $options = null) {
        $this->client = $client;

        if (is_string($options)) {
            $this->options['index']['name'] = $options;
        } else if (is_array($options)) {
            $this->options = array_replace_recursive($this->options, $options);
        }

        $indexName = $this->getIndexName();
        if (empty($indexName)) {
            throw new RuntimeException(
                'Index name for elasticsearch metadata search backend must be given',
                503
            );
        }
    }

    /**
{'fraction_non_alphanumeric': 0.12444279346210996, 'fraction_numerical': 0.0029717682020802376, 'mean_word_length': 2.614765100671141, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '3931661', 'n_tokens_mistral': 763, 'n_tokens_neox': 759, 'n_words': 152}