Commit a3724aa4 by Florian Fackler

Fixed bug which prevented a field query as described in the example:

``` $result = Article::find()->query(["field" => ["title" => "yii"]])->all(); ````
parent ff7c25ea
...@@ -55,8 +55,10 @@ class QueryBuilder extends \yii\base\Object ...@@ -55,8 +55,10 @@ class QueryBuilder extends \yii\base\Object
$parts['from'] = (int) $query->offset; $parts['from'] = (int) $query->offset;
} }
if (empty($parts['query'])) { if (empty($query->query)) {
$parts['query'] = ["match_all" => (object)[]]; $parts['query'] = ["match_all" => (object)[]];
} else {
$parts['query'] = $query->query;
} }
$whereFilter = $this->buildCondition($query->where); $whereFilter = $this->buildCondition($query->where);
......
<?php
namespace yiiunit\extensions\elasticsearch;
use yii\elasticsearch\Query;
use yii\elasticsearch\QueryBuilder;
/**
* @group elasticsearch
*/
class QueryBuilderTest extends ElasticSearchTestCase
{
public function setUp()
{
parent::setUp();
$command = $this->getConnection()->createCommand();
$command->deleteAllIndexes();
}
private function prepareDbData()
{
$command = $this->getConnection()->createCommand();
$command->insert('test', 'article', ['title' => 'I love yii!'], 1);
$command->insert('test', 'article', ['title' => 'Symfony2 is another framework'], 2);
$command->insert('test', 'article', ['title' => 'Yii2 out now!'], 3);
$command->insert('test', 'article', ['title' => 'yii test'], 4);
$command->flushIndex();
}
public function testQueryBuilderRespectsQuery()
{
$queryParts = ['field' => ['title' => 'yii']];
$queryBuilder = new QueryBuilder($this->getConnection());
$query = new Query();
$query->query = $queryParts;
$build = $queryBuilder->build($query);
$this->assertTrue(array_key_exists('queryParts', $build));
$this->assertTrue(array_key_exists('query', $build['queryParts']));
$this->assertFalse(array_key_exists('match_all', $build['queryParts']), 'Match all should not be set');
$this->assertSame($queryParts, $build['queryParts']['query']);
}
public function testYiiCanBeFoundByQuery()
{
$this->prepareDbData();
$queryParts = ['field' => ['title' => 'yii']];
$query = new Query();
$query->from('test', 'article');
$query->query = $queryParts;
$result = $query->search($this->getConnection());
$this->assertEquals(2, $result['hits']['total']);
}
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('test', 'article');
$query->query = $queryParts;
$result = $query->search($this->getConnection());
$this->assertEquals(3, $result['hits']['total']);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment