Commit bad9c0cc by Qiang Xue

Merge branch 'master' of github.com:mintao/yii2 into mintao-master

Conflicts: extensions/yii/elasticsearch/CHANGELOG.md
parents ad913dd8 ac2434a0
...@@ -5,6 +5,7 @@ Yii Framework 2 elasticsearch extension Change Log ...@@ -5,6 +5,7 @@ Yii Framework 2 elasticsearch extension Change Log
---------------------------- ----------------------------
- Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder) - Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder)
- Bug #2324: Fixed QueryBuilder bug when building a query with "query" option (mintao)
- Enh #1313: made index and type available in `ActiveRecord::instantiate()` to allow creating records based on elasticsearch type when doing cross index/type search (cebe) - Enh #1313: made index and type available in `ActiveRecord::instantiate()` to allow creating records based on elasticsearch type when doing cross index/type search (cebe)
- Enh #1382: Added a debug toolbar panel for elasticsearch (cebe) - Enh #1382: Added a debug toolbar panel for elasticsearch (cebe)
- Enh #1765: Added support for primary key path mapping, pk can now be part of the attributes when mapping is defined (cebe) - Enh #1765: Added support for primary key path mapping, pk can now be part of the attributes when mapping is defined (cebe)
......
...@@ -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);
......
...@@ -44,6 +44,7 @@ Yii Framework 2 Change Log ...@@ -44,6 +44,7 @@ Yii Framework 2 Change Log
- Bug #2212: `yii\gridview\DataColumn` generates incorrect labels when used with nosql DB and there is no data (qiangxue) - Bug #2212: `yii\gridview\DataColumn` generates incorrect labels when used with nosql DB and there is no data (qiangxue)
- Bug #2298: Fixed the bug that Gii controller generator did not allow digit in the controller ID (qiangxue) - Bug #2298: Fixed the bug that Gii controller generator did not allow digit in the controller ID (qiangxue)
- Bug #2303: Fixed the bug that `yii\base\Theme::pathMap` did not support dynamic update with path aliases (qiangxue) - Bug #2303: Fixed the bug that `yii\base\Theme::pathMap` did not support dynamic update with path aliases (qiangxue)
- Bug #2324: Fixed QueryBuilder bug when building a query with "query" option (mintao)
- Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark) - Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark)
- Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark) - Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark)
- Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe) - Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe)
......
<?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