Example #1
0
 /**
  * @param int|null $offset
  * @param int|null $count
  * @return array
  */
 private function _getResult($offset = null, $count = null)
 {
     $cacheKey = array($this->_query->getSort(), $offset, $count);
     if (($result = $this->_cacheGet($cacheKey)) === false) {
         $data = array('query' => $this->_query->getQuery(), 'sort' => $this->_query->getSort());
         if ($this->_fields) {
             $data['fields'] = $this->_fields;
         }
         if ($offset !== null) {
             $data['from'] = $offset;
         }
         if ($count !== null) {
             $data['size'] = $count;
         }
         $searchResult = CM_Service_Manager::getInstance()->getElasticsearch()->query($this->_types, $data);
         $result = array('items' => array(), 'total' => 0);
         if (isset($searchResult['hits'])) {
             foreach ($searchResult['hits']['hits'] as $hit) {
                 if ($this->_fields && array_key_exists('fields', $hit)) {
                     if ($this->_returnType) {
                         $idArray = array('id' => $hit['_id'], 'type' => $hit['_type']);
                     } else {
                         $idArray = array('id' => $hit['_id']);
                     }
                     $fields = $hit['fields'];
                     $fields = Functional\map($fields, function ($field) {
                         if (is_array($field) && 1 == count($field)) {
                             $field = reset($field);
                         }
                         return $field;
                     });
                     $result['items'][] = array_merge($idArray, $fields);
                 } else {
                     if ($this->_returnType) {
                         $item = array('id' => $hit['_id'], 'type' => $hit['_type']);
                     } else {
                         $item = $hit['_id'];
                     }
                     $result['items'][] = $item;
                 }
             }
             $result['total'] = $searchResult['hits']['total'];
         }
         $this->_cacheSet($cacheKey, $result);
     }
     return $result;
 }