<commit_before><?php namespace Iverberk\Larasearch;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Iverberk\Larasearch\Exceptions\ImportException;

class Index {

	/**
	 * Index name
	 *
	 * @var string
	 */
	private $name;

	/**
	 * Elasticsearch client
	 *
	 * @var \Elasticsearch\Client
	 */
	private static $client;

	/**
	 * Index parameters
	 *
	 * @var array
	 */
	private $params;

	/**
	 * Larasearch Eloquent proxy
	 *
	 * @var Proxy
	 */
	private $proxy;

	/**
	 * Retrieve the ElasticSearch Client
	 *
	 * @return \Elasticsearch\Client
	 */
	private static function getClient()
	{
        return self::$client;
	}


	/**
	 * @param string $name
	 * @param Proxy  $proxy
	 */
	public function __construct(Proxy $proxy, $name = '')
	{
		self::$client = App::make('Elasticsearch');

		$this->setProxy($proxy);
		$this->setName($name ?: $proxy->getModel()->getTable());
	}

	/**
	 * Import an Eloquent
	 *
	 * @param Model	$model
	 * @param array	$relations
	 * @param int	  $batchSize
	 * @param callable $callback
	 * @internal param $type
	 */
	public function import(Model $model, $relations = [], $batchSize = 750, Callable $callback = null)
	{
		$batch = 0;

		while (true)
		{
			// Increase the batch number
			$batch += 1;

			// Load records from the database
			$records = $model
				->with($relations)
				->skip($batchSize * ($batch - 1))
				->take($batchSize)
				->get();

			// Break out of the loop if we are out of records
			if (count($records) == 0) break;

			// Call the callback function to provide feedback on the import process
			if ($callback)
			{
				$callback($batch);
			}

			// Transform each record before sending it to Elasticsearch
			$data = [];

			foreach ($records as $record)
			{
				$data[] = [
					'index' => [
						'_id' => $record->getEsId()
					]
				];

				$data[] = $record->transform();
			}

			// Bulk import the data to Elasticsearch
			$this->bulk($data);
		}
	}

	/**
	 * Set index name
	 *
	 * @param string
	 * @return Index
	 */
	public function setName($name)<commit_msg>Reindex with relations if they are found<commit_after><?php namespace Iverberk\Larasearch;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Iverberk\Larasearch\Exceptions\ImportException;

class Index {

	/**
	 * Index name
	 *
	 * @var string
	 */
	private $name;

	/**
	 * Elasticsearch client
	 *
	 * @var \Elasticsearch\Client
	 */
	private static $client;

	/**
	 * Index parameters
	 *
	 * @var array
	 */
	private $params;

	/**
	 * Larasearch Eloquent proxy
	 *
	 * @var Proxy
	 */
	private $proxy;

	/**
	 * Retrieve the ElasticSearch Client
	 *
	 * @return \Elasticsearch\Client
	 */
	private static function getClient()
	{
        return self::$client;
	}


	/**
	 * @param string $name
	 * @param Proxy  $proxy
	 */
	public function __construct(Proxy $proxy, $name = '')
	{
		self::$client = App::make('Elasticsearch');

		$this->setProxy($proxy);
		$this->setName($name ?: $proxy->getModel()->getTable());
	}

	/**
	 * Import an Eloquent
	 *
	 * @param Model	$model
	 * @param array	$relations
	 * @param int	  $batchSize
	 * @param callable $callback
	 * @internal param $type
	 */
	public function import(Model $model, $relations = [], $batchSize = 750, Callable $callback = null)
	{
		$batch = 0;

		while (true)
		{
			// Increase the batch number
			$batch += 1;

			// Load records from the database
			$records = $model
				->with($relations)
				->skip($batchSize * ($batch - 1))
				->take($batchSize)
				->get();

			// Break out of the loop if we are out of records
			if (count($records) == 0) break;

			// Call the callback function to provide feedback on the import process
			if ($callback)
			{
				$callback($batch);
			}

			// Transform each record before sending it to Elasticsearch
			$data = [];

			foreach ($records as $record)
			{
				$data[] = [
					'index' => [
						'_id' => $record->getEsId()
					]
				];

				$data[] = $record->transform( ! empty($relations));
			}

			// Bulk import the data to Elasticsearch
			$this->bulk($data);
		}
	}

	/**
	 * Set index name
	 *
	 * @param string
	 * @return Index
	 */
	public function setName($name)
{'fraction_non_alphanumeric': 0.14855824682814303, 'fraction_numerical': 0.003229527104959631, 'mean_word_length': 2.7868995633187774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '3202580', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1393, 'n_words': 376}