<commit_before><?php

namespace Basemkhirat\Elasticsearch;

use Elasticsearch\ClientBuilder;

/**
 * Class Connection
 * @package Basemkhirat\Elasticsearch
 */
class Connection
{

    /**
     * Laravel app instance
     * @var \Illuminate\Foundation\Application|mixed
     */
    protected $app;

    /**
     * Elastic config content
     * @var
     */
    protected $config;

    /**
     * The current connection
     * @var
     */
    protected $connection;

    /**
     * all available connections
     * @var array
     */
    protected $connections = [];


    /**
     * Connection constructor.
     */
    function __construct()
    {
        $this->app = app();

        $this->config = $this->app['config']['es'];
    }

    /**
     * Create a native connection
     * suitable for any non-laravel or non-lumen apps
     * any composer based frameworks
     * @param $config
     * @return Query
     */
    public static function create($config)
    {

        $clientBuilder = ClientBuilder::create();

        if (!empty($config['handler'])) {
            $clientBuilder->setHandler($config['handler']);
        }

        $clientBuilder->setHosts($config["servers"]);

        $clientBuilder = self::configureLogging($clientBuilder,$config);

        $query = new Query($clientBuilder->build());

        if (array_key_exists("index", $config) and $config["index"] != "") {
            $query->index($config["index"]);
        }

        return $query;
    }


    /**
     * Create a connection for laravel or lumen frameworks
     * @param $name
     * @return Query
     */
    function connection($name)
    {

        // Check if connection is already loaded.

        if ($this->isLoaded($name)) {

            $this->connection = $this->connections[$name];

            return $this->newQuery($name);

        }

        // Create a new connection.

        if (array_key_exists($name, $this->config["connections"])) {

            $config = $this->config["connections"][$name];

            // Instantiate a new ClientBuilder
            $clientBuilder = ClientBuilder::create();

            $clientBuilder->setHosts($config["servers"]);

            $clientBuilder = self::configureLogging($clientBuilder,$config);

            if (!empty($config['handler'])) {
                $clientBuilder->setHandler($config['handler']);
            }

            // Build the client object
            $connection = $clientBuilder->build();

            $this->connection = $connection;

            $this->connections[$name] = $connection;

            return $this->newQuery($name);
        }

        $this->app->abort(500, "Invalid elasticsearch connection driver `" . $name . "`");
    }


    /**
     * @param ClientBuilder $clientBuilder
     * @param array $config
     * @return ClientBuilder
     */
    public static function configureLogging(ClientBuilder $clientBuilder, array $config)
    {
        if (array_get($config,'logging.enabled')) {
            $logger = ClientBuilder::defaultLogger(array_get($config,'logging.location'), array_get($config,'logging.level','all'));
            $clientBuilder->setLogger($logger);
        }
        return $clientBuilder;
    }


    /**
     * route the request to the query class
     * @param $connection
     * @return Query
     */
    function newQuery($connection)
    {
<commit_msg>fix compatibility for laravel 6<commit_after><?php

namespace Basemkhirat\Elasticsearch;

use Elasticsearch\ClientBuilder;
use Illuminate\Support\Arr;

/**
 * Class Connection
 * @package Basemkhirat\Elasticsearch
 */
class Connection
{

    /**
     * Laravel app instance
     * @var \Illuminate\Foundation\Application|mixed
     */
    protected $app;

    /**
     * Elastic config content
     * @var
     */
    protected $config;

    /**
     * The current connection
     * @var
     */
    protected $connection;

    /**
     * all available connections
     * @var array
     */
    protected $connections = [];


    /**
     * Connection constructor.
     */
    function __construct()
    {
        $this->app = app();

        $this->config = $this->app['config']['es'];
    }

    /**
     * Create a native connection
     * suitable for any non-laravel or non-lumen apps
     * any composer based frameworks
     * @param $config
     * @return Query
     */
    public static function create($config)
    {

        $clientBuilder = ClientBuilder::create();

        if (!empty($config['handler'])) {
            $clientBuilder->setHandler($config['handler']);
        }

        $clientBuilder->setHosts($config["servers"]);

        $clientBuilder = self::configureLogging($clientBuilder,$config);

        $query = new Query($clientBuilder->build());

        if (array_key_exists("index", $config) and $config["index"] != "") {
            $query->index($config["index"]);
        }

        return $query;
    }


    /**
     * Create a connection for laravel or lumen frameworks
     * @param $name
     * @return Query
     */
    function connection($name)
    {

        // Check if connection is already loaded.

        if ($this->isLoaded($name)) {

            $this->connection = $this->connections[$name];

            return $this->newQuery($name);

        }

        // Create a new connection.

        if (array_key_exists($name, $this->config["connections"])) {

            $config = $this->config["connections"][$name];

            // Instantiate a new ClientBuilder
            $clientBuilder = ClientBuilder::create();

            $clientBuilder->setHosts($config["servers"]);

            $clientBuilder = self::configureLogging($clientBuilder,$config);

            if (!empty($config['handler'])) {
                $clientBuilder->setHandler($config['handler']);
            }

            // Build the client object
            $connection = $clientBuilder->build();

            $this->connection = $connection;

            $this->connections[$name] = $connection;

            return $this->newQuery($name);
        }

        $this->app->abort(500, "Invalid elasticsearch connection driver `" . $name . "`");
    }


    /**
     * @param ClientBuilder $clientBuilder
     * @param array $config
     * @return ClientBuilder
     */
    public static function configureLogging(ClientBuilder $clientBuilder, array $config)
    {
        if (Arr::get($config,'logging.enabled')) {
            $logger = ClientBuilder::defaultLogger(Arr::get($config,'logging.location'), Arr::get($config,'logging.level','all'));
            $clientBuilder->setLogger($logger);
        }
        return $clientBuilder;
    }


    /**
     * route the request to the query class
     * @param $connection
     * @return Query
     */
    function newQuery($connection)
    {

{'fraction_non_alphanumeric': 0.14117126419825934, 'fraction_numerical': 0.0010326006785661601, 'mean_word_length': 2.1905882352941175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '2596844', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1764, 'n_words': 382}