From f196155049259e65805a19946e73a3bd4c7f9525 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Tue, 14 Oct 2014 18:51:02 +0200 Subject: [PATCH] Fix PHRAS-322, add result debug informations (aggregations) --- lib/Alchemy/Phrasea/Controller/Prod/Query.php | 7 +++++ .../Elastic/ElasticSearchEngine.php | 26 ++++++++++++++++--- .../SearchEngine/SearchEngineResult.php | 13 +++++++++- www/skins/prod/jquery.main-prod.js | 21 ++++++++++++++- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Query.php b/lib/Alchemy/Phrasea/Controller/Prod/Query.php index 6e74685840..9c14a2851a 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Query.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Query.php @@ -12,6 +12,7 @@ namespace Alchemy\Phrasea\Controller\Prod; use Alchemy\Phrasea\SearchEngine\SearchEngineOptions; +use Alchemy\Phrasea\SearchEngine\SearchEngineResult; use Silex\Application; use Silex\ControllerProviderInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -69,6 +70,7 @@ class Query implements ControllerProviderInterface $page = 1; } + /** @var SearchEngineResult $result */ $result = $app['phraseanet.SE']->query($query, (($page - 1) * $perPage), $perPage, $options); $app['manipulator.user']->logQuery($app['authentication']->getUser(), $result->getQuery()); @@ -191,7 +193,12 @@ class Query implements ControllerProviderInterface ); $json['query'] = $query; + + /** Debug */ $json['parsed_query'] = $result->getQuery(); + $json['aggregations'] = $result->getAggregations(); + /** End debug */ + $json['phrasea_props'] = $proposals; $json['total_answers'] = (int) $result->getAvailable(); $json['next_page'] = ($page < $npages && $result->getAvailable() > 0) ? ($page + 1) : false; diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php index d4e3a7f24d..395138369a 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php @@ -327,8 +327,27 @@ class ElasticSearchEngine implements SearchEngineInterface } $params = $this->createRecordQueryParams($recordQuery, $options, null); - $params['from'] = $offset; - $params['size'] = $perPage; + $params['body']['from'] = $offset; + $params['body']['size'] = $perPage; + + // Debug at the moment. See https://phraseanet.atlassian.net/browse/PHRAS-322 + $params['body']['aggs'] = array ( + 'Keywords' => array ('terms' => + array ('field' => 'caption.Keywords.raw', 'size' => 10), + ), + 'Photographer' => array ('terms' => + array ('field' => 'caption.Photographer.raw', 'size' => 10), + ), + 'Headline' => array ('terms' => + array ('field' => 'caption.Headline.raw', 'size' => 10), + ), + 'City' => array ('terms' => + array ('field' => 'caption.City.raw', 'size' => 10), + ), + 'Country' => array ('terms' => + array ('field' => 'caption.Country.raw', 'size' => 10), + ), + ); $res = $this->doExecute('search', $params); @@ -349,7 +368,8 @@ class ElasticSearchEngine implements SearchEngineInterface $query['query'] = json_encode($params); return new SearchEngineResult($results, json_encode($query), $res['took'], $offset, - $res['hits']['total'], $res['hits']['total'], null, null, $suggestions, [], $this->indexName); + $res['hits']['total'], $res['hits']['total'], null, null, $suggestions, [], + $this->indexName, $res['aggregations']); } /** diff --git a/lib/Alchemy/Phrasea/SearchEngine/SearchEngineResult.php b/lib/Alchemy/Phrasea/SearchEngine/SearchEngineResult.php index 476280816e..343f3859ed 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/SearchEngineResult.php +++ b/lib/Alchemy/Phrasea/SearchEngine/SearchEngineResult.php @@ -26,8 +26,10 @@ class SearchEngineResult protected $suggestions; protected $propositions; protected $indexes; + protected $aggregations; - public function __construct(ArrayCollection $results, $query, $duration, $offsetStart, $available, $total, $error, $warning, ArrayCollection $suggestions, $propositions, $indexes) + public function __construct(ArrayCollection $results, $query, $duration, $offsetStart, $available, $total, $error, + $warning, ArrayCollection $suggestions, $propositions, $indexes, $aggregations = array()) { $this->results = $results; $this->query = $query; @@ -40,6 +42,7 @@ class SearchEngineResult $this->suggestions = $suggestions; $this->propositions = $propositions; $this->indexes = $indexes; + $this->aggregations = $aggregations; return $this; } @@ -172,4 +175,12 @@ class SearchEngineResult { return $this->indexes; } + + /** + * @return array + */ + public function getAggregations() + { + return $this->aggregations; + } } diff --git a/www/skins/prod/jquery.main-prod.js b/www/skins/prod/jquery.main-prod.js index 3f8790b9f0..03b10ac247 100644 --- a/www/skins/prod/jquery.main-prod.js +++ b/www/skins/prod/jquery.main-prod.js @@ -472,8 +472,27 @@ function initAnswerForm() { query = JSON.parse(query); } catch (e) {} - console.log('Parsed Query:', query); + console.info('All Details:', query); + console.debug('Paths:'); + console.debug(query._paths); + + var aggs = datas.aggregations; + try { + aggs = JSON.parse(aggs); + } + catch (e) {} + + console.debug('Aggregations:'); + var toDisplay = []; + _.each(aggs, function(value, key) { + _.each(value.buckets, function(bucket, keyBis) { + if (!toDisplay[keyBis]) { toDisplay[keyBis] = {}; } + toDisplay[keyBis][key] = bucket.key + ' ('+ bucket.doc_count + ')'; + }); + + }); + console.table(toDisplay); $('#answers').empty().append(datas.results).removeClass('loading');