PHRAS-442 #time 1h

less coupling (mdarse)
This commit is contained in:
Jean-Yves Gaulier
2015-06-22 19:07:56 +02:00
parent 0584fddfc8
commit 2ee19c17f5
2 changed files with 28 additions and 17 deletions

View File

@@ -21,7 +21,6 @@ use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface; use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
use Alchemy\Phrasea\SearchEngine\SearchEngineOptions; use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
use Alchemy\Phrasea\SearchEngine\SearchEngineResult; use Alchemy\Phrasea\SearchEngine\SearchEngineResult;
use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion;
use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Exception\RuntimeException;
use Closure; use Closure;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
@@ -284,7 +283,6 @@ class ElasticSearchEngine implements SearchEngineInterface
$res = $this->client->search($params); $res = $this->client->search($params);
$results = new ArrayCollection(); $results = new ArrayCollection();
$suggestions = new ArrayCollection();
$n = 0; $n = 0;
foreach ($res['hits']['hits'] as $hit) { foreach ($res['hits']['hits'] as $hit) {
@@ -294,21 +292,25 @@ class ElasticSearchEngine implements SearchEngineInterface
/** @var FacetsResponse $facets */ /** @var FacetsResponse $facets */
$facets = $this->facetsResponseFactory->__invoke($res); $facets = $this->facetsResponseFactory->__invoke($res);
// for es, suggestions are a flat view of facets (api backward compatibility)
$suggestions->clear();
foreach($facets->getFacets() as $facet) {
foreach($facet['values'] as $value) {
$suggestions->add(new SearchEngineSuggestion($value['query'], $value['value'], $value['count']));
}
}
$query['ast'] = $this->app['query_compiler']->parse($string)->dump(); $query['ast'] = $this->app['query_compiler']->parse($string)->dump();
$query['query_main'] = $recordQuery; $query['query_main'] = $recordQuery;
$query['query'] = $params['body']; $query['query'] = $params['body'];
$query['query_string'] = json_encode($params['body']); $query['query_string'] = json_encode($params['body']);
return new SearchEngineResult($results, json_encode($query), $res['took'], $offset, return new SearchEngineResult(
$res['hits']['total'], $res['hits']['total'], null, null, $suggestions, [], $results, // ArrayCollection of results
$this->indexName, $facets); json_encode($query),
$res['took'], // duration
$offset, // offset start
$res['hits']['total'], // available
$res['hits']['total'], // total
null, // error
null, // warning
$facets->getAsSuggestions(), // ArrayCollection of suggestions
[], // propositions
$this->indexName,
$facets
);
} }
/** /**

View File

@@ -3,6 +3,8 @@
namespace Alchemy\Phrasea\SearchEngine\Elastic\Search; namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
use Alchemy\Phrasea\Exception\RuntimeException; use Alchemy\Phrasea\Exception\RuntimeException;
use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion;
use Doctrine\Common\Collections\ArrayCollection;
use JsonSerializable; use JsonSerializable;
class FacetsResponse implements JsonSerializable class FacetsResponse implements JsonSerializable
@@ -32,13 +34,20 @@ class FacetsResponse implements JsonSerializable
} }
/** /**
* Term aggregates * @return ArrayCollection
*
* @return array
*/ */
public function getFacets() public function getAsSuggestions()
{ {
return $this->facets; $suggestions = new ArrayCollection();
// for es, suggestions are a flat view of facets (api backward compatibility)
foreach ($this->facets as $facet) {
foreach ($facet['values'] as $value) {
$suggestions->add(new SearchEngineSuggestion($value['query'], $value['value'], $value['count']));
}
}
return $suggestions;
} }
private function buildBucketsValues($name, $buckets) private function buildBucketsValues($name, $buckets)