Remove some arguments from query and add them to options

This commit is contained in:
Benoît Burnichon
2016-04-15 16:34:47 +02:00
parent 581d5115af
commit 033542ac2f
10 changed files with 70 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
<?php <?php
/** /*
* This file is part of Phraseanet * This file is part of Phraseanet
* *
* (c) 2005-2016 Alchemy * (c) 2005-2016 Alchemy
@@ -49,14 +49,13 @@ class SearchController extends Controller
private function searchAndFormatEngineResult(Request $request) private function searchAndFormatEngineResult(Request $request)
{ {
$options = SearchEngineOptions::fromRequest($this->app, $request); $options = SearchEngineOptions::fromRequest($this->app, $request);
$options->setFirstResult($request->get('offset_start') ?: 0);
$offsetStart = (int) ($request->get('offset_start') ?: 0); $options->setMaxResults($request->get('per_page') ?: 10);
$perPage = (int) $request->get('per_page') ?: 10;
$query = (string) $request->get('query'); $query = (string) $request->get('query');
$this->getSearchEngine()->resetCache(); $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()); $this->getUserManipulator()->logQuery($this->getAuthenticatedUser(), $search_result->getQuery());
@@ -74,8 +73,8 @@ class SearchController extends Controller
$this->getSearchEngine()->clearCache(); $this->getSearchEngine()->clearCache();
$ret = [ $ret = [
'offset_start' => $offsetStart, 'offset_start' => $options->getFirstResult(),
'per_page' => $perPage, 'per_page' => $options->getMaxResults(),
'available_results' => $search_result->getAvailable(), 'available_results' => $search_result->getAvailable(),
'total_results' => $search_result->getTotal(), 'total_results' => $search_result->getTotal(),
'error' => (string)$search_result->getError(), 'error' => (string)$search_result->getError(),

View File

@@ -1088,13 +1088,13 @@ class V1Controller extends Controller
{ {
$options = SearchEngineOptions::fromRequest($this->app, $request); $options = SearchEngineOptions::fromRequest($this->app, $request);
$offsetStart = (int) ($request->get('offset_start') ?: 0); $options->setFirstResult((int) ($request->get('offset_start') ?: 0));
$perPage = (int) $request->get('per_page') ?: 10; $options->setMaxResults((int) $request->get('per_page') ?: 10);
$query = (string) $request->get('query'); $query = (string) $request->get('query');
$this->getSearchEngine()->resetCache(); $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()); $this->getUserManipulator()->logQuery($this->getAuthenticatedUser(), $search_result->getQuery());
@@ -1112,8 +1112,8 @@ class V1Controller extends Controller
$this->getSearchEngine()->clearCache(); $this->getSearchEngine()->clearCache();
$ret = [ $ret = [
'offset_start' => $offsetStart, 'offset_start' => $options->getFirstResult(),
'per_page' => $perPage, 'per_page' => $options->getMaxResults(),
'available_results' => $search_result->getAvailable(), 'available_results' => $search_result->getAvailable(),
'total_results' => $search_result->getTotal(), 'total_results' => $search_result->getTotal(),
'error' => (string)$search_result->getError(), 'error' => (string)$search_result->getError(),

View File

@@ -43,8 +43,6 @@ class QueryController extends Controller
$options = SearchEngineOptions::fromRequest($this->app, $request); $options = SearchEngineOptions::fromRequest($this->app, $request);
$form = $options->serialize();
$perPage = (int) $this->getSettings()->getUserSetting($this->getAuthenticatedUser(), 'images_per_page'); $perPage = (int) $this->getSettings()->getUserSetting($this->getAuthenticatedUser(), 'images_per_page');
$page = (int) $request->request->get('pag'); $page = (int) $request->request->get('pag');
@@ -56,13 +54,15 @@ class QueryController extends Controller
$page = 1; $page = 1;
} }
$options->setFirstResult(($page - 1) * $perPage);
$options->setMaxResults($perPage);
$user = $this->getAuthenticatedUser(); $user = $this->getAuthenticatedUser();
$userManipulator = $this->getUserManipulator(); $userManipulator = $this->getUserManipulator();
$userManipulator->logQuery($user, $query); $userManipulator->logQuery($user, $query);
try { try {
/** @var SearchEngineResult $result */ $result = $engine->query($query, $options);
$result = $engine->query($query, (($page - 1) * $perPage), $perPage, $options);
if ($this->getSettings()->getUserSetting($user, 'start_page') === 'LAST_QUERY') { if ($this->getSettings()->getUserSetting($user, 'start_page') === 'LAST_QUERY') {
$userManipulator->setUserSetting($user, 'start_page_query', $query); $userManipulator->setUserSetting($user, 'start_page_query', $query);
@@ -216,7 +216,7 @@ class QueryController extends Controller
$json['total_answers'] = (int) $result->getAvailable(); $json['total_answers'] = (int) $result->getAvailable();
$json['next_page'] = ($page < $npages && $result->getAvailable() > 0) ? ($page + 1) : false; $json['next_page'] = ($page < $npages && $result->getAvailable() > 0) ? ($page + 1) : false;
$json['prev_page'] = ($page > 1 && $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) { catch(\Exception $e) {
// we'd like a message from the parser so get all the exceptions messages // we'd like a message from the parser so get all the exceptions messages

View File

@@ -268,7 +268,7 @@ class ElasticSearchEngine implements SearchEngineInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function query($string, $offset, $perPage, SearchEngineOptions $options = null) public function query($string, SearchEngineOptions $options = null)
{ {
$options = $options ?: new SearchEngineOptions(); $options = $options ?: new SearchEngineOptions();
$context = $this->context_factory->createContext($options); $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) // ask ES to return field _version (incremental version number of document)
$params['body']['version'] = true; $params['body']['version'] = true;
$params['body']['from'] = $offset; $params['body']['from'] = $options->getFirstResult();
$params['body']['size'] = $perPage; $params['body']['size'] = $options->getMaxResults();
if($this->options->getHighlight()) { if($this->options->getHighlight()) {
$params['body']['highlight'] = $this->buildHighlightRules($context); $params['body']['highlight'] = $this->buildHighlightRules($context);
} }
if ($aggs = $this->getAggregationQueryParams($options)) { $aggs = $this->getAggregationQueryParams($options);
if ($aggs) {
$params['body']['aggs'] = $aggs; $params['body']['aggs'] = $aggs;
} }
@@ -314,7 +316,7 @@ class ElasticSearchEngine implements SearchEngineInterface
$results, // ArrayCollection of results $results, // ArrayCollection of results
json_encode($query), json_encode($query),
$res['took'], // duration $res['took'], // duration
$offset, // offset start $options->getFirstResult(),
$res['hits']['total'], // available $res['hits']['total'], // available
$res['hits']['total'], // total $res['hits']['total'], // total
null, // error null, // error

View File

@@ -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(); public function getAvailableTypes();
@@ -162,15 +162,12 @@ interface SearchEngineInterface
public function updateFeedEntry(FeedEntry $entry); public function updateFeedEntry(FeedEntry $entry);
/** /**
*
* @param string $query * @param string $query
* @param integer $offset
* @param integer $perPage
* @param SearchEngineOptions $options * @param SearchEngineOptions $options
* *
* @return SearchEngineResult * @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 * Return an array of suggestions corresponding to the last word of the

View File

@@ -50,7 +50,7 @@ class SearchEngineOptions
'sort_by', 'sort_by',
'sort_ord', 'sort_ord',
'business_fields', 'business_fields',
'max_per_page', 'max_results',
'first_result', 'first_result',
]; ];
@@ -113,7 +113,7 @@ class SearchEngineOptions
$options->allowBusinessFieldsOn($collectionNormalizer($value)); $options->allowBusinessFieldsOn($collectionNormalizer($value));
}, },
'first_result' => 'setFirstResult', 'first_result' => 'setFirstResult',
'max_per_page' => 'setMaxPerPage', 'max_results' => 'setResults',
]; ];
return $methods; return $methods;
@@ -149,7 +149,7 @@ class SearchEngineOptions
/** /**
* @var int * @var int
*/ */
private $max_per_page = 10; private $max_results = 10;
/** /**
* @var int * @var int
@@ -710,16 +710,16 @@ class SearchEngineOptions
return $options; 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;
} }
/** /**

View File

@@ -192,6 +192,21 @@ SQL;
return false; 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 * @param string $label
* @return caption_field|null * @return caption_field|null

View File

@@ -114,6 +114,16 @@ class databox_descriptionStructure implements IteratorAggregate, Countable
: null; : 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 * @param string $label
* @return databox_field|null * @return databox_field|null

View File

@@ -89,8 +89,13 @@ class record_preview extends record_adapter
if (null === $search_engine) { if (null === $search_engine) {
throw new \LogicException('Search Engine should be provided'); 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()) { if ($results->getResults()->isEmpty()) {
throw new Exception('Record introuvable'); throw new Exception('Record introuvable');
@@ -187,9 +192,11 @@ class record_preview extends record_adapter
switch ($this->env) { switch ($this->env) {
case 'RESULT': case 'RESULT':
$perPage = 56; $options = $this->options ?: new SearchEngineOptions();
$index = ($this->pos - 3) < 0 ? 0 : ($this->pos - 3); $options->setFirstResult(($this->pos - 3) < 0 ? 0 : ($this->pos - 3));
$results = $this->searchEngine->query($this->query, $index, $perPage, $this->options); $options->setMaxResults(56);
$results = $this->searchEngine->query($this->query, $options);
$this->train = $results->getResults()->toArray(); $this->train = $results->getResults()->toArray();
break; break;

View File

@@ -39,7 +39,7 @@ class SearchEngineOptionsTest extends \PhraseanetTestCase
$options->setMinDate(\DateTime::createFromFormat(DATE_ATOM, $min_date->format(DATE_ATOM))); $options->setMinDate(\DateTime::createFromFormat(DATE_ATOM, $min_date->format(DATE_ATOM)));
$options->setMaxDate(\DateTime::createFromFormat(DATE_ATOM, $max_date->format(DATE_ATOM))); $options->setMaxDate(\DateTime::createFromFormat(DATE_ATOM, $max_date->format(DATE_ATOM)));
$options->setFirstResult(3); $options->setFirstResult(3);
$options->setMaxPerPage(42); $options->setMaxResults(42);
$serialized = $options->serialize(); $serialized = $options->serialize();