<commit_before><?php namespace Michaeljennings\Laralastica;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Michaeljennings\Laralastica\Events\IndexesWhenSaved;
use Michaeljennings\Laralastica\Events\RemovesDocumentWhenDeleted;

trait Searchable
{
    /**
     * The laralastica instance.
     *
     * @var Laralastica
     */
    protected $laralastica;

    /**
     * Boot the trait.
     */
    protected static function bootSearchable()
    {
        if ( ! is_null(static::$dispatcher)) {
            static::observe(new Observer(static::$dispatcher));
        }
    }

    /**
     * Return an array of attributes to be indexed.
     *
     * @param Model $model
     * @return array
     */
    public function getIndexableAttributes(Model $model)
    {
        return $model->getAttributes();
    }

    /**
     * Return an array of columns to be indexed with the column as the key and
     * the desired data type as the value.
     *
     * @return array
     */
    public function getSearchDataTypes()
    {
        return ['id' => 'int'];
    }

    /**
     * Get the elasticsearch type.
     *
     * @return string
     */
    public function getSearchType()
    {
        return $this->getTable();
    }

    /**
     * Return the search key value.
     *
     * @return mixed
     */
    public function getSearchKey()
    {
        return $this->getAttribute($this->getSearchKeyName());
    }

    /**
     * Return the key to be used when indexing a document.
     *
     * @return mixed
     */
    public function getSearchKeyName()
    {
        return $this->getKeyName();
    }

    /**
     * Get the full path for the search key.
     *
     * @return string
     */
    public function getRelativeSearchKey()
    {
        return $this->getTable() . '.' . $this->getSearchKeyName();
    }

    /**
     * Loop through the attributes and type cast them if neccesary.
     *
     * @param array $attributes
     * @return array
     */
    public function transformAttributes(array $attributes)
    {
        $searchDataTypes = $this->getSearchDataTypes();

        if ( ! empty($searchDataTypes)) {
            foreach ($attributes as $key => $attribute) {
                if (array_key_exists($key, $searchDataTypes)) {
                    switch ($searchDataTypes[$key]) {
                        case "int":
                            $attribute = (int)$attribute;
                            break;
                        case "integer":
                            $attribute = (int)$attribute;
                            break;
                        case "string":
                            $attribute = (string)$attribute;
                            break;
                        case "float":
                            $attribute = (float)$attribute;
                            break;
                        case "bool":
                            $attribute = (bool)$attribute;
                            break;
                        case "boolean":
                            $attribute = (bool)$attribute;
                            break;
                    }

                    $attributes[$key] = $attribute;
                }
            }
        }

        return $attributes;
    }

    /**
     * Run the provided query on the elastic search index and then run a where
     * in.
     *
     * @param callable    $query
     * @param callable    $searchQuery
     * @param string|null $key
     * @param bool        $sortByResults
     * @return mixed
     */
    public function scopeSearch($query, callable $searchQuery, $key = null, $sortByResults = true)
    {
        if ( ! isset($this->laralastica)) {
            $this->laralastica = laralastica();
        }

        $results = $this->laralastica->search($this->getSearchType(), $searchQuery);
        $searchKey = $key ?: $this->getSearchKeyName();
        $values = $results->map(function ($result) {
            return $result->{$this->getSearchKeyName()};<commit_msg>Update searchable scope to use relative search key<commit_after><?php namespace Michaeljennings\Laralastica;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Michaeljennings\Laralastica\Events\IndexesWhenSaved;
use Michaeljennings\Laralastica\Events\RemovesDocumentWhenDeleted;

trait Searchable
{
    /**
     * The laralastica instance.
     *
     * @var Laralastica
     */
    protected $laralastica;

    /**
     * Boot the trait.
     */
    protected static function bootSearchable()
    {
        if ( ! is_null(static::$dispatcher)) {
            static::observe(new Observer(static::$dispatcher));
        }
    }

    /**
     * Return an array of attributes to be indexed.
     *
     * @param Model $model
     * @return array
     */
    public function getIndexableAttributes(Model $model)
    {
        return $model->getAttributes();
    }

    /**
     * Return an array of columns to be indexed with the column as the key and
     * the desired data type as the value.
     *
     * @return array
     */
    public function getSearchDataTypes()
    {
        return ['id' => 'int'];
    }

    /**
     * Get the elasticsearch type.
     *
     * @return string
     */
    public function getSearchType()
    {
        return $this->getTable();
    }

    /**
     * Return the search key value.
     *
     * @return mixed
     */
    public function getSearchKey()
    {
        return $this->getAttribute($this->getSearchKeyName());
    }

    /**
     * Return the key to be used when indexing a document.
     *
     * @return mixed
     */
    public function getSearchKeyName()
    {
        return $this->getKeyName();
    }

    /**
     * Get the full path for the search key.
     *
     * @return string
     */
    public function getRelativeSearchKey()
    {
        return $this->getTable() . '.' . $this->getSearchKeyName();
    }

    /**
     * Loop through the attributes and type cast them if neccesary.
     *
     * @param array $attributes
     * @return array
     */
    public function transformAttributes(array $attributes)
    {
        $searchDataTypes = $this->getSearchDataTypes();

        if ( ! empty($searchDataTypes)) {
            foreach ($attributes as $key => $attribute) {
                if (array_key_exists($key, $searchDataTypes)) {
                    switch ($searchDataTypes[$key]) {
                        case "int":
                            $attribute = (int)$attribute;
                            break;
                        case "integer":
                            $attribute = (int)$attribute;
                            break;
                        case "string":
                            $attribute = (string)$attribute;
                            break;
                        case "float":
                            $attribute = (float)$attribute;
                            break;
                        case "bool":
                            $attribute = (bool)$attribute;
                            break;
                        case "boolean":
                            $attribute = (bool)$attribute;
                            break;
                    }

                    $attributes[$key] = $attribute;
                }
            }
        }

        return $attributes;
    }

    /**
     * Run the provided query on the elastic search index and then run a where
     * in.
     *
     * @param callable    $query
     * @param callable    $searchQuery
     * @param string|null $key
     * @param bool        $sortByResults
     * @return mixed
     */
    public function scopeSearch($query, callable $searchQuery, $key = null, $sortByResults = true)
    {
        if ( ! isset($this->laralastica)) {
            $this->laralastica = laralastica();
        }

        $results = $this->laralastica->search($this->getSearchType(), $searchQuery);
        $searchKey = $key ?: $this->getRelativeSearchKey();
        $values = $results->map(function ($result) {
            return $result->{$this->getSearchKeyName()};
{'fraction_non_alphanumeric': 0.10854816824966079, 'fraction_numerical': 0.0, 'mean_word_length': 1.521144278606965, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 41, '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': '3054251', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2106, 'n_words': 518}