Skip to content
Snippets Groups Projects
Commit 481336a0 authored by Mantas Var's avatar Mantas Var
Browse files

Merge pull request #61 from mvar/update_to_array_structure

Include query type in query's toArray() response
parents 5ee3f67f 4934515e
No related branches found
No related tags found
No related merge requests found
Showing
with 45 additions and 196 deletions
...@@ -6,6 +6,7 @@ v2.0.0 (2016-x) ...@@ -6,6 +6,7 @@ v2.0.0 (2016-x)
- [BC break] Aggregation name is not prefixed anymore - [BC break] Aggregation name is not prefixed anymore
- [BC break] Removed all filters and filtered query - [BC break] Removed all filters and filtered query
- [BC break] Query's `toArray()` now returns array WITH query type
v1.1.1 (2016-01-26) v1.1.1 (2016-01-26)
--- ---
......
...@@ -68,9 +68,7 @@ class FilterAggregation extends AbstractAggregation ...@@ -68,9 +68,7 @@ class FilterAggregation extends AbstractAggregation
throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added"); throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added");
} }
$filterData = [$this->filter->getType() => $this->filter->toArray()]; return $this->filter->toArray();
return $filterData;
} }
/** /**
......
...@@ -125,12 +125,15 @@ class TopHitsAggregation extends AbstractAggregation ...@@ -125,12 +125,15 @@ class TopHitsAggregation extends AbstractAggregation
*/ */
public function getArray() public function getArray()
{ {
$output = $this->getSort() ? $this->getSort()->toArray() : [];
$output = array_filter( $output = array_filter(
[ array_merge(
'sort' => $this->getSort() ? $this->getSort()->toArray() : null, $output,
'size' => $this->getSize(), [
'from' => $this->getFrom(), 'size' => $this->getSize(),
], 'from' => $this->getFrom(),
]
),
function ($val) { function ($val) {
return (($val || is_array($val) || ($val || is_numeric($val)))); return (($val || is_array($val) || ($val || is_numeric($val))));
} }
......
...@@ -19,10 +19,7 @@ interface BuilderInterface ...@@ -19,10 +19,7 @@ interface BuilderInterface
/** /**
* Generates array which will be passed to elasticsearch-php client. * Generates array which will be passed to elasticsearch-php client.
* *
* WARNING: the output of this method will change in version v2.0. It will * @return array
* always return array WITH query/aggregation type as key.
*
* @return array|object
*/ */
public function toArray(); public function toArray();
......
...@@ -41,18 +41,6 @@ class BoolQuery implements BuilderInterface ...@@ -41,18 +41,6 @@ class BoolQuery implements BuilderInterface
$this->container = []; $this->container = [];
} }
/**
* Checks if bool expression is relevant.
*
* @return bool
*
* @deprecated Will be removed in 2.0. No replacement. Always use full structure.
*/
public function isRelevant()
{
return true;
}
/** /**
* @param null $boolType * @param null $boolType
* @return array * @return array
...@@ -103,16 +91,23 @@ class BoolQuery implements BuilderInterface ...@@ -103,16 +91,23 @@ class BoolQuery implements BuilderInterface
*/ */
public function toArray() public function toArray()
{ {
if (count($this->container) === 1 && isset($this->container[self::MUST])
&& count($this->container[self::MUST]) === 1) {
$query = reset($this->container[self::MUST]);
return $query->toArray();
}
$output = []; $output = [];
foreach ($this->container as $boolType => $builders) { foreach ($this->container as $boolType => $builders) {
/** @var BuilderInterface $builder */ /** @var BuilderInterface $builder */
foreach ($builders as $builder) { foreach ($builders as $builder) {
$output[$boolType][] = [$builder->getType() => $builder->toArray()]; $output[$boolType][] = $builder->toArray();
} }
} }
return $output; return [$this->getType() => $output];
} }
/** /**
......
...@@ -59,11 +59,11 @@ class BoostingQuery implements BuilderInterface ...@@ -59,11 +59,11 @@ class BoostingQuery implements BuilderInterface
public function toArray() public function toArray()
{ {
$query = [ $query = [
'positive' => [$this->positive->getType() => $this->positive->toArray()], 'positive' => $this->positive->toArray(),
'negative' => [$this->negative->getType() => $this->negative->toArray()], 'negative' => $this->negative->toArray(),
'negative_boost' => $this->negativeBoost, 'negative_boost' => $this->negativeBoost,
]; ];
return $query; return [$this->getType() => $query];
} }
} }
...@@ -64,6 +64,6 @@ class CommonTermsQuery implements BuilderInterface ...@@ -64,6 +64,6 @@ class CommonTermsQuery implements BuilderInterface
$this->field => $this->processArray($query), $this->field => $this->processArray($query),
]; ];
return $output; return [$this->getType() => $output];
} }
} }
...@@ -50,13 +50,11 @@ class ConstantScoreQuery implements BuilderInterface ...@@ -50,13 +50,11 @@ class ConstantScoreQuery implements BuilderInterface
public function toArray() public function toArray()
{ {
$query = [ $query = [
'filter' => [ 'filter' => $this->query->toArray(),
$this->query->getType() => $this->query->toArray(),
],
]; ];
$output = $this->processArray($query); $output = $this->processArray($query);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -65,10 +65,10 @@ class DisMaxQuery implements BuilderInterface ...@@ -65,10 +65,10 @@ class DisMaxQuery implements BuilderInterface
{ {
$query = []; $query = [];
foreach ($this->queries as $type) { foreach ($this->queries as $type) {
$query['queries'][] = [$type->getType() => $type->toArray()]; $query = array_merge($query, $type->toArray());
} }
$output = $this->processArray($query); $output = $this->processArray(['queries' => $query]);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -49,7 +49,9 @@ class ExistsQuery implements BuilderInterface ...@@ -49,7 +49,9 @@ class ExistsQuery implements BuilderInterface
public function toArray() public function toArray()
{ {
return [ return [
'field' => $this->field, $this->getType() => [
'field' => $this->field,
],
]; ];
} }
} }
...@@ -15,17 +15,15 @@ use ONGR\ElasticsearchDSL\BuilderInterface; ...@@ -15,17 +15,15 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait; use ONGR\ElasticsearchDSL\ParametersTrait;
/** /**
* Elasticsearch function_score query class. * Represents Elasticsearch "function_score" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
*/ */
class FunctionScoreQuery implements BuilderInterface class FunctionScoreQuery implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
/** /**
* Query of filter.
*
* In Function score could be used query or filter. Use setDslType() to change type.
*
* @var BuilderInterface * @var BuilderInterface
*/ */
private $query; private $query;
...@@ -62,9 +60,7 @@ class FunctionScoreQuery implements BuilderInterface ...@@ -62,9 +60,7 @@ class FunctionScoreQuery implements BuilderInterface
private function applyQuery(array &$function, BuilderInterface $query = null) private function applyQuery(array &$function, BuilderInterface $query = null)
{ {
if ($query) { if ($query) {
$function['query'] = [ $function['query'] = $query->toArray();
$query->getType() => $query->toArray(),
];
} }
} }
...@@ -222,14 +218,12 @@ class FunctionScoreQuery implements BuilderInterface ...@@ -222,14 +218,12 @@ class FunctionScoreQuery implements BuilderInterface
public function toArray() public function toArray()
{ {
$query = [ $query = [
'query' => [ 'query' => $this->query->toArray(),
$this->query->getType() => $this->query->toArray(),
],
'functions' => $this->functions, 'functions' => $this->functions,
]; ];
$output = $this->processArray($query); $output = $this->processArray($query);
return $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\Query;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch fuzzy_like_this_field query class.
*/
class FuzzyLikeThisFieldQuery implements BuilderInterface
{
use ParametersTrait;
/**
* @var string
*/
private $field;
/**
* @var string
*/
private $likeText;
/**
* @param string $field
* @param string $likeText
* @param array $parameters
*/
public function __construct($field, $likeText, array $parameters = [])
{
$this->field = $field;
$this->likeText = $likeText;
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'fuzzy_like_this_field';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$query = [
'like_text' => $this->likeText,
];
$output = [
$this->field => $this->processArray($query),
];
return $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\Query;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch fuzzy_like_this query class.
*/
class FuzzyLikeThisQuery implements BuilderInterface
{
use ParametersTrait;
/**
* @var string[]
*/
private $fields;
/**
* @var string
*/
private $likeText;
/**
* @param string|string[] $fields Multiple fields can be set via comma or just an array.
* @param string $likeText
* @param array $parameters
*/
public function __construct($fields, $likeText, array $parameters = [])
{
if (!is_array($fields)) {
$fields = explode(',', $fields);
}
$this->fields = $fields;
$this->likeText = $likeText;
$this->setParameters($parameters);
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'fuzzy_like_this';
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$output = [
'fields' => $this->fields,
'like_text' => $this->likeText,
];
return $this->processArray($output);
}
}
...@@ -64,6 +64,6 @@ class FuzzyQuery implements BuilderInterface ...@@ -64,6 +64,6 @@ class FuzzyQuery implements BuilderInterface
$this->field => $this->processArray($query), $this->field => $this->processArray($query),
]; ];
return $output; return [$this->getType() => $output];
} }
} }
...@@ -80,6 +80,6 @@ class GeoBoundingBoxQuery implements BuilderInterface ...@@ -80,6 +80,6 @@ class GeoBoundingBoxQuery implements BuilderInterface
$output = $this->processArray($query); $output = $this->processArray($query);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -72,6 +72,6 @@ class GeoDistanceQuery implements BuilderInterface ...@@ -72,6 +72,6 @@ class GeoDistanceQuery implements BuilderInterface
]; ];
$output = $this->processArray($query); $output = $this->processArray($query);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -69,6 +69,6 @@ class GeoDistanceRangeQuery implements BuilderInterface ...@@ -69,6 +69,6 @@ class GeoDistanceRangeQuery implements BuilderInterface
$query = $this->range + [$this->field => $this->location]; $query = $this->range + [$this->field => $this->location];
$output = $this->processArray($query); $output = $this->processArray($query);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -61,6 +61,6 @@ class GeoPolygonQuery implements BuilderInterface ...@@ -61,6 +61,6 @@ class GeoPolygonQuery implements BuilderInterface
$query = [$this->field => ['points' => $this->points]]; $query = [$this->field => ['points' => $this->points]];
$output = $this->processArray($query); $output = $this->processArray($query);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -97,6 +97,6 @@ class GeoShapeQuery implements BuilderInterface ...@@ -97,6 +97,6 @@ class GeoShapeQuery implements BuilderInterface
{ {
$output = $this->processArray($this->fields); $output = $this->processArray($this->fields);
return $output; return [$this->getType() => $output];
} }
} }
...@@ -62,6 +62,6 @@ class GeohashCellQuery implements BuilderInterface ...@@ -62,6 +62,6 @@ class GeohashCellQuery implements BuilderInterface
$query = [$this->field => $this->location]; $query = [$this->field => $this->location];
$output = $this->processArray($query); $output = $this->processArray($query);
return $output; return [$this->getType() => $output];
} }
} }
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