mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 12:33:26 +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) {
|
function (SearchEngineSuggestion $suggestion) {
|
||||||
return $suggestion->toArray();
|
return $suggestion->toArray();
|
||||||
}, $search_result->getSuggestions()->toArray()),
|
}, $search_result->getSuggestions()->toArray()),
|
||||||
|
'facets' => $search_result->getFacets(),
|
||||||
'results' => [],
|
'results' => [],
|
||||||
'query' => $search_result->getQuery(),
|
'query' => $search_result->getQuery(),
|
||||||
];
|
];
|
||||||
|
@@ -271,13 +271,13 @@ 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) {
|
||||||
$results[] = ElasticsearchRecordHydrator::hydrate($hit, $n++);
|
$results[] = ElasticsearchRecordHydrator::hydrate($hit, $n++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var FacetsResponse $facets */
|
||||||
$facets = $this->facetsResponseFactory->__invoke($res);
|
$facets = $this->facetsResponseFactory->__invoke($res);
|
||||||
|
|
||||||
$query['ast'] = $this->app['query_compiler']->parse($string)->dump();
|
$query['ast'] = $this->app['query_compiler']->parse($string)->dump();
|
||||||
@@ -285,9 +285,20 @@ class ElasticSearchEngine implements SearchEngineInterface
|
|||||||
$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
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -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
|
||||||
@@ -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)
|
private function buildBucketsValues($name, $buckets)
|
||||||
{
|
{
|
||||||
$values = array();
|
$values = array();
|
||||||
|
@@ -74,7 +74,8 @@ class SearchEngineSuggestion
|
|||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'query' => $this->getSuggestion(),
|
'suggestion' => $this->getSuggestion(),
|
||||||
|
'query' => $this->getQuery(),
|
||||||
'hits' => $this->getHits(),
|
'hits' => $this->getHits(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@@ -44,7 +44,7 @@ class SearchEngineSuggestionTest extends \PhraseanetTestCase
|
|||||||
$hits = 35;
|
$hits = 35;
|
||||||
|
|
||||||
$suggestion = new SearchEngineSuggestion($query, $words, $hits);
|
$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()
|
public function testToArrayWithNullValue()
|
||||||
@@ -54,6 +54,6 @@ class SearchEngineSuggestionTest extends \PhraseanetTestCase
|
|||||||
$hits = null;
|
$hits = null;
|
||||||
|
|
||||||
$suggestion = new SearchEngineSuggestion($query, $words, $hits);
|
$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