-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.py
222 lines (188 loc) · 7.54 KB
/
mongo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import configparser
from pymongo import MongoClient, errors
from datetime import datetime
PRINT_DUPLICATE_ERRORS = 0
SERVER_TIMEOUT_MS = 5000
client = None
db = None
collection = None
AVAILABLE_LANGUAGES = {
'Bulgarian': 'bg',
'Croatian': 'hr',
'Czech': 'cs',
'Danish': 'da',
'Dutch': 'nl',
'English': 'en',
'Estonian': 'et',
'Finnish': 'fi',
'French': 'fr',
'German': 'de',
'Greek': 'el',
'Hungarian': 'hu',
'Irish': 'ga',
'Italian': 'it',
'Latvian': 'lv',
'Lithuanian': 'lt',
'Maltese': 'mt',
'Polish': 'pl',
'Portuguese': 'pt',
'Romanian': 'ro',
'Slovak': 'sk',
'Slovenian': 'sl',
'Spanish': 'es',
'Swedish': 'sv'
}
def init_client():
config = configparser.ConfigParser()
config.read("config.ini")
global client, db, collection
client = MongoClient(config.get("mongo_db", "host"), config.getint(
"mongo_db", "port"), serverSelectionTimeoutMS=SERVER_TIMEOUT_MS)
db = client.judgment_corpus
collection = client.judgement_corpus.judgments_en # english as default language
def close_client():
client.close()
# accepts yyyy-mm-dd string and returns a datetime object for mongoDB
def __get_datetime_from_string(ymd_string):
split = ymd_string.split('-')
date = datetime(int(split[0]), int(split[1]), int(split[2]))
return date
def change_cur_coll(language):
if language in AVAILABLE_LANGUAGES.values():
_collection = db["judgments_{}".format(language)]
return _collection
def init_db(used_languages=['en']):
global db, collection
collist = db.list_collection_names()
for lang in used_languages:
if lang not in AVAILABLE_LANGUAGES.values():
continue
coll_name = "judgments_" + lang
if coll_name in collist:
print("DB: collection already created for:'{}'".format(lang))
else:
db.create_collection(coll_name)
db[coll_name].create_index([('reference', -1)], unique=True)
db[coll_name].create_index([('celex', -1)])
db[coll_name].create_index([('ecli', -1)])
# possibility that two collections are created at once
# update collist repeatedly to ensure no crashes due to "collection has already been created"
collist = db.list_collection_names()
def db_is_running():
try:
init_client()
client.server_info()
client.close()
except:
print('Error connecting to MongoDB instance. Ensure MongoDB is running.')
return False
else:
return True
def insert_doc(doc, language):
global collection
if language in AVAILABLE_LANGUAGES.values():
collection = change_cur_coll(language)
try:
collection.insert_one(doc)
except errors.DuplicateKeyError as e:
if PRINT_DUPLICATE_ERRORS:
print("document exists already")
print(e)
else:
print("DB: insert_doc(): unsuported language")
def get_all_docs(language):
global collection
collection = change_cur_coll(language)
cursor = collection.find({})
return cursor
# retrieves documents between two dates (dates must be in the format: y-m-dT00:00:00.000+00:00)
def get_docs_between_dates(start, end, language):
global collection
collection = change_cur_coll(language)
start_date = __get_datetime_from_string(start)
end_date = __get_datetime_from_string(end)
cursor = collection.find({'date': {'$lt': end_date, '$gte': start_date}})
return cursor
def get_docs_by_object_key(column, values, language):
global collection
# retrieves documents by searching a object column for a specified value
collection = change_cur_coll(language)
cursor = collection.find({column + "." + "ids": {"$in": values}})
return cursor
def get_docs_by_object_value(column, values, language):
global collection
# retrieves documents by searching a object column for a specified value
collection = change_cur_coll(language)
cursor = collection.find({column + "." + "labels": {"$in": values}})
return cursor
def get_docs_search_string(column, search, language):
global collection
# retrieves documents by searching a string column with specified words (words separated by whitespace)
collection = change_cur_coll(language)
search_words = search.split(" ")
search_string = ""
for word in search_words:
regex_c_group = "(.*" + word + ".*)"
search_string += regex_c_group
cursor = collection.find({column: {"$regex": search_string, "$options": "i"}})
return cursor
def get_docs_by_value(column, value, language):
global collection
collection = change_cur_coll(language)
cursor = collection.find({column: value})
return cursor
def get_docs_by_custom_query(query_args, language):
global collection
search_dict = {}
keys_containing_dicts = ["author", "subject_matter", "case_law_directory",
"applicant", "defendant", "procedure_type"]
collection = change_cur_coll(language)
# encapsule in list if only one column is specified.
if not isinstance(query_args, list):
query_args = [query_args]
for item in query_args:
if item.get('column') in keys_containing_dicts:
if item.get('search identifier'):
if isinstance(item.get('value'), list):
if item.get('operator') == 'NOT':
search_dict[item.get('column')+".ids"] = {"$nin": item.get('value')}
else:
search_dict[item.get('column')+".ids"] = {"$in": item.get('value')}
else:
if item.get('operator') == 'NOT':
search_dict[item.get('column')+".ids"] = {"$ne": item.get('value')}
else:
search_dict[item.get('column')+'.ids'] = item.get('value')
else:
if isinstance(item.get('value'), list):
if item.get('operator') == 'NOT':
search_dict[item.get('column')+".labels"] = {"$nin": item.get('value')}
else:
search_dict[item.get('column')+".labels"] = {"$in": item.get('value')}
else:
if item.get('operator') == 'NOT':
search_dict[item.get('column')+".labels"] = {"$ne": item.get('value')}
else:
search_dict[item.get('column')+".labels"] = item.get('value')
elif item.get('column') == 'date':
start_date = __get_datetime_from_string(item.get('start date'))
end_date = __get_datetime_from_string(item.get('end date'))
if start_date and end_date:
search_dict['date'] = {'$lt': end_date, '$gte': start_date}
elif start_date:
search_dict['date'] = {'$gte': start_date}
elif end_date:
search_dict['date'] = {'$lt': end_date}
else:
if isinstance(item.get('value'), list):
if item.get('operator') == 'NOT':
search_dict[item.get('column')] = {"$nin": item.get('value')}
else:
search_dict[item.get('column')] = {"$in": item.get('value')}
else:
if item.get('operator') == 'NOT':
search_dict[item.get('column')] = {"$ne": item.get('value')}
else:
search_dict[item.get('column')] = item.get('value')
cursor = collection.find(search_dict)
return cursor