Skip to content

Commit e9e3430

Browse files
committed
Merge pull request #71 from beowulfenator/master
options in query
2 parents f4c5e4e + cb05df5 commit e9e3430

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Yii Framework 2 elasticsearch extension Change Log
1515
- Enh #33: Implemented `Command::updateSettings()` and `Command::updateAnalyzers()` (githubjeka)
1616
- Enh #50: Implemented HTTP auth (silverfire)
1717
- Enh #62: Added support for scroll API in `batch()` and `each()` (beowulfenator, 13leaf)
18+
- Enh #70: `Query` and `ActiveQuery` now have `$options` attribute that is passed to commands generated by queries (beowulfenator)
1819
- Enh: Unified model creation from result set in `Query` and `ActiveQuery` with `populate()` (beowulfenator)
1920

2021

Command.php

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class Command extends Component
3939
* @var array list of arrays or json strings that become parts of a query
4040
*/
4141
public $queryParts;
42+
/**
43+
* @var array options to be appended to the query URL, such as "search_type" for search or "timeout" for delete
44+
*/
4245
public $options = [];
4346

4447

Query.php

+44
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Yii;
1111
use yii\base\Component;
12+
use yii\base\InvalidParamException;
1213
use yii\db\QueryInterface;
1314
use yii\db\QueryTrait;
1415

@@ -157,6 +158,12 @@ class Query extends Component implements QueryInterface
157158
* @see http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-min-score.html
158159
*/
159160
public $minScore;
161+
/**
162+
* @var array list of options that will passed to commands created by this query.
163+
* @see Command::$options
164+
* @since 2.0.4
165+
*/
166+
public $options = [];
160167

161168
/**
162169
* @inheritdoc
@@ -607,4 +614,41 @@ public function minScore($minScore)
607614
$this->minScore = $minScore;
608615
return $this;
609616
}
617+
618+
/**
619+
* Sets the options to be passed to the command created by this query.
620+
* @param array $options the options to be set.
621+
* @return $this the query object itself
622+
* @throws InvalidParamException if $options is not an array
623+
* @see Command::$options
624+
* @since 2.0.4
625+
*/
626+
public function options($options)
627+
{
628+
if (!is_array($options)) {
629+
throw new InvalidParamException('Array parameter expected, ' . gettype($options) . ' received.');
630+
}
631+
632+
$this->options = $options;
633+
return $this;
634+
}
635+
636+
/**
637+
* Adds more options, overwriting existing options.
638+
* @param array $options the options to be added.
639+
* @return $this the query object itself
640+
* @throws InvalidParamException if $options is not an array
641+
* @see options()
642+
* @since 2.0.4
643+
*/
644+
public function addOptions($options)
645+
{
646+
if (!is_array($options)) {
647+
throw new InvalidParamException('Array parameter expected, ' . gettype($options) . ' received.');
648+
}
649+
650+
$this->options = array_merge($this->options, $options);
651+
return $this;
652+
}
653+
610654
}

QueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function build($query)
119119
$parts['sort'] = $sort;
120120
}
121121

122-
$options = [];
122+
$options = $query->options;
123123
if ($query->timeout !== null) {
124124
$options['timeout'] = $query->timeout;
125125
}

tests/QueryBuilderTest.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public function setUp()
3434
private function prepareDbData()
3535
{
3636
$command = $this->getConnection()->createCommand();
37-
$command->insert('yiitest', 'article', ['title' => 'I love yii!'], 1);
38-
$command->insert('yiitest', 'article', ['title' => 'Symfony2 is another framework'], 2);
39-
$command->insert('yiitest', 'article', ['title' => 'Yii2 out now!'], 3);
40-
$command->insert('yiitest', 'article', ['title' => 'yii test'], 4);
37+
$command->insert('yiitest', 'article', ['title' => 'I love yii!', 'weight' => 1], 1);
38+
$command->insert('yiitest', 'article', ['title' => 'Symfony2 is another framework', 'weight' => 2], 2);
39+
$command->insert('yiitest', 'article', ['title' => 'Yii2 out now!', 'weight' => 3], 3);
40+
$command->insert('yiitest', 'article', ['title' => 'yii test', 'weight' => 4], 4);
4141

4242
$command->flushIndex('yiitest');
4343
}
@@ -67,20 +67,34 @@ public function testYiiCanBeFoundByQuery()
6767

6868
public function testMinScore()
6969
{
70-
if (version_compare($this->version, '1.6', '<')) {
71-
$this->markTestSkipped('Score calculation in ES < 1.6 is untestable');
72-
}
73-
$queryParts = ['term' => ['title' => 'yii']];
70+
$queryParts = [
71+
'function_score' => [
72+
'boost_mode' => 'replace',
73+
'query' => ['term' => ['title' => 'yii']],
74+
'functions' => [
75+
['script_score' => [
76+
'script' => "doc['weight'].getValue()",
77+
]],
78+
],
79+
],
80+
];
81+
//without min_score should get 2 documents with weights 1 and 4
82+
7483
$query = new Query();
7584
$query->from('yiitest', 'article');
76-
$query->query = $queryParts;
77-
$query->minScore(0.9);
85+
$query->query($queryParts);
86+
87+
$query->minScore(0.5);
7888
$result = $query->search($this->getConnection());
79-
$this->assertEquals(0, $result['hits']['total']);
89+
$this->assertEquals(2, $result['hits']['total']);
8090

81-
$query->minScore(0.6);
91+
$query->minScore(2);
8292
$result = $query->search($this->getConnection());
8393
$this->assertEquals(1, $result['hits']['total']);
94+
95+
$query->minScore(5);
96+
$result = $query->search($this->getConnection());
97+
$this->assertEquals(0, $result['hits']['total']);
8498
}
8599

86100
public function testMltSearch()

tests/QueryTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ public function testBatch()
302302

303303
//test each
304304
$query = new Query;
305-
$query->from('yiitest', 'user')->limit(3)->orderBy(['name' => SORT_ASC])->indexBy('name');
305+
$query->from('yiitest', 'user')->limit(3)->orderBy(['name' => SORT_ASC])->indexBy('name')->options(['preference' => '_local']);
306+
//NOTE: preference -> _local has no influence on query result, everything's fine as long as query doesn't fail
306307

307308
$result_keys = [];
308309
$result_values = [];
@@ -319,7 +320,8 @@ public function testBatch()
319320

320321
//test batch
321322
$query = new Query;
322-
$query->from('yiitest', 'user')->limit(3)->orderBy(['name' => SORT_ASC])->indexBy('name');
323+
$query->from('yiitest', 'user')->limit(3)->orderBy(['name' => SORT_ASC])->indexBy('name')->options(['preference' => '_local']);
324+
//NOTE: preference -> _local has no influence on query result, everything's fine as long as query doesn't fail
323325

324326
$results = [];
325327
foreach ($query->batch('1m', $this->getConnection()) as $batchId => $batch) {

0 commit comments

Comments
 (0)