mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 07:23:13 +00:00
Remove some arguments from query and add them to options
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
/**
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2016 Alchemy
|
||||
@@ -49,14 +49,13 @@ class SearchController extends Controller
|
||||
private function searchAndFormatEngineResult(Request $request)
|
||||
{
|
||||
$options = SearchEngineOptions::fromRequest($this->app, $request);
|
||||
|
||||
$offsetStart = (int) ($request->get('offset_start') ?: 0);
|
||||
$perPage = (int) $request->get('per_page') ?: 10;
|
||||
$options->setFirstResult($request->get('offset_start') ?: 0);
|
||||
$options->setMaxResults($request->get('per_page') ?: 10);
|
||||
|
||||
$query = (string) $request->get('query');
|
||||
$this->getSearchEngine()->resetCache();
|
||||
|
||||
$search_result = $this->getSearchEngine()->query($query, $offsetStart, $perPage, $options);
|
||||
$search_result = $this->getSearchEngine()->query($query, $options);
|
||||
|
||||
$this->getUserManipulator()->logQuery($this->getAuthenticatedUser(), $search_result->getQuery());
|
||||
|
||||
@@ -74,8 +73,8 @@ class SearchController extends Controller
|
||||
$this->getSearchEngine()->clearCache();
|
||||
|
||||
$ret = [
|
||||
'offset_start' => $offsetStart,
|
||||
'per_page' => $perPage,
|
||||
'offset_start' => $options->getFirstResult(),
|
||||
'per_page' => $options->getMaxResults(),
|
||||
'available_results' => $search_result->getAvailable(),
|
||||
'total_results' => $search_result->getTotal(),
|
||||
'error' => (string)$search_result->getError(),
|
||||
|
@@ -1088,13 +1088,13 @@ class V1Controller extends Controller
|
||||
{
|
||||
$options = SearchEngineOptions::fromRequest($this->app, $request);
|
||||
|
||||
$offsetStart = (int) ($request->get('offset_start') ?: 0);
|
||||
$perPage = (int) $request->get('per_page') ?: 10;
|
||||
$options->setFirstResult((int) ($request->get('offset_start') ?: 0));
|
||||
$options->setMaxResults((int) $request->get('per_page') ?: 10);
|
||||
|
||||
$query = (string) $request->get('query');
|
||||
$this->getSearchEngine()->resetCache();
|
||||
|
||||
$search_result = $this->getSearchEngine()->query($query, $offsetStart, $perPage, $options);
|
||||
$search_result = $this->getSearchEngine()->query($query, $options);
|
||||
|
||||
$this->getUserManipulator()->logQuery($this->getAuthenticatedUser(), $search_result->getQuery());
|
||||
|
||||
@@ -1112,8 +1112,8 @@ class V1Controller extends Controller
|
||||
$this->getSearchEngine()->clearCache();
|
||||
|
||||
$ret = [
|
||||
'offset_start' => $offsetStart,
|
||||
'per_page' => $perPage,
|
||||
'offset_start' => $options->getFirstResult(),
|
||||
'per_page' => $options->getMaxResults(),
|
||||
'available_results' => $search_result->getAvailable(),
|
||||
'total_results' => $search_result->getTotal(),
|
||||
'error' => (string)$search_result->getError(),
|
||||
|
@@ -43,8 +43,6 @@ class QueryController extends Controller
|
||||
|
||||
$options = SearchEngineOptions::fromRequest($this->app, $request);
|
||||
|
||||
$form = $options->serialize();
|
||||
|
||||
$perPage = (int) $this->getSettings()->getUserSetting($this->getAuthenticatedUser(), 'images_per_page');
|
||||
|
||||
$page = (int) $request->request->get('pag');
|
||||
@@ -56,13 +54,15 @@ class QueryController extends Controller
|
||||
$page = 1;
|
||||
}
|
||||
|
||||
$options->setFirstResult(($page - 1) * $perPage);
|
||||
$options->setMaxResults($perPage);
|
||||
|
||||
$user = $this->getAuthenticatedUser();
|
||||
$userManipulator = $this->getUserManipulator();
|
||||
$userManipulator->logQuery($user, $query);
|
||||
|
||||
try {
|
||||
/** @var SearchEngineResult $result */
|
||||
$result = $engine->query($query, (($page - 1) * $perPage), $perPage, $options);
|
||||
$result = $engine->query($query, $options);
|
||||
|
||||
if ($this->getSettings()->getUserSetting($user, 'start_page') === 'LAST_QUERY') {
|
||||
$userManipulator->setUserSetting($user, 'start_page_query', $query);
|
||||
@@ -216,7 +216,7 @@ class QueryController extends Controller
|
||||
$json['total_answers'] = (int) $result->getAvailable();
|
||||
$json['next_page'] = ($page < $npages && $result->getAvailable() > 0) ? ($page + 1) : false;
|
||||
$json['prev_page'] = ($page > 1 && $result->getAvailable() > 0) ? ($page - 1) : false;
|
||||
$json['form'] = $form;
|
||||
$json['form'] = $options->serialize();
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
// we'd like a message from the parser so get all the exceptions messages
|
||||
|
@@ -268,7 +268,7 @@ class ElasticSearchEngine implements SearchEngineInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function query($string, $offset, $perPage, SearchEngineOptions $options = null)
|
||||
public function query($string, SearchEngineOptions $options = null)
|
||||
{
|
||||
$options = $options ?: new SearchEngineOptions();
|
||||
$context = $this->context_factory->createContext($options);
|
||||
@@ -282,13 +282,15 @@ class ElasticSearchEngine implements SearchEngineInterface
|
||||
// ask ES to return field _version (incremental version number of document)
|
||||
$params['body']['version'] = true;
|
||||
|
||||
$params['body']['from'] = $offset;
|
||||
$params['body']['size'] = $perPage;
|
||||
$params['body']['from'] = $options->getFirstResult();
|
||||
$params['body']['size'] = $options->getMaxResults();
|
||||
if($this->options->getHighlight()) {
|
||||
$params['body']['highlight'] = $this->buildHighlightRules($context);
|
||||
}
|
||||
|
||||
if ($aggs = $this->getAggregationQueryParams($options)) {
|
||||
$aggs = $this->getAggregationQueryParams($options);
|
||||
|
||||
if ($aggs) {
|
||||
$params['body']['aggs'] = $aggs;
|
||||
}
|
||||
|
||||
@@ -314,7 +316,7 @@ class ElasticSearchEngine implements SearchEngineInterface
|
||||
$results, // ArrayCollection of results
|
||||
json_encode($query),
|
||||
$res['took'], // duration
|
||||
$offset, // offset start
|
||||
$options->getFirstResult(),
|
||||
$res['hits']['total'], // available
|
||||
$res['hits']['total'], // total
|
||||
null, // error
|
||||
|
@@ -76,7 +76,7 @@ interface SearchEngineInterface
|
||||
|
||||
/**
|
||||
*
|
||||
* @return an array of self::GEM_TYPE_* indexed types
|
||||
* @return array an array of self::GEM_TYPE_* indexed types
|
||||
*/
|
||||
public function getAvailableTypes();
|
||||
|
||||
@@ -162,15 +162,12 @@ interface SearchEngineInterface
|
||||
public function updateFeedEntry(FeedEntry $entry);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $query
|
||||
* @param integer $offset
|
||||
* @param integer $perPage
|
||||
* @param SearchEngineOptions $options
|
||||
*
|
||||
* @return SearchEngineResult
|
||||
*/
|
||||
public function query($query, $offset, $perPage, SearchEngineOptions $options = null);
|
||||
public function query($query, SearchEngineOptions $options = null);
|
||||
|
||||
/**
|
||||
* Return an array of suggestions corresponding to the last word of the
|
||||
|
@@ -50,7 +50,7 @@ class SearchEngineOptions
|
||||
'sort_by',
|
||||
'sort_ord',
|
||||
'business_fields',
|
||||
'max_per_page',
|
||||
'max_results',
|
||||
'first_result',
|
||||
];
|
||||
|
||||
@@ -113,7 +113,7 @@ class SearchEngineOptions
|
||||
$options->allowBusinessFieldsOn($collectionNormalizer($value));
|
||||
},
|
||||
'first_result' => 'setFirstResult',
|
||||
'max_per_page' => 'setMaxPerPage',
|
||||
'max_results' => 'setResults',
|
||||
];
|
||||
|
||||
return $methods;
|
||||
@@ -149,7 +149,7 @@ class SearchEngineOptions
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $max_per_page = 10;
|
||||
private $max_results = 10;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
@@ -710,16 +710,16 @@ class SearchEngineOptions
|
||||
return $options;
|
||||
}
|
||||
|
||||
public function setMaxPerPage($max_per_page)
|
||||
public function setMaxResults($max_results)
|
||||
{
|
||||
Assertion::greaterOrEqualThan($max_per_page, 0);
|
||||
Assertion::greaterOrEqualThan($max_results, 0);
|
||||
|
||||
$this->max_per_page = (int)$max_per_page;
|
||||
$this->max_results = (int)$max_results;
|
||||
}
|
||||
|
||||
public function getMaxPerPage()
|
||||
public function getMaxResults()
|
||||
{
|
||||
return $this->max_per_page;
|
||||
return $this->max_results;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -192,6 +192,21 @@ SQL;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return caption_field[]
|
||||
*/
|
||||
public function getDCFields()
|
||||
{
|
||||
$databoxDcesFieldIds = array_map(function (databox_field $databox_field) {
|
||||
return $databox_field->get_id();
|
||||
}, $this->getDatabox()->get_meta_structure()->getDcesFields());
|
||||
|
||||
return array_intersect_key(
|
||||
$this->retrieve_fields(),
|
||||
array_fill_keys($databoxDcesFieldIds, null)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
* @return caption_field|null
|
||||
|
@@ -114,6 +114,16 @@ class databox_descriptionStructure implements IteratorAggregate, Countable
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return databox_field[]
|
||||
*/
|
||||
public function getDcesFields()
|
||||
{
|
||||
return array_filter($this->elements, function (databox_field $field) {
|
||||
return null !== $field->get_dces_element();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
* @return databox_field|null
|
||||
|
@@ -89,8 +89,13 @@ class record_preview extends record_adapter
|
||||
if (null === $search_engine) {
|
||||
throw new \LogicException('Search Engine should be provided');
|
||||
}
|
||||
if (!$options) {
|
||||
$options = new SearchEngineOptions();
|
||||
}
|
||||
$options->setFirstResult($pos);
|
||||
$options->setMaxResults(1);
|
||||
|
||||
$results = $search_engine->query($query, (int) ($pos), 1, $options);
|
||||
$results = $search_engine->query($query, $options);
|
||||
|
||||
if ($results->getResults()->isEmpty()) {
|
||||
throw new Exception('Record introuvable');
|
||||
@@ -187,9 +192,11 @@ class record_preview extends record_adapter
|
||||
|
||||
switch ($this->env) {
|
||||
case 'RESULT':
|
||||
$perPage = 56;
|
||||
$index = ($this->pos - 3) < 0 ? 0 : ($this->pos - 3);
|
||||
$results = $this->searchEngine->query($this->query, $index, $perPage, $this->options);
|
||||
$options = $this->options ?: new SearchEngineOptions();
|
||||
$options->setFirstResult(($this->pos - 3) < 0 ? 0 : ($this->pos - 3));
|
||||
$options->setMaxResults(56);
|
||||
|
||||
$results = $this->searchEngine->query($this->query, $options);
|
||||
|
||||
$this->train = $results->getResults()->toArray();
|
||||
break;
|
||||
|
@@ -39,7 +39,7 @@ class SearchEngineOptionsTest extends \PhraseanetTestCase
|
||||
$options->setMinDate(\DateTime::createFromFormat(DATE_ATOM, $min_date->format(DATE_ATOM)));
|
||||
$options->setMaxDate(\DateTime::createFromFormat(DATE_ATOM, $max_date->format(DATE_ATOM)));
|
||||
$options->setFirstResult(3);
|
||||
$options->setMaxPerPage(42);
|
||||
$options->setMaxResults(42);
|
||||
|
||||
$serialized = $options->serialize();
|
||||
|
||||
|
Reference in New Issue
Block a user