Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include query type in query's toArray() response #61

Merged
merged 1 commit into from Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ v2.0.0 (2016-x)

- [BC break] Aggregation name is not prefixed anymore
- [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)
---
Expand Down
4 changes: 1 addition & 3 deletions src/Aggregation/FilterAggregation.php
Expand Up @@ -68,9 +68,7 @@ public function getArray()
throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added");
}

$filterData = [$this->filter->getType() => $this->filter->toArray()];

return $filterData;
return $this->filter->toArray();
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Aggregation/TopHitsAggregation.php
Expand Up @@ -125,12 +125,15 @@ public function getType()
*/
public function getArray()
{
$output = $this->getSort() ? $this->getSort()->toArray() : [];
$output = array_filter(
[
'sort' => $this->getSort() ? $this->getSort()->toArray() : null,
'size' => $this->getSize(),
'from' => $this->getFrom(),
],
array_merge(
$output,
[
'size' => $this->getSize(),
'from' => $this->getFrom(),
]
),
function ($val) {
return (($val || is_array($val) || ($val || is_numeric($val))));
}
Expand Down
5 changes: 1 addition & 4 deletions src/BuilderInterface.php
Expand Up @@ -19,10 +19,7 @@ interface BuilderInterface
/**
* Generates array which will be passed to elasticsearch-php client.
*
* WARNING: the output of this method will change in version v2.0. It will
* always return array WITH query/aggregation type as key.
*
* @return array|object
* @return array
*/
public function toArray();

Expand Down
23 changes: 9 additions & 14 deletions src/Query/BoolQuery.php
Expand Up @@ -41,18 +41,6 @@ public function __construct()
$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
* @return array
Expand Down Expand Up @@ -103,16 +91,23 @@ public function add(BuilderInterface $query, $type = self::MUST, $key = null)
*/
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 = [];

foreach ($this->container as $boolType => $builders) {
/** @var BuilderInterface $builder */
foreach ($builders as $builder) {
$output[$boolType][] = [$builder->getType() => $builder->toArray()];
$output[$boolType][] = $builder->toArray();
}
}

return $output;
return [$this->getType() => $output];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Query/BoostingQuery.php
Expand Up @@ -59,11 +59,11 @@ public function getType()
public function toArray()
{
$query = [
'positive' => [$this->positive->getType() => $this->positive->toArray()],
'negative' => [$this->negative->getType() => $this->negative->toArray()],
'positive' => $this->positive->toArray(),
'negative' => $this->negative->toArray(),
'negative_boost' => $this->negativeBoost,
];

return $query;
return [$this->getType() => $query];
}
}
2 changes: 1 addition & 1 deletion src/Query/CommonTermsQuery.php
Expand Up @@ -64,6 +64,6 @@ public function toArray()
$this->field => $this->processArray($query),
];

return $output;
return [$this->getType() => $output];
}
}
6 changes: 2 additions & 4 deletions src/Query/ConstantScoreQuery.php
Expand Up @@ -50,13 +50,11 @@ public function getType()
public function toArray()
{
$query = [
'filter' => [
$this->query->getType() => $this->query->toArray(),
],
'filter' => $this->query->toArray(),
];

$output = $this->processArray($query);

return $output;
return [$this->getType() => $output];
}
}
6 changes: 3 additions & 3 deletions src/Query/DisMaxQuery.php
Expand Up @@ -65,10 +65,10 @@ public function toArray()
{
$query = [];
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];
}
}
4 changes: 3 additions & 1 deletion src/Query/ExistsQuery.php
Expand Up @@ -49,7 +49,9 @@ public function getType()
public function toArray()
{
return [
'field' => $this->field,
$this->getType() => [
'field' => $this->field,
],
];
}
}
18 changes: 6 additions & 12 deletions src/Query/FunctionScoreQuery.php
Expand Up @@ -15,17 +15,15 @@
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
{
use ParametersTrait;

/**
* Query of filter.
*
* In Function score could be used query or filter. Use setDslType() to change type.
*
* @var BuilderInterface
*/
private $query;
Expand Down Expand Up @@ -62,9 +60,7 @@ public function getType()
private function applyQuery(array &$function, BuilderInterface $query = null)
{
if ($query) {
$function['query'] = [
$query->getType() => $query->toArray(),
];
$function['query'] = $query->toArray();
}
}

Expand Down Expand Up @@ -222,14 +218,12 @@ public function addSimpleFunction(array $function)
public function toArray()
{
$query = [
'query' => [
$this->query->getType() => $this->query->toArray(),
],
'query' => $this->query->toArray(),
'functions' => $this->functions,
];

$output = $this->processArray($query);

return $output;
return [$this->getType() => $output];
}
}
69 changes: 0 additions & 69 deletions src/Query/FuzzyLikeThisFieldQuery.php

This file was deleted.

70 changes: 0 additions & 70 deletions src/Query/FuzzyLikeThisQuery.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Query/FuzzyQuery.php
Expand Up @@ -64,6 +64,6 @@ public function toArray()
$this->field => $this->processArray($query),
];

return $output;
return [$this->getType() => $output];
}
}
2 changes: 1 addition & 1 deletion src/Query/GeoBoundingBoxQuery.php
Expand Up @@ -80,6 +80,6 @@ public function toArray()

$output = $this->processArray($query);

return $output;
return [$this->getType() => $output];
}
}
2 changes: 1 addition & 1 deletion src/Query/GeoDistanceQuery.php
Expand Up @@ -72,6 +72,6 @@ public function toArray()
];
$output = $this->processArray($query);

return $output;
return [$this->getType() => $output];
}
}
2 changes: 1 addition & 1 deletion src/Query/GeoDistanceRangeQuery.php
Expand Up @@ -69,6 +69,6 @@ public function toArray()
$query = $this->range + [$this->field => $this->location];
$output = $this->processArray($query);

return $output;
return [$this->getType() => $output];
}
}
2 changes: 1 addition & 1 deletion src/Query/GeoPolygonQuery.php
Expand Up @@ -61,6 +61,6 @@ public function toArray()
$query = [$this->field => ['points' => $this->points]];
$output = $this->processArray($query);

return $output;
return [$this->getType() => $output];
}
}
2 changes: 1 addition & 1 deletion src/Query/GeoShapeQuery.php
Expand Up @@ -97,6 +97,6 @@ public function toArray()
{
$output = $this->processArray($this->fields);

return $output;
return [$this->getType() => $output];
}
}