/**
  * Tests building the elasticsearch query body.
  */
 public function testBuildBody()
 {
     $this->specify('match all query body page 1', function () {
         $this->search->setPage(1);
         $this->search->setSize(100);
         verify($this->search->buildBody())->equals(['query' => ['match_all' => []], 'size' => 100, 'from' => 0]);
     });
     $this->specify('match all query body page 2', function () {
         $this->search->setPage(2);
         $this->search->setSize(100);
         verify($this->search->buildBody())->equals(['query' => ['match_all' => []], 'size' => 100, 'from' => 100]);
     });
     $this->specify('bool query body page 1', function () {
         $this->search->setPage(1);
         $this->search->setSize(100);
         $this->search->setQuery($this->query);
         verify($this->search->buildBody())->equals(['query' => ['bool' => ['must' => [['term' => ['field1' => 'value1']]]]], 'size' => 100, 'from' => 0]);
     });
     $this->specify('match all query with sort body', function () {
         $this->search->setSort($this->sort);
         verify($this->search->buildBody())->equals(['query' => ['match_all' => []], 'sort' => ['_score'], 'size' => 100, 'from' => 0]);
     });
     $this->specify('match all query with global aggregation', function () {
         $this->search->addAggregation($this->aggregation);
         verify($this->search->buildBody())->equals(['query' => ['match_all' => []], 'aggs' => ['global_name' => ['global' => new stdClass(), 'aggs' => ['min_name' => ['min' => ['field' => 'field_name']], 'max_name' => ['max' => ['field' => 'field_name']]]]], 'size' => 100, 'from' => 0]);
     });
 }
 /**
  * @inheritdoc
  */
 public function _before()
 {
     $this->service = $this->getMockBuilder('\\Nord\\Lumen\\Elasticsearch\\ElasticsearchService')->disableOriginalConstructor()->getMock();
     $this->query = new \Nord\Lumen\Elasticsearch\Search\Query\Compound\BoolQuery();
     $termQuery = new \Nord\Lumen\Elasticsearch\Search\Query\TermLevel\TermQuery();
     $this->query->addMust($termQuery->setField('field1')->setValue('value1'));
     $this->search = new \Nord\Lumen\Elasticsearch\Search\Search();
     $this->search->setPage(1)->setSize(2)->setQuery($this->query);
     $this->adapter = new \Nord\Lumen\Elasticsearch\Pagerfanta\Adapter\ElasticsearchAdapter($this->service, $this->search);
 }
 /**
  * @param int|null $offset
  * @param int|null $length
  * @return array
  */
 public function getResult($offset = null, $length = null)
 {
     if (!is_null($offset) && !is_null($length)) {
         $page = $offset / $length + 1;
         $size = $length;
         if ($page !== $this->search->getPage() || $size !== $this->search->getSize()) {
             $this->result = null;
             $this->search->setPage($page)->setSize($size);
         }
     }
     if (empty($this->result)) {
         $this->result = $this->elasticsearch->execute($this->search);
     }
     return $this->result;
 }
 /**
  * @inheritdoc
  */
 public function execute(Search $search)
 {
     return $this->search(['index' => $search->getIndex(), 'type' => $search->getType(), 'body' => $search->buildBody()]);
 }