Browse Source

Merge branch 'master' of https://intra.acdp.at/gogs/tanja/cdplib

ogert 4 years ago
parent
commit
2328076f8b
2 changed files with 17 additions and 7 deletions
  1. 11 5
      cdplib/db_handlers/MongodbHandler.py
  2. 6 2
      cdplib/db_migration/MigrationCleaning.py

+ 11 - 5
cdplib/db_handlers/MongodbHandler.py

@@ -121,7 +121,7 @@ class MongodbHandler:
         except Exception as error:
             self._log.log_and_raise_error(('Couldnt drop the collection {}. Error: {}').format(collection_name, error))
     
-    def create_index(self, collection_name: str, key: str, direction: (str, int)='text'):
+    def create_index(self, collection_name: str, key: (str, int, list), direction: (str, int)='text'):
         '''
         :param str collection_name: name on the collection for which the schema will be set.
         :param str key: Which value should be used as the index.
@@ -132,14 +132,21 @@ class MongodbHandler:
 
         assert(isinstance(collection_name, str)),\
             "Parameter 'collection_name' must be a string type"
-        assert(isinstance(key, (str, int))),\
-            "Parameter 'key' must be a string or integer type"
+        assert(isinstance(key, (str, int, list))),\
+            "Parameter 'key' must be a string, integer or list type"
         assert(direction in allowed_directions),\
             "Parameter 'key' must be one of these values: 1, -1, '2d', 'geoHaystack', '2dsphere', 'hashed', 'text' "
         assert(isinstance(direction, str)),\
             "Parameter 'direction' must be a string type"
+        if type(key) == list:
             
-        self._database[collection_name].create_index([(key, direction)], name=key)
+            key_list=[]
+            for item in key:
+                key_list.append((item,direction))
+            
+            self._database[collection_name].create_index(key_list,name='_'.join(key))
+        else:
+            self._database[collection_name].create_index([(key, direction)], name=key)
         
 
     def set_collection_schema(self, collection_name: str, schema_path: str,
@@ -228,7 +235,6 @@ class MongodbHandler:
                     data = data.iloc[0]
                 elif type(data) is list:
                     data = data[0]
-
                 self._database[collection_name].insert_one(data)
             else:
                 self._database[collection_name].insert_many(data, ordered=ordered)

+ 6 - 2
cdplib/db_migration/MigrationCleaning.py

@@ -348,14 +348,18 @@ class MigrationCleaning:
                             formats=self._date_formats[column])
 
                 elif (python_type == int) and data[column].isnull().any():
-
+                    
                     self.log.log_and_raise_error(("Column {} contains missing values "
                                         "and cannot be of integer type"
                                         .format(column)))
 
                 elif python_type == str:
-
+                     
+                    # might not be the smoothes solution but it works
+                    python_type = str
+                    data[column] = data[column].astype(python_type)
                     python_type = object
+                    data[column] = data[column].astype(python_type)
 
                 else: