api-platform / api-platform/core

[Elasticsearch] Filtering on Nested Properties seems to not work

Open
#7,997 4 comments 2 reactions 0 assignees View on GitHub
Elasticsearch
Dominant language
PHP
Stars
2.6k
Forks
980
Avg merge
2d 5h
Merged PRs (30d)
48

Description

**API Platform version(s) affected**: 2.5.3

**Description**
When filtering on a nested property in ES, we must provide the nested path. Function getNestedFieldPath() in FieldDatatypeTrait.php seems to always return null.

With the example given in your documentation, if we try this filter: /tweets?author.gender=male

author is an object so we enter in this condition:
```
if (
Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()
&& null !== ($nextResourceClass = $type->getClassName())
&& $this->resourceClassResolver->isResourceClass($nextResourceClass)
) {
$nestedPath = $this->getNestedFieldPath($nextResourceClass, implode('.', $properties));

return null === $nestedPath ? $nestedPath : "$currentProperty.$nestedPath";
}
```

getNestedFieldPath() is called recursively.

gender is the last element of the string, so when we do `$currentProperty = array_shift($properties);`, $properties is an empty array, so this part returns null:
```
if (!$properties) {
return null;
}
```

$nestedPath is null, so this line `return null === $nestedPath ? $nestedPath : "$currentProperty.$nestedPath";` returns null

This part seems to be never called:
```
if (
null !== ($type = $type->getCollectionValueType())
&& Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()
&& null !== ($className = $type->getClassName())
&& $this->resourceClassResolver->isResourceClass($className)
) {
return $currentProperty;
}
```

Thanks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.