Skip to content

Commit dc76673

Browse files
author
jgaff
committed
Forge updates and improvements
Simplify search method. All term- or field-based searches are limited to 10k results, because there’s no way to guarantee complete results otherwise. Add aggregate_source, which aggregates all records from a given source_name. This is the only method that scrolls through results, because this is the only search that can guarantee correct results with scrolling. Remove `raw` flag from searches. All results will now return the gmeta_pop of the GlobusHTTPResponse. Also revert Search scope workaround.
1 parent 8ceb8ed commit dc76673

2 files changed

Lines changed: 39 additions & 54 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
*.pyc
22
*.swp
33
*.swo
4-
*_json.pickle
54
*login*
65
*.ipynb_checkpoints
76
Untitled*.ipynb
8-
*.mdf_paths.json
97

10-
feedstock/*
11-
feedstock
12-
datasets/*
13-
datasets
8+
*.cache*
149

1510
*/build/*
1611
*/dist/*

‎forge/mdf_forge/forge.py‎

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Forge:
1313
index = "mdf"
14-
services = ["mdf", "transfer", "search_ingest"]
14+
services = ["mdf", "transfer", "search"]
1515
app_name = "MDF Forge"
1616

1717
def __init__(self, data={}):
@@ -44,12 +44,16 @@ def match_elements(self, elements, match_all=True):
4444
return Query(self.search_client).match_elements(elements=elements, match_all=match_all)
4545

4646

47-
def search(self, q, raw=False, advanced=False, limit=None):
48-
return Query(self.search_client).search(q=q, raw=raw, advanced=advanced, limit=limit)
47+
def search(self, q, advanced=False, limit=None):
48+
return Query(self.search_client).search(q=q, advanced=advanced, limit=limit)
4949

5050

51-
def search_by_elements(self, elements=[], sources=[], limit=None, match_all=False, raw=False):
52-
return Query(self.search_client).match_elements(elements, match_all=match_all).match_sources(sources, match_all=match_all).search(limit=limit, raw=raw)
51+
def search_by_elements(self, elements=[], sources=[], limit=None, match_all=False):
52+
return Query(self.search_client).match_elements(elements, match_all=match_all).match_sources(sources, match_all=match_all).search(limit=limit)
53+
54+
55+
def aggregate_source(self, source, limit=None):
56+
return Query(self.search_client).aggregate_source(source=source, limit=limit)
5357

5458

5559
def http_download(self, results, dest=".", preserve_dir=False, verbose=True):
@@ -198,11 +202,10 @@ def http_return(self, results, verbose=True):
198202

199203

200204
class Query:
201-
def __init__(self, search_client, q="", limit=None, raw=False, advanced=False):
205+
def __init__(self, search_client, q="", limit=None, advanced=False):
202206
self.search_client = search_client
203207
self.query = q
204208
self.limit = limit
205-
self.raw = raw
206209
self.advanced = False
207210

208211

@@ -240,18 +243,16 @@ def match_elements(self, elements, match_all=True):
240243
return self
241244

242245

243-
def search(self, q=None, raw=None, advanced=None, limit=None):
246+
def search(self, q=None, advanced=None, limit=None):
244247
if q is None:
245248
q = self.query
246249
if not q:
247250
print("Error: No query specified")
248251
return []
249-
if raw is None:
250-
raw = self.raw
251252
if advanced is None or self.advanced:
252253
advanced = self.advanced
253254
if limit is None:
254-
limit = self.limit
255+
limit = self.limit or 10000
255256

256257
# Clean query string
257258
q = q.strip()
@@ -264,46 +265,35 @@ def search(self, q=None, raw=None, advanced=None, limit=None):
264265
q = q.strip()
265266

266267

267-
full_res = []
268268
# Simple query (max 10k results)
269-
if not advanced:
270-
if not limit or limit > 10000:
271-
limit = 10000
269+
query = {
270+
"q": q,
271+
"advanced": advanced,
272+
"limit": limit
273+
}
274+
return toolbox.gmeta_pop(self.search_client.structured_search(query))
275+
276+
277+
def execute(self, q=None, advanced=None, limit=None):
278+
return self.search(q, advanced, limit)
279+
280+
281+
def aggregate_source(self, source, limit=None):
282+
full_res = []
283+
res = True # Start res as value that will pass while condition
284+
# Scroll while results are being returned and limit not reached
285+
while res and (limit is None or limit > 0):
272286
query = {
273-
"q": q,
274-
"advanced": False,
275-
"limit": limit
287+
"q": "mdf.source_name:" + source + " AND mdf.scroll_id:(>" + str(len(full_res)) + " AND <" + str( len(full_res) + (limit or 10000) ) + ")",
288+
"advanced": True,
289+
"limit": limit or 10000
276290
}
277-
res = self.search_client.structured_search(query)
278-
full_res += (res if raw else toolbox.gmeta_pop(res))
279-
# Advanced query
280-
else:
281-
# If there is no limit, iterate "forever"
282-
# If there is a limit, iterate until that many records are returned
283-
while limit is None or limit > 0:
284-
# Perform search
285-
query = {
286-
"q": q + " AND mdf.scroll_id:(>" + str(len(full_res)) + " AND <=" + str(len(full_res) + min(limit or 10000, 10000)) + ")",
287-
"advanced": True,
288-
"limit": 10000
289-
}
290-
res = self.search_client.structured_search(query)
291-
num_res = len(toolbox.gmeta_pop(res))
292-
# If results were returned, add to full_res
293-
if num_res > 0:
294-
full_res += (res if raw else toolbox.gmeta_pop(res))
295-
# If a limit was set, lower future limit by number of results saved
296-
if limit:
297-
limit -= len(toolbox.gmeta_pop(res))
298-
# If partial results were returned, either limit < 10k or the end of the results was reached
299-
if num_res < 10000:
300-
break
301-
# If no results were returned, the end of results was reached
302-
else:
303-
break
291+
res = toolbox.gmeta_pop(self.search_client.structured_search(query))
292+
num_res = len(res)
293+
full_res += res
294+
# If a limit was set, lower future limit by number of results saved
295+
if limit:
296+
limit -= num_res
304297
return full_res
305298

306-
307-
def execute(self, q=None, raw=None, advanced=None, limit=None):
308-
return self.search(q, raw, advanced, limit)
309299

0 commit comments

Comments
 (0)