@param string|array|\Elastica\Query $query
@param int|array                    $options
@param BuilderInterface             $builder

@return Search
962616
public function createSearch($query = '', $options = null, BuilderInterface $builder = null)
    {
        $search = new Search($this->getClient(), $builder);
        $search->addIndex($this);
        $search->setOptionsAndQuery($options, $query);

        return $search;
    }
879490
public function delete($query, array $options = []) {
		$this->_checkConnection();

		$defaults = [
			'justOne' => false,
			'w' => $this->_config['w'],
			'wTimeoutMS' => $this->_config['wTimeoutMS'],
			'fsync' => false
		];
		$options = array_intersect_key($options + $defaults, $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, ['keys' => ['source', 'conditions']]);
			$source = $args['source'];
			$conditions = $args['conditions'];

			if ($source === "{$prefix}.files") {
				$result = $this->_deleteFile($conditions);
			} else {
				$result = $this->connection->{$args['source']}->remove($conditions, $options);
				$result = $this->_ok($result);
			}
			if ($result && $query->entity()) {
				$query->entity()->sync(null, [], ['dematerialize' => true]);
			}
			return $result;
		});
	}
641466
protected function performSearch(Builder $builder, array $options = [])
    {
        $algolia = $this->algolia->initIndex(
            $builder->index ?: $builder->model->searchableAs()
        );

        if ($builder->callback) {
            return call_user_func(
                $builder->callback,
                $algolia,
                $builder->query,
                $options
            );
        }

        return $algolia->search($builder->query, $options);
    }
1252915
public function where($query, $queryParams = null, string $op = ''): self
    {
        if ($query == '') {
            throw new BuilderException('Query required!');
        }

        // sub-where
        if ($queryParams instanceof Builder) {
            // $opr argument is empty, should be exists in query (eg: id = )
            $query = $this->prepare($query, '', $queryParams);

            return $this->push('where', $query);
        }

        if (is_array($query)) {
            // eg: [['id' => ...], ...]
            foreach ($query as $field => $params) {
                // eg: ['id' => [1, '=', '?', 'or/and']], ['a.id' => [id('b.id'), ...]]
                if (is_array($params)) {
                    @ [$params, $operator, $escapeOperator, $op] = (array) $params;
                    if ($operator == null) {
                        $operator = '='; // @default=equal
                    }
                    $query = '?? '. $operator .' '. ($params instanceof Identifier ? '??' : $escapeOperator ?: '?');
                    $queryParams = [$field, $params];
                } else {
                    // eg: ['id' => 1]
                    $query = '?? = '. ($params instanceof Identifier ? '??' : '?');
                    $queryParams = [$field, $params];
                }

                $op = $op ? strtoupper($op) : self::OP_AND;
                if ($op != self::OP_OR && $op != self::OP_AND) {
                    throw new BuilderException('Available ops: OR, AND');
                }

                $query = $this->agent->prepare($query, $queryParams);

                $this->push('where', [[$query, $op]]);
            }

            return $this;
        }

        if (!is_string($query)) {
            throw new BuilderException(sprintf('String, array or Builder type queries are accepted only, %s given!',
                gettype($query)));
        }

        if ($queryParams !== null) {
            if (!is_array($queryParams) && !is_scalar($queryParams) && !($queryParams instanceof Identifier)) {
                throw new BuilderException(sprintf('Array, scalar and Query\Identifier params are accepted only, %s given!',
                    gettype($queryParams)));
            }
        }

        $query = $this->agent->prepare($query, $queryParams);

        $op = $op ? strtoupper($op) : self::OP_AND;
        if ($op != self::OP_OR && $op != self::OP_AND) {
            throw new BuilderException('Available ops: OR, AND');
        }

        return $this->push('where', [[$query, $op]]);
    }
937135
protected function filter($query, EntryQueryOptions $options)
    {
        if ($options->familyHash || $options->tag || $options->batchId) {
            return $this;
        }

        $query->where('should_display_on_index', true);

        return $this;
    }
885018
protected function getRequestParameters(Builder $query, array $availableFields)
    {
        $givenFields = $this->getRequestParametersRaw();

        foreach ($givenFields as $fieldName => $value) {

            $from = substr($fieldName, 0, -5);
            $to = substr($fieldName, 0, -3);

            if (in_array($from, $availableFields) && $value != '')
                $query->where($from, '>=', $value);

            if (in_array($to, $availableFields) && $value != '')
                $query->where($to, '<=', $value);

            if (in_array($fieldName, $availableFields))
                if (is_array($value))
                    $query->whereIn($fieldName, $value);
                else
                    $query->where($fieldName, '=', $value);
        }

        return $query;
    }
1135335
public function search($query = '', $options = null): ResultSet
    {
        return $this->getType()->search($query, $options = null);
    }
862916
public function search(QueryBuilderInterface $Builder) {
        $this->serialyse($Builder);
        $statement = $this->connector->query($this->parsedQuery);
        $result    = $this->statementToArray($statement);
        $resultSetClass = $this->resultSetClassName;
        $resultSet = new $resultSetClass($result);
        $queryCount = $this->getSerialyser()->setCriteriaList($Builder)->count(true)->serialyse();
        $resultSet->setParent($this)->setCountQuery($queryCount);
        return $resultSet;
    }
675346
public function queryAssoc($query, $options = array())
    {
        $statement = $this->_runQuery($query, $options);
        return $statement->fetch(\PDO::FETCH_ASSOC);
    }
445259
public function alterBaseQueryBuilder($queryBuilder, $options = array(), $action = 'list')
    {
        if ($this->getHasContextProperty()) {

            $queryBuilder = $this->getResourceContextManager()->addContextFilterToQueryBuilder(
                $queryBuilder,
                $this->getContextPropertyRelationName(),
                $this->getContextPropertyValueNamePrefix()
            );

        }
        return $queryBuilder;
    }
201589
public function query($sql, $key = null, $options = []) {
		return $this->object->query($sql, $key, $options);
	}
1066358
protected function searchQuery(Builder $query, string $phrase): Builder
    {
        $fields = $this->getModel()::getFillableFields();

        return $query->where(function (Builder $query) use ($fields, $phrase) {
            foreach ($fields as $key => $field) {
                $method = $key == 0 ? 'where' : 'orWhere';

                if (strpos($field, '_at') === false) {
                    $query->{$method}($field, 'LIKE', '%' . $phrase . '%');
                }
            }

            return $query;
        });
    }
484091
public function execute(QueryBuilderCache $queryBuilderCache, array $options = [])
    {
        // Reconnect the connection in case isn't connected yet.
        $this->reconnect();

        $queryStatement = new QueryStatement($queryBuilderCache);

        if ($queryStatement->getCollection()) {
            $startTime = microtime(true);
            $result = $this->platformExecuteHandler($queryStatement, $options);

            $queryStatement->setDuration($startTime);
            $queryStatement->setAffectedDocuments($this->getAffectedDocuments());
            $queryStatement->setLastInsertId($this->getLastInsertId());

            $this->queriesCache[] = $queryStatement;

            if ($queryStatement->hasErrors()) {
                if ($this->debugEnabled) {
                    throw new RuntimeException($queryStatement->getLatestErrorMessage(),
                        $queryStatement->getLatestErrorCode());
                }
            }

            return (bool)$result;
        }

        return false;
    }
1324696
public function applySearch(Builder $query, \Illuminate\Http\Request $request)
    {
        $search = $request->input('search.value');
        if (empty($search)) {
            return;
        }

        $query->where(function (Builder $query) use ($search) {
            $columns = $this->getColumns()->all();

            foreach ($columns as $column) {
                if ($column->isSearchable()) {
                    if ($column instanceof ColumnInterface) {
                        if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) {
                            if (method_exists($metaInstance, 'onSearch')) {
                                $metaInstance->onSearch($column, $query, $search);
                                continue;
                            }
                        }

                        if (is_callable($callback = $column->getSearchCallback())) {
                            $callback($column, $query, $search);
                            continue;
                        }
                    }

                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
                }
            }
        });
    }
110461
public function query( SqlInterface $query, $options = 0 )
    {

        $options += $options & ( self::RECONNECT_AS_REQUIRED | self::RECONNECT_NO ) ? 0 : self::RECONNECT_DEFAULT;

        $this->numQuerys++;

        $timings = array();
        $timings['start'] = microtime( true );

        $sql = $query->parse( $this );
        $timings['query_parsed'] = microtime( true );

        $this->debug->emit( Debug::INFO, self::QUERY_PARSED, $sql );

        // debugging search path pest
        // $search_path = pg_query( $this->resource->get(), "SHOW search_path"); $result = pg_fetch_row( $search_path ); d_pr( $result );

        // set exception
        $resource = $this->resource->get( $options & self::RECONNECT_AS_REQUIRED );

        try {

            // really ghetto debuggin for when shit really hits the fan
            // file_put_contents( '~/sqllog.out', "{$sql}\n\n", FILE_APPEND );

            // execute query
            if( false === $result = @pg_query( $resource, $sql ) ) {
                throw new \Exception();
            }

        } catch( \Exception $e ) {

            $exceptionFactory = new QueryExceptionFactory( $resource, $sql, $this->debug );
            throw $exceptionFactory->get();

        }

        $timings['query_executed'] = microtime( true );
        $timings['total'] = $timings['query_executed'] - $timings['start'];
        $timings['parsing'] = $timings['query_parsed'] - $timings['start'];
        $timings['total'] = $timings['query_executed'] - $timings['query_parsed'];

        $this->debug->emit( self::QUERY_OK, $sql, $timings );

        $resultResourceObject = new Result( $result, $this, $query, $timings );

        return $resultResourceObject;

    }
1096030
private static function applyOptions(QueryBuilder $query, array $options)
    {
        if (isset($options['orderby'])) {
            $option = $options['orderby'];
            foreach ($option as $column => $order) {
                $query = $query->orderBy($column, $order);
            }
        }

        if (isset($options['for_update']) && $options['for_update'] === true) {
            $query->forUpdate();
        } elseif (isset($options['shared_lock']) && $options['shared_lock'] === true) {
            $query->sharedLock();
        }

        $limit  = $options['limit'] ?? null;
        $offset = $options['offset'] ?? 0;

        if ($limit !== null) {
            $query = $query->limit($limit, $offset);
        }

        return $query;
    }
627062
protected function filterByRequestParameters(Builder $query, Request $request, array $availableFields): Builder
    {
        $givenFields = $this->getRequestParameters($request);

        foreach ($givenFields as $fieldName => $value) {
            $from = substr($fieldName, 0, -5);
            $to = substr($fieldName, 0, -3);

            if (in_array($from, $availableFields) && $value != '') {
                $query->where($from, '>=', $value);
            }

            if (in_array($to, $availableFields) && $value != '') {
                $query->where($to, '<=', $value);
            }

            if (in_array($fieldName, $availableFields)) {
                if (is_array($value)) {
                    $query->whereIn($fieldName, $value);
                } else {
                    if ($value === 'NOT_NULL') {
                        $query->whereNotNull($fieldName);
                    } elseif ($value === 'NULL') {
                        $query->whereNull($fieldName);
                    } else {
                        $query->where($fieldName, '=', $value);
                    }
                }
            }
        }

        return $query;
    }