/** * 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; }
/** * Fetch and return the given document * * In case you are only interested in the source, pass "_source" as the only desired field. * * @param string $index The index the document is located in * @param string $documentType The type of the document to fetch * @param string $id The id of the document to fetch * @param array $fields The desired fields to return instead of all fields * @param UrlParams $params Additional URL parameters to add to the request * * @return object|false Returns false in case no document could be found */ public function fetchDocument($index, $documentType, $id, array $fields = null, UrlParams $params = null) { $request = new GetApiRequest($index, $documentType, $id); if ($params !== null) { $request->setParams($params); } if (!empty($fields)) { if (count($fields) == 1 && reset($fields) === '_source') { $request->setSourceOnly(); $fields = null; } elseif (!$request->getParams()->has('_source')) { $request->getParams()->set('_source', join(',', $fields)); } } $response = $this->request($request); if (!$response->isSuccess()) { if ($response->getStatusCode() === 404) { return false; } throw new QueryException($this->renderErrorMessage($response)); } $hit = new SearchHit($response->json()); return $hit->createRow($fields ?: array()); }