ElasticsearchDSL icon indicating copy to clipboard operation
ElasticsearchDSL copied to clipboard

Add support for Parent Id Query

Open saimaz opened this issue 8 years ago • 2 comments

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-parent-id-query.html

saimaz avatar Jan 27 '17 14:01 saimaz

<?php

namespace ONGR\ElasticsearchDSL\Query\Joining;

use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;

/**
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-parent-id-query.html
 */
class ParentIdQuery implements BuilderInterface
{
    use ParametersTrait;

    /**
     * @var string
     */
    private $childType;

    /**
     * @var string
     */
    private $parentId;

    /**
     * @param string $parentId
     * @param string $childType
     * @param array  $parameters
     */
    public function __construct($parentId, string $childType, array $parameters = [])
    {
        $this->childType = $childType;
        $this->parentId = $parentId;
        $this->setParameters($parameters);
    }

    /**
     * {@inheritdoc}
     */
    public function getType()
    {
        return 'parent_id';
    }

    /**
     * {@inheritdoc}
     */
    public function toArray()
    {
        $query = [
            'id' => $this->parentId,
            'type' => $this->childType,
        ];
        $output = $this->processArray($query);

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

Here is some working code for parent_id quey, sry that i didn't made a PR, feels bad. (It will also work if you put this class into your own bundle and use it there, just change the namespace to your own)

oliver-schulz avatar Apr 03 '18 10:04 oliver-schulz

Thanks for the input @oliver-schulz

saimaz avatar Apr 03 '18 11:04 saimaz