add theme/subject filter to back documents api end point
authorymh <ymh.work@gmail.com>
Wed, 19 Oct 2016 15:55:04 +0200
changeset 370 d7c5b43d309a
parent 369 796725d33b67
child 371 0365fd9977a4
add theme/subject filter to back documents api end point
server/src/app/Http/Controllers/Api/DocumentController.php
server/src/app/Libraries/Filters/CorpusFilterManager.php
server/src/app/Repositories/RdfDocumentRepository.php
server/src/config/corpusparole.php
server/src/tests/Libraries/Filters/CorpusFilterManagerTest.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);
 
--- 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
+                ]
+            ]
+        ];
+    }
+
 }
--- 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'),
--- 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',
--- 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']);
+    }
+
+
+
 }