Skip to content
Snippets Groups Projects
Commit c03205d3 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas Committed by GitHub
Browse files

Merge pull request #103 from einorler/query_template

added template query
parents 2e03bf47 17e73b3a
No related branches found
No related tags found
No related merge requests found
# 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
<?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];
}
}
<?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();
}
}
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