/**
  * Create and return a result set for the given search response
  *
  * @param   RestApiResponse     $response
  *
  * @return  array
  */
 public function createSearchResult(RestApiResponse $response)
 {
     $foldedColumn = $this->getUnfoldAttribute();
     $requestedFields = $this->getColumns();
     $offset = $this->getOffset();
     $limit = $this->getLimit();
     $count = 0;
     $result = array();
     $json = $response->json();
     foreach ($json['hits']['hits'] as $hit) {
         $hit = new SearchHit($hit);
         if ($foldedColumn === null) {
             $result[] = $hit->createRow($requestedFields);
         } else {
             foreach ($hit->createRows($requestedFields, $foldedColumn) as $row) {
                 $matches = true;
                 if (isset($hit['highlight'])) {
                     foreach ($this->foldedHighlights() as $column => $field) {
                         if (isset($hit['highlight'][$field])) {
                             $value = $row->{$foldedColumn};
                             if (is_string($column) && is_object($value)) {
                                 $value = $value->{$column};
                             }
                             if (!in_array($value, $hit['highlight'][$field], true)) {
                                 $matches = false;
                                 break;
                             }
                         }
                     }
                 }
                 if ($matches) {
                     $count += 1;
                     if ($offset === 0 || $offset < $count) {
                         $result[] = $row;
                     }
                     if ($limit > 0 && $limit === count($result)) {
                         return $result;
                     }
                 }
             }
         }
     }
     return $result;
 }
 /**
  * Render and return a human readable error message for the given error document
  *
  * @return  string
  *
  * @todo    Parse Elasticsearch 2.x structured errors
  */
 public function renderErrorMessage(RestApiResponse $response)
 {
     try {
         $errorDocument = $response->json();
     } catch (IcingaException $e) {
         return sprintf('%s: %s', $e->getMessage(), $response->getPayload());
     }
     if (!isset($errorDocument['error'])) {
         return sprintf('Elasticsearch unknown json error %s: %s', $response->getStatusCode(), $response->getPayload());
     }
     if (is_string($errorDocument['error'])) {
         return $errorDocument['error'];
     }
     return sprintf('Elasticsearch json error %s: %s', $response->getStatusCode(), json_encode($errorDocument['error']));
 }