Query provides a set of methods to facilitate the specification of different parameters of the query.
These methods can be chained together.
By calling Query::createCommand, we can get a Command instance which can be further
used to perform/execute the DB query against a database.
For example,
~~~
$query = new Query;
$query->fields('id, name')
->from('myindex', 'users')
->limit(10);
build and execute the query
$command = $query->createCommand();
$rows = $command->search(); // this way you get the raw output of elasticsearch.
~~~
You would normally call $query->search() instead of creating a command as this method
adds the indexBy() feature and also removes some inconsistencies from the response.
Query also provides some methods to easier get some parts of the result only:
- Query::one: returns a single record populated with the first row of data.
- Query::all: returns all records based on the query results.
- Query::count: returns the number of records.
- Query::scalar: returns the value of the first column in the first row of the query result.
- Query::column: returns the value of the first column in the query result.
- Query::exists: returns a value indicating whether the query result has data or not.
NOTE: elasticsearch limits the number of records returned to 10 records by default.
If you expect to get more records you should specify limit explicitly.
public function testFuzzySearch() { $this->prepareDbData(); $queryParts = ["fuzzy_like_this" => ["fields" => ["title"], "like_text" => "Similar to YII", "max_query_terms" => 4]]; $query = new Query(); $query->from('yiitest', 'article'); $query->query = $queryParts; $result = $query->search($this->getConnection()); $this->assertEquals(3, $result['hits']['total']); }
/** * @inheritdoc */ public function createCommand($db = null) { $this->mapAggregations(); $searchQuery = $this->getSearchQuery(); if ($searchQuery) { $this->query = $this->mapQuery($searchQuery); } return parent::createCommand($db); }
/** * @inheritdoc */ public function column($field, $db = null) { if ($field == '_id') { $command = $this->createCommand($db); $command->queryParts['fields'] = []; $command->queryParts['_source'] = false; $result = $command->search(); if (empty($result['hits']['hits'])) { return []; } $column = []; foreach ($result['hits']['hits'] as $row) { $column[] = $row['_id']; } return $column; } return parent::column($field, $db); }
/** * @return mixed */ public function Build() { // Remove all new lines etc. from the json to make it valid $this->query = json_decode($this->query); $query = new Query(); $query->fields(['name'])->from('api-builder', 'api')->highlight([])->limit(5)->query($this->query); // build and execute the query $command = $query->createCommand(); $rows = $command->search(); // this way you get the raw output of elasticsearch. return $rows; }
public function testLimitOffset() { $query = new Query(); $query->limit(10)->offset(5); $this->assertEquals(10, $query->limit); $this->assertEquals(5, $query->offset); }
/** * @inheritdoc */ protected function prepareTotalCount() { if (!$this->query instanceof Query) { throw new InvalidConfigException('The "query" property must be an instance "' . Query::className() . '" or its subclasses.'); } $results = $this->getQueryResults(); return (int) $results['hits']['total']; }