Skip to content
Snippets Groups Projects
Commit 4db593f3 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Introduced filter or query detect trait, refactored constant score query

parent 40e1af57
No related branches found
No related tags found
No related merge requests found
# Constant score query
> More info about Constant score query is in the [official elasticsearch docs][1]
Inside constant score query you can insert filter or query.
Lets take an example to write a constant score query with filter inside.
```JSON
{
"constant_score" : {
"filter" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2
}
}
```
And now the query via DSL:
```php
$termFilter = new TermFilter("user", "kimchy");
$constantScoreQuery = new ConstantScoreQuery($termFilter, ["boost" => 1.2]);
$search = new Search();
$search->addQuery($constantScoreQuery);
$queryArray = $search->toArray();
```
To form a query with query inside is very easy, just add a query in `ConstantScoreQuery` constructor instead of filter.
```JSON
{
"constant_score" : {
"query" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2
}
}
```
via DSL:
```php
$termQuery = new TermQuery("user", "kimchy");
$constantScoreQuery = new ConstantScoreQuery($termQuery, ["boost" => 1.2]);
$search = new Search();
$search->addQuery($constantScoreQuery);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL;
/**
* A trait which can detect query or filter is passed.
*/
trait FilterOrQueryDetectionTrait
{
/**
* Detects a dsl type.
*
* @param BuilderInterface $object
* @return string
*
* @throws \InvalidArgumentException
*/
public function detectDslType(BuilderInterface $object)
{
$namespace = get_class($object);
$dslTypes = ['Filter', 'Query'];
foreach ($dslTypes as $type) {
$length = strlen($type);
$dslType = substr($namespace, -$length);
if ($dslType === $type) {
return strtolower($dslType);
}
}
throw new \InvalidArgumentException();
}
}
......@@ -12,7 +12,7 @@
namespace ONGR\ElasticsearchDSL\Query;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\DslTypeAwareTrait;
use ONGR\ElasticsearchDSL\FilterOrQueryDetectionTrait;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
......@@ -21,7 +21,7 @@ use ONGR\ElasticsearchDSL\ParametersTrait;
class ConstantScoreQuery implements BuilderInterface
{
use ParametersTrait;
use DslTypeAwareTrait;
use FilterOrQueryDetectionTrait;
/**
* @var BuilderInterface
......@@ -36,7 +36,6 @@ class ConstantScoreQuery implements BuilderInterface
{
$this->query = $query;
$this->setParameters($parameters);
$this->setDslType('query');
}
/**
......@@ -53,7 +52,7 @@ class ConstantScoreQuery implements BuilderInterface
public function toArray()
{
$query = [
strtolower($this->getDslType()) => [
$this->detectDslType($this->query) => [
$this->query->getType() => $this->query->toArray(),
],
];
......
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL;
use ONGR\ElasticsearchDSL\Aggregation\GlobalAggregation;
use ONGR\ElasticsearchDSL\Filter\MatchAllFilter;
use ONGR\ElasticsearchDSL\FilterOrQueryDetectionTrait;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
/**
* Test for FilterOrQueryDetectionTrait.
*/
class FilterOrQueryDetectionTraitTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FilterOrQueryDetectionTrait
*/
private $mock;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->mock = $this->getMockForTrait('ONGR\ElasticsearchDSL\FilterOrQueryDetectionTrait');
}
/**
* Tests if setDslType throws exception.
*
* @expectedException \InvalidArgumentException
*/
public function testIfTraitDetectsNotKnowType()
{
$aggregation = new GlobalAggregation('global');
$this->mock->detectDslType($aggregation);
}
/**
* Tests if detectDslType detects passed query.
*/
public function testIfTraitDetectsQuery()
{
$query = new MatchAllQuery();
$result = $this->mock->detectDslType($query);
$this->assertEquals('query', $result);
}
/**
* Tests if detectDslType detects passed filter.
*/
public function testIfTraitDetectsFilter()
{
$filter = new MatchAllFilter();
$result = $this->mock->detectDslType($filter);
$this->assertEquals('filter', $result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment