Skip to content
Snippets Groups Projects
Request.php 3.17 KiB
Newer Older
Pavel's avatar
Pavel committed
<?php

namespace IQDEV\ElasticSearch\Search;

use IQDEV\ElasticSearch\Esable;
Nikita Chernykh's avatar
Nikita Chernykh committed
use IQDEV\ElasticSearch\Criteria\Match\QueryMatchCollection;
use IQDEV\ElasticSearch\Criteria\Order\OrderCollection;
Pavel's avatar
Pavel committed
use IQDEV\ElasticSearch\Search\Aggs\AggsCollection;
use IQDEV\ElasticSearch\Search\BoolQuery\Query;

final class Request implements Esable
{
    private ?Query $query = null;
    private ?Query $postFilter = null;
    private ?AggsCollection $aggs = null;
    private ?Pagination $pagination = null;
Pavel's avatar
Pavel committed
    private ?OrderCollection $sort = null;
    private ?QueryMatchCollection $matchCollection = null;
Pavel's avatar
Pavel committed
    private ?array $source = null;
Pavel's avatar
Pavel committed

    public function setPagination(?Pagination $pagination): self
    {
        $this->pagination = $pagination;

        return $this;
    }

    public function getQuery(): Query
    {
Pavel's avatar
Pavel committed
        if (null === $this->query) {
Pavel's avatar
Pavel committed
            $this->query = new Query();
        }

        return $this->query;
    }

    public function setQuery(Query $query): self
    {
        $this->query = $query;

        return $this;
    }

Pavel's avatar
Pavel committed
    public function getPostFilter(): Query
    {
Pavel's avatar
Pavel committed
        if (null === $this->postFilter) {
Pavel's avatar
Pavel committed
            $this->postFilter = new Query();
        }

        return $this->postFilter;
    }

    public function setPostFilter(Query $query): self
    {
        $this->postFilter = $query;

        return $this;
    }

Pavel's avatar
Pavel committed
    public function getAggs(): AggsCollection
    {
Pavel's avatar
Pavel committed
        if (null === $this->aggs) {
Pavel's avatar
Pavel committed
            $this->aggs = new AggsCollection();
        }

        return $this->aggs;
    }

    public function setAggs(AggsCollection $aggs): self
Pavel's avatar
Pavel committed
    {
        $this->aggs = $aggs;

        return $this;
Pavel's avatar
Pavel committed
    }

    public function getQueryMatch(): QueryMatchCollection
Pavel's avatar
Pavel committed
    {
        if (null === $this->matchCollection) {
            $this->matchCollection = new QueryMatchCollection();
        }
        return $this->matchCollection;
    }

    public function getPagination(): ?Pagination
    {
        return $this->pagination;
Pavel's avatar
Pavel committed
    }

Pavel's avatar
Pavel committed
    public function setSource(array $s): self
Pavel's avatar
Pavel committed
    {
Pavel's avatar
Pavel committed
        $this->source = $s;

Pavel's avatar
Pavel committed
        return $this;
    }

Pavel's avatar
Pavel committed
    public function getSort(): OrderCollection
    {
        if (null === $this->sort) {
            $this->sort = new OrderCollection();
        }

        return $this->sort;
    }

Pavel's avatar
Pavel committed
    public function es(): array
    {
Pavel's avatar
Pavel committed
        $request = [];

        if ($this->source) {
            $request['_source'] = $this->source;
        }
Pavel's avatar
Pavel committed

Pavel's avatar
Pavel committed
        if ($this->postFilter && false === $this->postFilter->isEmpty()) {
Pavel's avatar
Pavel committed
            $request['post_filter'] = $this->postFilter->es()['query'];
        }

Pavel's avatar
Pavel committed
        if ($this->query && false === $this->query->isEmpty()) {
Pavel's avatar
Pavel committed
            $request['query'] = $this->query->es()['query'];
        }

        if ($this->matchCollection && false === $this->matchCollection->isEmpty()) {
            $request['query']['match'] = $this->matchCollection->es();
Pavel's avatar
Pavel committed
        }

        if ($this->aggs) {
            $request['aggs'] = $this->aggs->es()['aggs'];
        }

        if ($this->pagination) {
            $request = array_merge($request, $this->pagination->es());
        }
Pavel's avatar
Pavel committed

        if ($this->sort) {
            $request['sort'] = $this->sort->es();
Pavel's avatar
Pavel committed
        }

        return $request;
    }
Pavel's avatar
Pavel committed
}