2016-12-09 11:47:28 +01:00
|
|
|
<?php
|
2017-07-06 23:48:47 +02:00
|
|
|
namespace Codappix\SearchCore\Connection\Elasticsearch;
|
2016-12-09 11:47:28 +01:00
|
|
|
|
|
|
|
/*
|
2016-12-09 13:19:35 +01:00
|
|
|
* Copyright (C) 2016 Daniel Siepmann <coding@daniel-siepmann.de>
|
2016-12-09 11:47:28 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Connection\FacetInterface;
|
|
|
|
use Codappix\SearchCore\Connection\ResultItemInterface;
|
2017-08-08 17:07:23 +02:00
|
|
|
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Connection\SearchResultInterface;
|
2018-03-06 09:04:47 +01:00
|
|
|
use Codappix\SearchCore\Domain\Model\QueryResultInterfaceStub;
|
|
|
|
use Codappix\SearchCore\Domain\Model\ResultItem;
|
2017-07-06 12:03:52 +02:00
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
2016-12-09 11:47:28 +01:00
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
class SearchResult implements SearchResultInterface
|
2016-12-09 11:47:28 +01:00
|
|
|
{
|
2018-03-06 09:04:47 +01:00
|
|
|
use QueryResultInterfaceStub;
|
|
|
|
|
2017-08-08 17:07:23 +02:00
|
|
|
/**
|
|
|
|
* @var SearchRequestInterface
|
|
|
|
*/
|
|
|
|
protected $searchRequest;
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* @var \Elastica\ResultSet
|
|
|
|
*/
|
|
|
|
protected $result;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<FacetInterface>
|
|
|
|
*/
|
|
|
|
protected $facets = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<ResultItemInterface>
|
|
|
|
*/
|
|
|
|
protected $results = [];
|
|
|
|
|
2017-09-06 22:38:46 +02:00
|
|
|
/**
|
|
|
|
* For Iterator interface.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $position = 0;
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* @var ObjectManagerInterface
|
|
|
|
*/
|
|
|
|
protected $objectManager;
|
|
|
|
|
2017-08-08 17:07:23 +02:00
|
|
|
public function __construct(
|
|
|
|
SearchRequestInterface $searchRequest,
|
|
|
|
\Elastica\ResultSet $result,
|
|
|
|
ObjectManagerInterface $objectManager
|
|
|
|
) {
|
|
|
|
$this->searchRequest = $searchRequest;
|
2017-07-06 12:03:52 +02:00
|
|
|
$this->result = $result;
|
|
|
|
$this->objectManager = $objectManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<ResultItemInterface>
|
|
|
|
*/
|
2018-03-06 17:40:49 +01:00
|
|
|
public function getResults() : array
|
2017-07-06 12:03:52 +02:00
|
|
|
{
|
|
|
|
$this->initResults();
|
|
|
|
|
|
|
|
return $this->results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all facets, if any.
|
|
|
|
*
|
2017-10-14 13:06:22 +02:00
|
|
|
* @return array<FacetInterface>
|
2017-07-06 12:03:52 +02:00
|
|
|
*/
|
2018-03-06 17:40:49 +01:00
|
|
|
public function getFacets() : array
|
2017-07-06 12:03:52 +02:00
|
|
|
{
|
|
|
|
$this->initFacets();
|
|
|
|
|
|
|
|
return $this->facets;
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:40:49 +01:00
|
|
|
public function getCurrentCount() : int
|
2017-07-06 12:03:52 +02:00
|
|
|
{
|
2017-08-08 17:07:23 +02:00
|
|
|
return $this->result->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function initResults()
|
|
|
|
{
|
|
|
|
if ($this->results !== []) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->result->getResults() as $result) {
|
2018-03-20 16:07:28 +01:00
|
|
|
$this->results[] = new ResultItem($result->getData(), $result->getParam('_type'));
|
2017-08-08 17:07:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function initFacets()
|
|
|
|
{
|
|
|
|
if ($this->facets !== [] || !$this->result->hasAggregations()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->result->getAggregations() as $aggregationName => $aggregation) {
|
|
|
|
$this->facets[$aggregationName] = $this->objectManager->get(Facet::class, $aggregationName, $aggregation);
|
|
|
|
}
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Countable - Interface
|
|
|
|
public function count()
|
|
|
|
{
|
2017-08-08 17:07:23 +02:00
|
|
|
return $this->result->getTotalHits();
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator - Interface
|
|
|
|
public function current()
|
|
|
|
{
|
2017-09-06 22:38:46 +02:00
|
|
|
return $this->getResults()[$this->position];
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function next()
|
|
|
|
{
|
2017-09-06 22:38:46 +02:00
|
|
|
++$this->position;
|
|
|
|
|
|
|
|
return $this->current();
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function key()
|
|
|
|
{
|
2017-09-06 22:38:46 +02:00
|
|
|
return $this->position;
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function valid()
|
|
|
|
{
|
2017-09-06 22:38:46 +02:00
|
|
|
return isset($this->getResults()[$this->position]);
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rewind()
|
|
|
|
{
|
2017-09-06 22:38:46 +02:00
|
|
|
$this->position = 0;
|
2017-07-06 12:03:52 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 17:07:23 +02:00
|
|
|
public function getQuery()
|
2017-07-06 12:03:52 +02:00
|
|
|
{
|
2017-08-08 17:07:23 +02:00
|
|
|
return $this->searchRequest;
|
|
|
|
}
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|