mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 20:43:25 +00:00
Retrieve Collections indexed by field name
This commit is contained in:
@@ -16,6 +16,7 @@ use Alchemy\Phrasea\SearchEngine\Elastic\Indexer\RecordIndexer;
|
|||||||
use Alchemy\Phrasea\SearchEngine\Elastic\Indexer\TermIndexer;
|
use Alchemy\Phrasea\SearchEngine\Elastic\Indexer\TermIndexer;
|
||||||
use Alchemy\Phrasea\SearchEngine\Elastic\RecordHelper;
|
use Alchemy\Phrasea\SearchEngine\Elastic\RecordHelper;
|
||||||
use Alchemy\Phrasea\SearchEngine\Elastic\Search\FacetsResponse;
|
use Alchemy\Phrasea\SearchEngine\Elastic\Search\FacetsResponse;
|
||||||
|
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryCompiler;
|
||||||
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
|
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
|
||||||
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
|
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
|
||||||
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
|
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
|
||||||
@@ -247,7 +248,9 @@ class ElasticSearchEngine implements SearchEngineInterface
|
|||||||
$options = $options ?: new SearchEngineOptions();
|
$options = $options ?: new SearchEngineOptions();
|
||||||
|
|
||||||
$context = $this->createQueryContext($options);
|
$context = $this->createQueryContext($options);
|
||||||
$recordQuery = $this->app['query_compiler']->compile($string, $context);
|
/** @var QueryCompiler $query_compiler */
|
||||||
|
$query_compiler = $this->app['query_compiler'];
|
||||||
|
$recordQuery = $query_compiler->compile($string, $context);
|
||||||
|
|
||||||
$params = $this->createRecordQueryParams($recordQuery, $options, null);
|
$params = $this->createRecordQueryParams($recordQuery, $options, null);
|
||||||
|
|
||||||
@@ -276,7 +279,7 @@ class ElasticSearchEngine implements SearchEngineInterface
|
|||||||
/** @var FacetsResponse $facets */
|
/** @var FacetsResponse $facets */
|
||||||
$facets = $this->facetsResponseFactory->__invoke($res);
|
$facets = $this->facetsResponseFactory->__invoke($res);
|
||||||
|
|
||||||
$query['ast'] = $this->app['query_compiler']->parse($string)->dump();
|
$query['ast'] = $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']);
|
||||||
@@ -306,18 +309,43 @@ class ElasticSearchEngine implements SearchEngineInterface
|
|||||||
$this->locales,
|
$this->locales,
|
||||||
$this->app['locale']
|
$this->app['locale']
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return $queryContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of allowed collection base_id indexed by field name.
|
||||||
|
*
|
||||||
|
* [
|
||||||
|
* "FieldName" => [1, 4, 5],
|
||||||
|
* "OtherFieldName" => [4],
|
||||||
|
* ]
|
||||||
|
*
|
||||||
|
* @param SearchEngineOptions $options
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
private function getAllowedPrivateFields(SearchEngineOptions $options)
|
private function getAllowedPrivateFields(SearchEngineOptions $options)
|
||||||
{
|
{
|
||||||
$collections = $options->getBusinessFieldsOn();
|
$fields = array_keys($this->structure->getPrivateFields());
|
||||||
$private_fields = $this->structure->getPrivateFields();
|
|
||||||
$allowed = array();
|
$allowed = array_fill_keys($fields, []);
|
||||||
// TODO Build a map with the collections allowed for each private field
|
foreach ($options->getDataboxes() as $databox) {
|
||||||
// [
|
$databoxFields = $databox->get_meta_structure();
|
||||||
// "FieldName" => [1, 4, 5],
|
foreach ($fields as $field) {
|
||||||
// "OtherFieldName" => [4],
|
if ($databoxFields->get_element_by_name($field)) {
|
||||||
// ]
|
$allowed[$field] += $databox->get_collection_unique_ids();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$businessCollections = array_map(function (\collection $collection) {
|
||||||
|
return $collection->get_base_id();
|
||||||
|
}, $options->getBusinessFieldsOn());
|
||||||
|
|
||||||
|
// Remove collections base_id which access is restricted.
|
||||||
|
foreach ($allowed as $name => &$collections) {
|
||||||
|
$collections = array_diff($collections, $businessCollections);
|
||||||
|
}
|
||||||
|
|
||||||
return $allowed;
|
return $allowed;
|
||||||
}
|
}
|
||||||
|
@@ -39,6 +39,8 @@ class SearchEngineOptions
|
|||||||
protected $search_type = 0;
|
protected $search_type = 0;
|
||||||
/** @var \collection[] */
|
/** @var \collection[] */
|
||||||
protected $collections = [];
|
protected $collections = [];
|
||||||
|
/** @var null|\databox[] */
|
||||||
|
private $databoxes;
|
||||||
/** @var \databox_field[] */
|
/** @var \databox_field[] */
|
||||||
protected $fields = [];
|
protected $fields = [];
|
||||||
protected $status = [];
|
protected $status = [];
|
||||||
@@ -217,6 +219,8 @@ class SearchEngineOptions
|
|||||||
public function onCollections(array $collections)
|
public function onCollections(array $collections)
|
||||||
{
|
{
|
||||||
$this->collections = $collections;
|
$this->collections = $collections;
|
||||||
|
// Defer databox retrieval
|
||||||
|
$this->databoxes = null;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -580,17 +584,10 @@ class SearchEngineOptions
|
|||||||
/** @var \collection[] $bas */
|
/** @var \collection[] $bas */
|
||||||
$bas = array_filter($bas, $filter);
|
$bas = array_filter($bas, $filter);
|
||||||
|
|
||||||
/** @var \databox[] $databoxes */
|
$options->onCollections($bas);
|
||||||
$databoxes = [];
|
|
||||||
|
|
||||||
foreach ($bas as $collection) {
|
|
||||||
if (!isset($databoxes[$collection->get_sbas_id()])) {
|
|
||||||
$databoxes[$collection->get_sbas_id()] = $collection->get_databox();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($isAuthenticated && $acl->has_right('modifyrecord')) {
|
if ($isAuthenticated && $acl->has_right('modifyrecord')) {
|
||||||
$bf = array_filter($bas, function (\collection $collection) use ($app, $acl) {
|
$bf = array_filter($bas, function (\collection $collection) use ($acl) {
|
||||||
return $acl->has_right_on_base($collection->get_base_id(), 'canmodifrecord');
|
return $acl->has_right_on_base($collection->get_base_id(), 'canmodifrecord');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -601,7 +598,7 @@ class SearchEngineOptions
|
|||||||
$fields = is_array($request->get('fields')) ? $request->get('fields') : [];
|
$fields = is_array($request->get('fields')) ? $request->get('fields') : [];
|
||||||
if (empty($fields)) {
|
if (empty($fields)) {
|
||||||
// Select all fields (business included)
|
// Select all fields (business included)
|
||||||
foreach ($databoxes as $databox) {
|
foreach ($options->getDataboxes() as $databox) {
|
||||||
foreach ($databox->get_meta_structure() as $field) {
|
foreach ($databox->get_meta_structure() as $field) {
|
||||||
$fields[] = $field->get_name();
|
$fields[] = $field->get_name();
|
||||||
}
|
}
|
||||||
@@ -610,7 +607,7 @@ class SearchEngineOptions
|
|||||||
}
|
}
|
||||||
|
|
||||||
$databoxFields = [];
|
$databoxFields = [];
|
||||||
|
$databoxes = $options->getDataboxes();
|
||||||
foreach ($databoxes as $databox) {
|
foreach ($databoxes as $databox) {
|
||||||
$metaStructure = $databox->get_meta_structure();
|
$metaStructure = $databox->get_meta_structure();
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
@@ -628,8 +625,6 @@ class SearchEngineOptions
|
|||||||
$options->setFields($databoxFields);
|
$options->setFields($databoxFields);
|
||||||
$options->setStatus($status);
|
$options->setStatus($status);
|
||||||
|
|
||||||
$options->onCollections($bas);
|
|
||||||
|
|
||||||
$options->setSearchType($request->get('search_type'));
|
$options->setSearchType($request->get('search_type'));
|
||||||
$options->setRecordType($request->get('record_type'));
|
$options->setRecordType($request->get('record_type'));
|
||||||
|
|
||||||
|
@@ -229,6 +229,12 @@ class databox extends base
|
|||||||
|
|
||||||
public function get_collection_unique_ids()
|
public function get_collection_unique_ids()
|
||||||
{
|
{
|
||||||
|
static $base_ids;
|
||||||
|
|
||||||
|
if (isset($base_ids)) {
|
||||||
|
return $base_ids;
|
||||||
|
}
|
||||||
|
|
||||||
$conn = $this->app['phraseanet.appbox']->get_connection();
|
$conn = $this->app['phraseanet.appbox']->get_connection();
|
||||||
$sql = "SELECT b.base_id FROM bas b
|
$sql = "SELECT b.base_id FROM bas b
|
||||||
WHERE b.sbas_id = :sbas_id AND b.active = '1'
|
WHERE b.sbas_id = :sbas_id AND b.active = '1'
|
||||||
|
Reference in New Issue
Block a user