# HG changeset patch # User ymh # Date 1476885304 -7200 # Node ID d7c5b43d309a4324b01f3ddcd32c914f6001c7fc # Parent 796725d33b67a9780bb912a5d5d6a16d9ee4bfc8 add theme/subject filter to back documents api end point diff -r 796725d33b67 -r d7c5b43d309a server/src/app/Http/Controllers/Api/DocumentController.php --- a/server/src/app/Http/Controllers/Api/DocumentController.php Wed Oct 19 13:08:02 2016 +0200 +++ b/server/src/app/Http/Controllers/Api/DocumentController.php Wed Oct 19 15:55:04 2016 +0200 @@ -40,6 +40,10 @@ if(!empty($location)) { $filters['location'] = $location; } + $themes = CorpusFilterManager::prepareTheme($request->input('theme', [])); + if(!empty($themes)) { + $filters['themes'] = $themes; + } $sort = $request->input('sort', null); diff -r 796725d33b67 -r d7c5b43d309a server/src/app/Libraries/Filters/CorpusFilterManager.php --- a/server/src/app/Libraries/Filters/CorpusFilterManager.php Wed Oct 19 13:08:02 2016 +0200 +++ b/server/src/app/Libraries/Filters/CorpusFilterManager.php Wed Oct 19 15:55:04 2016 +0200 @@ -101,4 +101,41 @@ ]; } + public static function prepareTheme($entities) { + if(empty($entities)) { + return []; + } + if(is_string($entities)) { + $entities = [$entities,]; + } + return array_reduce($entities, function($res, $e) { + if(preg_match(config('corpusparole.bnf_ark_url_regexp'), $e, $m)) { + array_push($res, $m[1]); + } elseif(Utils::startsWith($e, config('corpusparole.bnf_ark_id_base'))) { + array_push($res, $e); + } + + return $res; + }, []); + } + + public static function getThemeFilterPart($themes) { + $nestedQueries = []; + foreach($themes as $t) { + $nestedQueries[] = [ + 'nested' => [ + 'path' => "subject", + 'query' => [ 'term' => ['subject.code' => "$t" ] ] + ] + ]; + } + return [ + 'query' => [ + 'bool' => [ + 'must' => $nestedQueries + ] + ] + ]; + } + } diff -r 796725d33b67 -r d7c5b43d309a server/src/app/Repositories/RdfDocumentRepository.php --- a/server/src/app/Repositories/RdfDocumentRepository.php Wed Oct 19 13:08:02 2016 +0200 +++ b/server/src/app/Repositories/RdfDocumentRepository.php Wed Oct 19 15:55:04 2016 +0200 @@ -159,9 +159,15 @@ if(is_array($location)) { $location = $location[0]; // we know it is not empty } - $location = $filters['location']; $qFilterParts[] = CorpusFilterManager::getLocationFilterPart($location); } + if(array_key_exists('themes', $filters) && !empty($filters['themes'])) { + $themes = $filters['themes']; + if(is_string($themes)) { + $themes = [$themes,]; // we know it is not empty + } + $qFilterParts[] = CorpusFilterManager::getThemeFilterPart($themes); + } $query = [ 'index' => config('corpusparole.elasticsearch_index'), diff -r 796725d33b67 -r d7c5b43d309a server/src/config/corpusparole.php --- a/server/src/config/corpusparole.php Wed Oct 19 13:08:02 2016 +0200 +++ b/server/src/config/corpusparole.php Wed Oct 19 15:55:04 2016 +0200 @@ -132,6 +132,7 @@ 'bnf_base_url' => 'http://data.bnf.fr/', 'bnf_ark_base_url' => 'http://ark.bnf.fr/', 'bnf_ark_url_regexp' => '/http[s]?\:\/\/(?:data|ark)\.bnf\.fr\/(ark\:\/12148\/[[:alnum:]]+)\/?/', + 'bnf_ark_id_base' => "ark:/12148/", 'bnf_cache_expiration' => 60*24*30, 'bnf_max_ids' => 5, 'bnf_query_url' => 'http://data.bnf.fr/sparql', diff -r 796725d33b67 -r d7c5b43d309a server/src/tests/Libraries/Filters/CorpusFilterManagerTest.php --- a/server/src/tests/Libraries/Filters/CorpusFilterManagerTest.php Wed Oct 19 13:08:02 2016 +0200 +++ b/server/src/tests/Libraries/Filters/CorpusFilterManagerTest.php Wed Oct 19 15:55:04 2016 +0200 @@ -142,4 +142,56 @@ $this->assertEquals('3030293', $locationOutput); } + + /** + * test prepare themes noop + * + * @return void + */ + public function testPrepareThemesNoOp() + { + $themesInput = ['ark:/12148/cb11937931x', 'ark:/12148/cb11946662b', 'ark:/12148/cb13318415c']; + $themesOutput = CorpusFilterManager::prepareTheme($themesInput); + $this->assertEquals($themesOutput, $themesInput); + } + + /** + * test prepare themes full url + * + * @return void + */ + public function testPrepareThemesFullUrl() + { + $themesInput = ['http://ark.bnf.fr/ark:/12148/cb11937931x', 'http://data.bnf.fr/ark:/12148/cb11946662b', 'https://ark.bnf.fr/ark:/12148/cb13318415c']; + $themesOutput = CorpusFilterManager::prepareTheme($themesInput); + $this->assertEquals($themesOutput, ['ark:/12148/cb11937931x', 'ark:/12148/cb11946662b', 'ark:/12148/cb13318415c']); + } + + + /** + * test prepare themes full url + * + * @return void + */ + public function testPrepareThemesUnknown() + { + $themesInput = ['ark:/12148/cb11937931x', 'foo', 'ark:/12148/cb11946662b', 'ark:/12148/cb13318415c', 'bar']; + $themesOutput = CorpusFilterManager::prepareTheme($themesInput); + $this->assertEquals($themesOutput, ['ark:/12148/cb11937931x', 'ark:/12148/cb11946662b', 'ark:/12148/cb13318415c']); + } + + /** + * test prepare themes mix + * + * @return void + */ + public function testPrepareThemesMix() + { + $themesInput = ['ark:/12148/cb11937931x', 'foo', 'http://data.bnf.fr/ark:/12148/cb11946662b', 'ark:/12148/cb13318415c', 'bar']; + $themesOutput = CorpusFilterManager::prepareTheme($themesInput); + $this->assertEquals($themesOutput, ['ark:/12148/cb11937931x', 'ark:/12148/cb11946662b', 'ark:/12148/cb13318415c']); + } + + + }