mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-20 16:38:22 +08:00
Fixed bug which prevented a field query as described in the example:
``` $result = Article::find()->query(["field" => ["title" => "yii"]])->all(); ````
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
73
tests/unit/extensions/elasticsearch/QueryBuilderTest.php
Normal file
73
tests/unit/extensions/elasticsearch/QueryBuilderTest.php
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
<?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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user