diff --git a/docs/Query/Template.md b/docs/Query/Template.md new file mode 100644 index 0000000000000000000000000000000000000000..949ef31454d51ff1e8ef93422f8700ef064c0111 --- /dev/null +++ b/docs/Query/Template.md @@ -0,0 +1,69 @@ +# Template query + +> More info about Boosting query is in the [official elasticsearch docs][1] + +A query that accepts a query template and a map of key/value pairs to fill in template parameters. + +```JSON +{ + "query": { + "template": { + "inline": { "match": { "text": "{{query_string}}" }}, + "params" : { + "query_string" : "all about search" + } + } + } +} +``` + +And now the query via DSL: + +```php +$template = '"match": { "text": "{{query_string}}"'; +$params = ['query_string' => 'all about search']; + +$templateQuery = new TemplateQuery(); +$templateQuery->setInline($template); +$templateQuery->setParams($params); + +$search = new Search(); +$search->addQuery($templateQuery); + +$queryArray = $search->toArray(); +``` + +The template of the query can also be stored in a different file, that way, the file path must +be provided in stead of `inline` parameter: + +```yaml + +{ + "query": { + "template": { + "file": "my_template", + "params" : { + "query_string" : "all about search" + } + } + } +} + +``` + +And now the query via DSL: + +```php +$params = ['query_string' => 'all about search']; + +$templateQuery = new TemplateQuery(); +$templateQuery->setFile('my_template'); +$templateQuery->setParams($params); + +$search = new Search(); +$search->addQuery($templateQuery); + +$queryArray = $search->toArray(); +``` + +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html diff --git a/src/Query/TemplateQuery.php b/src/Query/TemplateQuery.php new file mode 100644 index 0000000000000000000000000000000000000000..4719ac51255b2684bf2f339d73205cd84db8b64d --- /dev/null +++ b/src/Query/TemplateQuery.php @@ -0,0 +1,132 @@ +<?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\Query; + +use ONGR\ElasticsearchDSL\BuilderInterface; +use ONGR\ElasticsearchDSL\ParametersTrait; + +/** + * Represents Elasticsearch "template" query. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html + */ +class TemplateQuery implements BuilderInterface +{ + use ParametersTrait; + + /** + * @var string + */ + private $file; + + /** + * @var string + */ + private $inline; + + /** + * @var array + */ + private $params; + + /** + * @param string $file A template of the query + * @param string $inline A template of the query + * @param array $params Parameters to insert into template + */ + public function __construct($file = null, $inline = null, array $params = []) + { + $this->setFile($file); + $this->setInline($inline); + $this->setParams($params); + } + + /** + * @return string + */ + public function getFile() + { + return $this->file; + } + + /** + * @param string $file + */ + public function setFile($file) + { + $this->file = $file; + } + + /** + * @return string + */ + public function getInline() + { + return $this->inline; + } + + /** + * @param string $inline + */ + public function setInline($inline) + { + $this->inline = $inline; + } + + /** + * @return array + */ + public function getParams() + { + return $this->params; + } + + /** + * @param array $params + */ + public function setParams($params) + { + $this->params = $params; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'template'; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + $output = array_filter( + [ + 'file' => $this->getFile(), + 'inline' => $this->getInline(), + 'params' => $this->getParams(), + ] + ); + + if (!isset($output['file']) && !isset($output['inline'])) { + throw new \InvalidArgumentException( + 'Template query requires that either `inline` or `file` parameters are set' + ); + } + + $output = $this->processArray($output); + + return [$this->getType() => $output]; + } +} diff --git a/tests/Query/TemplateQueryTest.php b/tests/Query/TemplateQueryTest.php new file mode 100644 index 0000000000000000000000000000000000000000..19265621bb20435820cbf7c57d525dcc24429a64 --- /dev/null +++ b/tests/Query/TemplateQueryTest.php @@ -0,0 +1,67 @@ +<?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\Query; + +use ONGR\ElasticsearchDSL\Query\TemplateQuery; + +/** + * Unit test for Template. + */ +class TemplateQueryTest extends \PHPUnit_Framework_TestCase +{ + /** + * Tests toArray() method with inline. + */ + public function testToArrayInline() + { + $inline = '"term": {"field": "{{query_string}}"}'; + $params = ['query_string' => 'all about search']; + $query = new TemplateQuery(null, $inline, $params); + $expected = [ + 'template' => [ + 'inline' => $inline, + 'params' => $params + ], + ]; + $this->assertEquals($expected, $query->toArray()); + } + + /** + * Tests toArray() method with file + */ + public function testToArrayFile() + { + $file = 'my_template'; + $params = ['query_string' => 'all about search']; + $query = new TemplateQuery(); + $query->setFile($file); + $query->setParams($params); + $expected = [ + 'template' => [ + 'file' => $file, + 'params' => $params, + ], + ]; + $this->assertEquals($expected, $query->toArray()); + } + + /** + * Tests toArray() exception + * + * @expectedException \InvalidArgumentException + */ + public function testToArrayException() + { + $query = new TemplateQuery(); + $query->toArray(); + } +}