diff --git a/docs/Query/ConstantScore.md b/docs/Query/ConstantScore.md new file mode 100644 index 0000000000000000000000000000000000000000..dda1c0dad55fbb071c4c262c076f71c28a15c660 --- /dev/null +++ b/docs/Query/ConstantScore.md @@ -0,0 +1,58 @@ +# 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 diff --git a/src/FilterOrQueryDetectionTrait.php b/src/FilterOrQueryDetectionTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..ae6e394541d5d61747087ca423e65581e5dddd13 --- /dev/null +++ b/src/FilterOrQueryDetectionTrait.php @@ -0,0 +1,43 @@ +<?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(); + } +} diff --git a/src/Query/ConstantScoreQuery.php b/src/Query/ConstantScoreQuery.php index 6c3b6bbbf91750692552d9c4efd001412c741d89..d4662d25099fd32be477c22be3637d3be7935637 100644 --- a/src/Query/ConstantScoreQuery.php +++ b/src/Query/ConstantScoreQuery.php @@ -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(), ], ]; diff --git a/tests/FilterOrQueryDetectionTraitTest.php b/tests/FilterOrQueryDetectionTraitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9493dcf02176cf388134b2944d635338e0a27500 --- /dev/null +++ b/tests/FilterOrQueryDetectionTraitTest.php @@ -0,0 +1,69 @@ +<?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); + } +}