<commit_before><?php

namespace Becklyn\SearchBundle\Elasticsearch;

use Becklyn\SearchBundle\Elasticsearch\Request\IndexDocumentRequest;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Becklyn\SearchBundle\Metadata\Metadata;


/**
 * Wrapper around the elasticsearch API
 */
class ElasticsearchClient
{
    const ENTITY_ID_FIELD = "entity-id";
    const ENTITY_TIMESTAMP_FIELD = "entity-timestamp";

    /**
     * @var Client
     */
    private $client;


    /**
     * @var string
     */
    private $index;


    /**
     * @var Metadata
     */
    private $metadata;



    /**
     * @param string   $host
     * @param string   $index
     * @param Metadata $metadata
     */
    public function __construct (string $host, string $index, Metadata $metadata)
    {
        $this->client = ClientBuilder::create()
            ->setHosts([$host])
            ->build();

        $this->index = $index;
        $this->metadata = $metadata;
    }



    /**
     * Sends the given request to elastic search
     *
     * @param ElasticsearchRequest $request
     *
     * @throws Missing404Exception
     */
    public function sendRequest (ElasticsearchRequest $request)
    {
        try
        {
            $client = $this->client;
            $namespace = $request->getActionNamespace();
            $action = $request->getAction();

            if (null !== $namespace)
            {
                $client = $client->{$namespace}();
            }

            $client->{$action}($request->getData());
        }
        catch (Missing404Exception $exception)
        {
            if (!$request->ignoreMissing())
            {
                throw $exception;
            }
        }
    }



    /**
     * Sends all given requests to elastic search
     *
     * @param ElasticsearchRequest[] $requests
     */
    public function sendRequests (array $requests)
    {
        foreach ($requests as $request)
        {
            $this->sendRequest($request);
        }
    }



    /**
     * Sends index requests in bulk
     *
     * @param IndexDocumentRequest[] $requests
     */
    public function sendBulkIndexRequests (array $requests)
    {
        $currentBulk = [];
        $maxIndex = count($requests) - 1;

        for ($i = 0; $i <= $maxIndex; $i++)
        {
            $request = $requests[$i];
            $data = $request->getData();

            // add header
            $currentBulk[] = [
                "index" => [
                    "_index" => $data["index"],
                    "_type" => $data["type"],
                    "_id" => $data["id"],
                ],
            ];

            // add data
            $currentBulk[] = $data["body"];

            // every 1000 items -> send
            if ($i % 1000 === 0 || $i >= $maxIndex)
            {
                $this->client->bulk([
                    "body" => $currentBulk,
                ]);
                $currentBulk = [];
            }
        }
    }



    /**
     * Searches with the given parameters
     *
     * @param string[] $types
     * @param array    $body
     *
     * @return array
     */
    public function search (array $types, array $body) : array
    {
        $results = $this->client->search([
            "index" => $this->index,
            "type" => $types,
            "body" => $body,
        ]);

        return $results["hits"];
    }
}
<commit_msg>Use consistent return values in the ES client<commit_after><?php

namespace Becklyn\SearchBundle\Elasticsearch;

use Becklyn\SearchBundle\Elasticsearch\Request\IndexDocumentRequest;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Becklyn\SearchBundle\Metadata\Metadata;


/**
 * Wrapper around the elasticsearch API
 */
class ElasticsearchClient
{
    const ENTITY_ID_FIELD = "entity-id";
    const ENTITY_TIMESTAMP_FIELD = "entity-timestamp";

    /**
     * @var Client
     */
    private $client;


    /**
     * @var string
     */
    private $index;


    /**
     * @var Metadata
     */
    private $metadata;



    /**
     * @param string   $host
     * @param string   $index
     * @param Metadata $metadata
     */
    public function __construct (string $host, string $index, Metadata $metadata)
    {
        $this->client = ClientBuilder::create()
            ->setHosts([$host])
            ->build();

        $this->index = $index;
        $this->metadata = $metadata;
    }



    /**
     * Sends the given request to elastic search
     *
     * @param ElasticsearchRequest $request
     *
     * @return array|null
     * @throws Missing404Exception
     */
    public function sendRequest (ElasticsearchRequest $request)
    {
        try
        {
            $client = $this->client;
            $namespace = $request->getActionNamespace();
            $action = $request->getAction();

            if (null !== $namespace)
            {
                $client = $client->{$namespace}();
            }

            return $client->{$action}($request->getData());
        }
        catch (Missing404Exception $exception)
        {
            if (!$request->ignoreMissing())
            {
                throw $exception;
            }

            return null;
        }
    }



    /**
     * Sends all given requests to elastic search
     *
     * @param ElasticsearchRequest[] $requests
     *
     * @return array
     */
    public function sendRequests (array $requests)
    {
        $results = [];

        foreach ($requests as $request)
        {
            $results[] = $this->sendRequest($request);
        }

        return $results;
    }



    /**
     * Sends index requests in bulk
     *
     * @param IndexDocumentRequest[] $requests
     */
    public function sendBulkIndexRequests (array $requests)
    {
        $currentBulk = [];
        $maxIndex = count($requests) - 1;

        for ($i = 0; $i <= $maxIndex; $i++)
        {
            $request = $requests[$i];
            $data = $request->getData();

            // add header
            $currentBulk[] = [
                "index" => [
                    "_index" => $data["index"],
                    "_type" => $data["type"],
                    "_id" => $data["id"],
                ],
            ];

            // add data
            $currentBulk[] = $data["body"];

            // every 1000 items -> send
            if ($i % 1000 === 0 || $i >= $maxIndex)
            {
                $this->client->bulk([
                    "body" => $currentBulk,
                ]);
                $currentBulk = [];
            }
        }
    }



    /**
     * Searches with the given parameters
     *
     * @param string[] $types
     * @param array    $body
     *
     * @return array
     */
    public function search (array $types, array $body) : array
    {
        $results = $this->client->search([
            "index" => $this->index,
            "type" => $types,
            "body" => $body,
        ]);

        return $results["hits"];
    }
}

{'fraction_non_alphanumeric': 0.13387594723547572, 'fraction_numerical': 0.005613247263541959, 'mean_word_length': 1.5426328933285764, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, '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': '2748125', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1928, 'n_words': 432}