server/src/app/Console/Commands/IndexDocuments.php
changeset 497 f3474aeec884
parent 496 a53762d61c06
child 498 265992e5b379
--- a/server/src/app/Console/Commands/IndexDocuments.php	Fri Feb 03 16:35:09 2017 +0100
+++ b/server/src/app/Console/Commands/IndexDocuments.php	Mon Feb 06 10:03:33 2017 +0100
@@ -105,6 +105,7 @@
                         ],
                         'date' => [ 'type' => 'date', 'index' => 'not_analyzed'],
                         'geonames_hierarchy' => [ 'type' => 'string', 'index' => 'not_analyzed'],
+                        'geonames_country' => ['type' => 'string', 'index' => 'not_analyzed'],
                         'location' => [ 'type' => 'geo_point'],
                         'creation_date' => ['type' => 'date', 'index' => 'not_analyzed'],
                         'language' => ['type' => 'string', 'index' => 'not_analyzed'],
@@ -160,12 +161,16 @@
         }
 
         $res = [];
+        $resCountry = null;
         foreach($hcache->hierarchy['geonames'] as $hierarchyElem) {
             if(in_array($hierarchyElem['fcode'], ['CONT','PCLI', 'PCL','PCLD', 'PCLF', 'PCLH', 'PCLIX', 'PCLIS', 'ADM1'])) {
                 array_push($res, $hierarchyElem['geonameId']);
             }
+            if(!empty($hierarchyElem['fcode']) && strpos($hierarchyElem['fcode'], 'PCL') === 0) {
+                $resCountry = $hierarchyElem['geonameId'];
+            }
         }
-        return $res;
+        return [$resCountry, $res];
 
     }
 
@@ -176,19 +181,24 @@
     private function getGeonamesHierarchy($doc) {
         $geoRes = $doc->getGeoInfo();
         if(is_null($geoRes)) {
-            return [];
+            return [null,[]];
         }
         // aggregate hierachy list from geonames results
         $res = [];
+        // The country is the first one
+        $resCountry = null;
         foreach($geoRes->getGeonamesLocs() as $gurl) {
             $geonamesId = CocoonUtils::getGeonamesidFromUrl($gurl);
             if(is_null($geonamesId)) {
                 continue;
             }
-            $hierarchyIds = $this->getGeonamesHierarchyArray($geonamesId);
+            list($country, $hierarchyIds) = $this->getGeonamesHierarchyArray($geonamesId);
             $res = array_unique(array_merge($res, $hierarchyIds));
+            if(is_null($resCountry) && !empty($country)) {
+                $resCountry = $country;
+            }
         }
-        return $res;
+        return [$resCountry, $res];
 
     }
 
@@ -515,6 +525,7 @@
     }
 
     private function getDocBody($doc) {
+        list($geonamesCountry, $geonamesHierarchy) = $this->getGeonamesHierarchy($doc);
         return [
             'title' => (string)$doc->getTitle(),
             'date' => (string)$doc->getModified(),
@@ -523,7 +534,8 @@
             'creation_years' => $this->getCreationYears($doc),
             'language' => $doc->getLanguagesValue(),
             'discourse_types' => $this->getDiscourseTypes($doc),
-            'geonames_hierarchy' => $this->getGeonamesHierarchy($doc),
+            'geonames_country' => $geonamesCountry,
+            'geonames_hierarchy' => $geonamesHierarchy,
             'subject' => $this->getSubjects($doc),
         ];
     }