Get Data From Elasticsearch

@param string $model Yii2 model
@param string  $query Select query to get data
@return Result from database
978784
public function getData($model, $query = null)
    {
        $data = [];
        foreach ($model as $key => $value) {
            $data[$key] = $value;
        }
        return $data;
    }
1277557
public function apply(Query $query) : Query
    {
        $method = is_array($this->value) ? 'in' : 'eq';

        return $query->where(
            (new QueryExpression())->{$method}(
                $this->field,
                [] === $this->value ? '' : $this->value,
                $query->getTypeMap()->type($this->field)
            )
        );
    }
254346
private static function getDataInstance(ActiveQuery $query)
    {
        $class = $query->modelClass;
        
        $keys = [$class];
        if ($query->where) {
            foreach ($query->where as $v) {
                if (is_scalar($v)) {
                    $keys[] = $v;
                }
            }
        }

        $instanceName = implode(",", $keys);
        
        if (!isset(static::$_dataInstance[$instanceName])) {
            $queryData = $query->all();
            static::$_dataInstance[$instanceName] = $queryData;
        }
        
        return static::$_dataInstance[$instanceName];
    }
1144807
public function getQuery($index, $field, &$result) {
		$con = null;
		if (isset($this->queries[ $field ])) {
			$con = $this->queries[ $field ];
		} else if (method_exists($this->view, $field)) {
			$con = $this->view->{$field}();
			/**@var Query $query */
			list($query, $fk, $lk) = $con;
			$query->where([$fk => $result[ $index ][ $lk ]]);
			$this->queries[ $field ] = $con;
		}

		if ($con) {
			/**@var Query $query */
			list($query, $fk, $lk) = $con;
			if (isset($result[ $index ][ $lk ])) {
				$idv = $result[ $index ][ $lk ];
				$query->updateWhereData([$fk => $idv]);

				return $query;
			}
		}

		return null;
	}
859852
public function _browse($query, $parameters = [])
    {
        /** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
        $modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');

        $index = null;

        if (isset($parameters['index'])) {
            $index = $modelHelper->getIndices($this, $parameters['index'])[0];
            unset($parameters['index']);
        } else {
            $index = $modelHelper->getIndices($this)[0];
        }

        $result = $index->browse($query, $parameters);

        return $result;
    }
182870
private function getQuery(Model $Model, $action)
	{
		$queries = $this->QueryLogSource->getModelQueries($Model, $action);
		$statement = '';

		if (!empty($queries)) {
			$statement = implode(";\n", $queries);
		}

		return $statement;
	}
1074868
public function read($query, array $options = []) {
		$this->_checkConnection();

		$defaults = ['return' => 'resource'];
		$options += $defaults;

		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$prefix = $this->_config['gridPrefix'];

			$query = $params['query'];
			$options = $params['options'];
			$args = $query->export($this);
			$source = $args['source'];
			$model = $query->model();

			if ($group = $args['group']) {
				$result = $this->_group($group, $args, $options);
				$config = ['class' => 'set', 'defaults' => false] + compact('query') + $result;
				return $model::create($config['data'], $config);
			}
			$collection = $this->connection->{$source};

			if ($source === "{$prefix}.files") {
				$collection = $this->connection->getGridFS($prefix);
			}
			$result = $collection->find($args['conditions'], $args['fields']);

			if ($query->calculate()) {
				return $result;
			}

			$resource = $result->sort($args['order'])->limit($args['limit'])->skip($args['offset']);
			$result = $this->_instance('result', compact('resource'));
			$config = compact('result', 'query') + ['class' => 'set', 'defaults' => false];
			return $model::create([], $config);
		});
	}
1199258
public function read($query, array $options = []) {
		$defaults = ['return' => 'resource', 'model' => $query->model()];
		$options += $defaults;
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$query = $params['query'];
			$params = $query->export($this);

			list($_path, $conditions) = (array) $params['conditions'];
			$model = $query->model();

			if (empty($_path)) {
				$_path = '_all_docs';
				$conditions['include_docs'] = 'true';
			}
			$path = "{$this->_config['database']}/{$_path}";
			$args = (array) $conditions + (array) $params['limit'] + (array) $params['order'];

			$result = $this->connection->get($path, $args);
			$result = is_string($result) ? json_decode($result, true) : $result;

			$data = $stats = [];

			if (isset($result['_id'])) {
				$data = [$result];
			} elseif (isset($result['rows'])) {
				$data = $result['rows'];
				unset($result['rows']);
				$stats = $result;
			}
			$stats += ['total_rows' => null, 'offset' => null];
			$opts = compact('stats') + [
				'class' => 'set', 'exists' => true, 'defaults' => false
			];

			return $this->item($model, $data, $opts);
		});
	}
295061
protected function convertQuery($query)
    {
        if (is_array($query)) {
            return array_map(function ($value) {
                if ($value instanceof Model) {
                    return $value->pk;
                } elseif ($value instanceof Manager || $value instanceof QuerySet) {
                    return $value->getQueryBuilder();
                }

                return $value;
            }, $query);
        } else {
            return $query;
        }
    }
940666
protected function execute($query)
    {
        $query->execute($this->bindings);
        
        $this->bindings = [];
        $this->wheres   = [];

        return $query;
    }
1067286
public function searchTypeAhead(Model $model, array $query) {
        if ($model->hasMethod('searchTypeAhead')) {
            return $model->searchTypeAhead($query);
        }

        $keyword = $query['term'];
        unset($query['term']);

        $results = $model->find('all', array(
            'conditions' => array($model->alias . '.' . $model->displayField . ' LIKE' => '%' . $keyword . '%') + $query,
            'order' => array($model->alias . '.' . $model->displayField => 'ASC'),
            'contain' => false
        ));
        $data = array();

        foreach ($results as $result) {
            $data[] = array(
                'id' => $result[$model->alias][$model->primaryKey],
                'title' => $result[$model->alias][$model->displayField]
            );
        }

        return $data;
    }
413071
final public function queryArray(string $query)
    {
        $result = $this->queryResult($query);
        if (false === $result) {
            return false;
        }

        $return = [];
        while (false !== $row = $result->fetchRow()) {
            $return[] = $row;
        }
        return $return;
    }
941500
public function scopeExtrasWithValues($query, $model)
    {
        $modelSlug = $model ? getModelSlug($model) : false;
        return $query->with([
            'extras' => function($query) use($model,$modelSlug)
            {
                if ( ! $model ) return $query;

                return $query->with([
                    "{$modelSlug}s" => function($query) use($model,$modelSlug)
                    {
                        return $query->wherePivot("{$modelSlug}_id",$model->id);
                    }
                ]);
            }
        ]);
    }
913892
protected function processSingleResult($query)
    {
        if (!empty($this->fetchRefs)) {
            $res = $query->fetch();
            $model = ($res ? $this->populateModelFromJoin($res) : $res);
        } else {
            $model = $query->fetchClass($this->modelClass);
        }
        if ($model && isset($this->manager)) {
            $this->manager->manageModel($model);
        }
        $this->fetchReferences(false);
        return $model;
    }
22554
public function getTable($model)
    {
        if (false !== $pos = array_search($model, $this->map)) {
            return $pos;
        }

        return null;
    }
1047596
protected function getResults($query)
    {
         $searchFields = ($this->getSearchFields() ?: singleton($this->sourceObject)->stat('searchable_fields'));

        if(!$searchFields) {
            throw new Exception(
                sprintf('HasOneAutocompleteField: No searchable fields could be found for class "%s"',
                $this->sourceObject));
        }

        $params = [];
        $sort = [];
        
        foreach($searchFields as $searchField) {
            $name = (strpos($searchField, ':') !== FALSE) ? $searchField : "$searchField:PartialMatch:nocase";
            $params[$name] = $query;
            $sort[$searchField] = "ASC";
        }

        $results = DataList::create($this->sourceObject)
            ->filterAny($params)
            ->sort($sort)
            ->limit($this->getResultsLimit());

        return $results;
    }
936076
public static function model( $model, $handler = null ) 
	{
		$model_data = call_user_func( $model.'::_model' );
		return Query::select( $model_data['table'], null, $model_data['handler'] )
			->fetch_handler( $model.'::_fetch_handler' );
	}