ErickTamayo / laravel-scout-elastic

Elastic Driver for Laravel Scout
MIT License
916 stars 242 forks source link

Search by integer not work #147

Closed Fanamurov closed 3 years ago

Fanamurov commented 4 years ago

Searchable model User.php

    public function toSearchableArray(): array
    {
        return [
            'id' => (string) $this->id,
            'client_id' => (string) $this->client_id,
            'email' => $this->email,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            //'type' => $this->type,
        ];
    }

'id' and 'client_id' is "int"

If I search by client ID, the search will not return anything.

But, if i execute query from console: curl 'http://localhost:9200/user/_search?pretty&q=2177' All is ok

Hotfix for my situation:

class ElasticsearchEngine

    protected function performSearch(Builder $builder, array $options = [])
    {
        // BUGFIX
        $int_check = (int)$builder->query;

        if (strlen($int_check) === strlen($builder->query)) {
            $query = $builder->query;
        } else {
            $query = '*' . $builder->query . '*';
        }

        $params = [
            'index' => $builder->model->searchableAs(),
            'type' => get_class($builder->model),
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [['query_string' => ['query' => "{$query}"]]]
                        //'must' => [['query_string' => ['query' => "*{$builder->query}*"]]]
                    ]
                ]
            ]
        ];
        // END BUGFIX

        if ($sort = $this->sort($builder)) {
            $params['body']['sort'] = $sort;
        }

        if (isset($options['from'])) {
            $params['body']['from'] = $options['from'];
        }

        if (isset($options['size'])) {
            $params['body']['size'] = $options['size'];
        }

        if (isset($options['numericFilters']) && count($options['numericFilters'])) {
            $params['body']['query']['bool']['must'] = array_merge(
                $params['body']['query']['bool']['must'],
                $options['numericFilters']
            );
        }

        if ($builder->callback) {
            return call_user_func(
                $builder->callback,
                $this->elastic,
                $builder->query,
                $params
            );
        }

        return $this->elastic->search($params);
    }
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.