def add_tag(): """ Add tag from a given document. This function uses Elasticsearch's scripting language.""" index = request.form.get('index','projects') doc_id = request.form.get('doc_id') tag_added = request.form.get('tag') # construct script based on if tag_added is topic or element if tag_added in topics: script = {"script": f"if (ctx._source.tags != null) {{ctx._source.tags.add('{tag_added}')}} else {{ ctx._source.tags = ['{tag_added}'] }}"} elif tag_added in element_tags: script = {"script": f"if (ctx._source.element_tags != null) {{ctx._source.element_tags.add('{tag_added}')}} else {{ ctx._source.element_tags = ['{tag_added}'] }}"} # use the update API to run Elasticsearch script that removes the tag client.update(index=index, doc_type='doc', id=doc_id, body=script, refresh=True) # construct script to add tag to added_tags field if it does not already exist or append tag_added script = { "script" : f"if (ctx._source.containsKey('added_tags')) {{ctx._source.added_tags.add('{tag_added}');}} else {{ctx._source.added_tags = ['{tag_added}']}}" } # add tag to "added_tags" field client.update(index=index, doc_type='doc', id=doc_id, body=script, refresh=True) return 'doc updated'
def bookmark(): index = request.form.get('index','projects') doc_id = request.form.get('doc_id') marked = request.form.get('marked') # update objectives and notes field for doc in database client.update(index=index, doc_type='doc', id=doc_id, body={"doc": {"bookmarked":marked}}) return 'doc updated'
def update_database(): now = datetime.datetime.now() year, month, day = now.year, now.month, now.day # create temporary directory for downloading files CWD = os.getcwd() TMP_DIRECTORY = os.path.join(CWD, r'.tmp') DOWNLOADS_FOLDER = os.path.join(TMP_DIRECTORY, f"{year:04}{month:02}{day:02}") XML_PATH = os.path.join(DOWNLOADS_FOLDER, "xml") JSON_PATH = os.path.join(DOWNLOADS_FOLDER, "json") PROJECT_FILES_PATH = os.path.join(JSON_PATH, "projects") PUB_FILES_PATH = os.path.join(JSON_PATH, "publications") try: shutil.rmtree(TMP_DIRECTORY) except: pass os.makedirs(DOWNLOADS_FOLDER) os.makedirs(XML_PATH) os.makedirs(JSON_PATH) os.makedirs(PROJECT_FILES_PATH) os.makedirs(PUB_FILES_PATH) # scrape TRID site webscrapper.scrape_trid(TMP_DIRECTORY, DOWNLOADS_FOLDER) # index documents index.index_documents("projects", PROJECT_FILES_PATH) index.tag_documents("projects", topics, element_tags) index.index_documents("publications", PUB_FILES_PATH) index.tag_documents("publications", topics, element_tags) # delete temporary downloads directory shutil.rmtree(TMP_DIRECTORY) # update appData index today = str(datetime.date.today()) client.update(index='appdata', doc_type='doc', id=1, body={'doc': { 'last_update': today }}) return "database updated"
def annotate(): if request.method == 'POST': # update record from form submission search_type = request.form.get('type', 'search') search_query = request.form.get('query') index = request.form.get('index', 'projects') filter_topic = request.form.get('topic', 'all') filter_element = request.form.get('element', 'all') filter_status = request.form.get('status', 'all') date_range = request.form.get('dateRange', '50') sort_by = request.form.get('sortBy', 'date') doc_type = index[:-1] objectives = [] notes = None for key in request.form: if key in {'index', 'doc_id'}: continue if 'objective' in key: # store objectives objectives.append(key) if 'notes' in key and request.form.get(key): # store notes notes = request.form.get(key) # notes = notes.strip() doc_id = request.form.get('doc_id') # update objectives and notes field for doc in database client.update(index=index, doc_type='doc', id=doc_id, body={"doc": { "objectives": objectives, "notes": notes }}) args = dict(type=search_type, query=search_query, index=index, topic=filter_topic, element=filter_element, status=filter_status, dateRange=date_range, sortBy=sort_by) return 'form submitted'