mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 04:23:19 +00:00
Merge pull request #1395 from jygaulier/PHRAS-442_FACETS-IN-API
PHRAS-442
This commit is contained in:
@@ -1111,6 +1111,7 @@ class V1Controller extends Controller
|
||||
function (SearchEngineSuggestion $suggestion) {
|
||||
return $suggestion->toArray();
|
||||
}, $search_result->getSuggestions()->toArray()),
|
||||
'facets' => $search_result->getFacets(),
|
||||
'results' => [],
|
||||
'query' => $search_result->getQuery(),
|
||||
];
|
||||
|
@@ -271,13 +271,13 @@ class ElasticSearchEngine implements SearchEngineInterface
|
||||
$res = $this->client->search($params);
|
||||
|
||||
$results = new ArrayCollection();
|
||||
$suggestions = new ArrayCollection();
|
||||
|
||||
$n = 0;
|
||||
foreach ($res['hits']['hits'] as $hit) {
|
||||
$results[] = ElasticsearchRecordHydrator::hydrate($hit, $n++);
|
||||
}
|
||||
|
||||
/** @var FacetsResponse $facets */
|
||||
$facets = $this->facetsResponseFactory->__invoke($res);
|
||||
|
||||
$query['ast'] = $this->app['query_compiler']->parse($string)->dump();
|
||||
@@ -285,9 +285,20 @@ class ElasticSearchEngine implements SearchEngineInterface
|
||||
$query['query'] = $params['body'];
|
||||
$query['query_string'] = json_encode($params['body']);
|
||||
|
||||
return new SearchEngineResult($results, json_encode($query), $res['took'], $offset,
|
||||
$res['hits']['total'], $res['hits']['total'], null, null, $suggestions, [],
|
||||
$this->indexName, $facets);
|
||||
return new SearchEngineResult(
|
||||
$results, // ArrayCollection of results
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -3,6 +3,8 @@
|
||||
namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
|
||||
|
||||
use Alchemy\Phrasea\Exception\RuntimeException;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use JsonSerializable;
|
||||
|
||||
class FacetsResponse implements JsonSerializable
|
||||
@@ -31,6 +33,23 @@ class FacetsResponse implements JsonSerializable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayCollection
|
||||
*/
|
||||
public function getAsSuggestions()
|
||||
{
|
||||
$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)
|
||||
{
|
||||
$values = array();
|
||||
|
@@ -74,7 +74,8 @@ class SearchEngineSuggestion
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'query' => $this->getSuggestion(),
|
||||
'suggestion' => $this->getSuggestion(),
|
||||
'query' => $this->getQuery(),
|
||||
'hits' => $this->getHits(),
|
||||
];
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ class SearchEngineSuggestionTest extends \PhraseanetTestCase
|
||||
$hits = 35;
|
||||
|
||||
$suggestion = new SearchEngineSuggestion($query, $words, $hits);
|
||||
$this->assertEquals(['query' => $words, 'hits' => 35], $suggestion->toArray());
|
||||
$this->assertEquals(['query' => $query, 'hits' => 35, 'suggestion' => $words], $suggestion->toArray());
|
||||
}
|
||||
|
||||
public function testToArrayWithNullValue()
|
||||
@@ -54,6 +54,6 @@ class SearchEngineSuggestionTest extends \PhraseanetTestCase
|
||||
$hits = null;
|
||||
|
||||
$suggestion = new SearchEngineSuggestion($query, $words, $hits);
|
||||
$this->assertEquals(['query' => $words, 'hits' => null], $suggestion->toArray());
|
||||
$this->assertEquals(['query' => $query, 'hits' => null, 'suggestion' => $words], $suggestion->toArray());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user