diff --git a/Lib/Search/BookSearch.php b/Lib/Search/BookSearch.php index e173431..55b5756 100644 --- a/Lib/Search/BookSearch.php +++ b/Lib/Search/BookSearch.php @@ -35,6 +35,15 @@ class BookSearch $this->query->addParam('q.op', 'AND'); } + public function addOrQuery(array $texts, $field) + { + if(count($texts) > 0) { + $texts = array_map(array('SolrUtils', 'escapeQueryChars'), $texts); + $query = sprintf('%s:(%s)', $field, implode(' OR ', $texts)); + $this->addQuery($query, null, false); + } + } + public function addQuery($queryText, $queryField = null, $escape = true) { if($escape) { @@ -115,8 +124,7 @@ class BookSearch } $bs = new static(); - $query = sprintf('%s:(%s)', $field, implode(' OR ', $codes)); - $bs->addQuery($query, null, false); + $bs->addOrQuery($codes, $field); $results = $bs->getResults(0, $count); return $results['books']; } diff --git a/NetBiblio.php b/NetBiblio.php index f93af8d..3070350 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -540,7 +540,23 @@ class NetBiblio extends WebService * This method is used by the website and the Android application to perform * book search. * - * TODO: describe various options for the parameters + * Search parameters : + * + * ° queryText : the text to search for + * ° queryType : the field to search in, defaults to 'text' + * + * ° genre : array of 'genreCode' to search for + * ° jeunesse : only display books for kids (must have format 'jeunesse' => array('filtrer' => 'filtrer') + * ° producerCode : filter by 'producerCode' + * ° reader : filter by 'reader' + * + * ° count : number of results we want + * ° page : page to start at (0 is the first) + * + * Deprecated, but still in use on mobile apps : + * + * ° category : synonym for 'genre' (see above) + * ° producer : synonym for 'producerCode' (see above) * * @param string $values JSON encoded object * @return array @@ -555,10 +571,16 @@ class NetBiblio extends WebService throw new WebException("CallArg", "Argument must be valid JSON.", -42); } - // The iOS and Android applications still uses 'category' instead of 'genre' - if(isset($queryArray['category']) && is_array($queryArray['category'])) { - $queryArray['genre'] = $queryArray['category']; - unset($queryArray['category']); + // The iOS and Android applications still uses 'category' and 'producer' + $compatibility = array( + 'category' => 'genre', + 'producer' => 'producerCode' + ); + foreach($compatibility as $old => $new) { + if(isset($queryArray[$old])) { + $queryArray[$new] = $queryArray[$old]; + unset($queryArray[$old]); + } } $bs = new BookSearch(); @@ -589,21 +611,18 @@ class NetBiblio extends WebService $selectedGenres = array_filter($queryArray['genre'], function ($c) { return $c != '0'; }); - if (count($selectedGenres) > 0) { - $selectedGenres = array_map(function ($c) { - return 'genreCode:'.\SolrUtils::escapeQueryChars($c); - }, $selectedGenres); - $bs->addQuery('('.implode(' OR ', $selectedGenres).')', null, false); - } + $bs->addOrQuery($selectedGenres, 'genreCode'); } if(isset($queryArray['jeunesse']) && $queryArray['jeunesse']['filtrer'] === 'filtrer') { $bs->addQuery(1, 'jeunesse'); } - // The following query filter is used by the mobile applications - if(isset($queryArray['producer']) && strlen($queryArray['producer']) > 0) { - $bs->addQuery($queryArray['producer'], 'producerCode'); + $queries = array('producerCode', 'reader'); + foreach($queries as $q) { + if(isset($queryArray[$q]) && strlen($queryArray[$q]) > 0) { + $bs->addQuery($queryArray[$q], $q); + } } $count = isset($queryArray['count']) ? (int) $queryArray['count'] : Configuration::get('solr.result_count');