from elasticsearch import Elasticsearch from datetime import datetime # uni-server es = Elasticsearch({'host':'inse.nes.uni-due.de', 'port': 443, 'path_prefix':'elastic', 'scheme':'https'}, basic_auth=('elastic', 'RLldUaJgbGeoqd3KCX3p'), request_timeout=60, max_retries=5, retry_on_timeout=True) index_name = "ude_colabs" def check_elastic(): if es.indices.exists(index=index_name): es.indices.delete(index=index_name) if not es.indices.exists(index=index_name): settings = { "analysis": { "analyzer": { "searchabilityAnalyzer": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "asciifolding" ] } } } } mappings = { "properties": { "year": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "lsf_id": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "name": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "firstname": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "surname": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "title": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_university": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_faculty": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_institute": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_chair": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "colabs": { "type": "nested", "properties": { "count": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "lsf_id": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "name": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "firstname": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "surname": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "title": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_university": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_faculty": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_institute": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } }, "affiliation_chair": { "type": "text", "analyzer": "searchabilityAnalyzer", "search_analyzer": "searchabilityAnalyzer", "fields": { "keyword": { "type": "keyword", "ignore_above": 250 } } } } } } } es.indices.create(index=index_name, mappings=mappings, settings=settings) if __name__ == '__main__': print("Starting") check_elastic() current_year = int(datetime.now().year) # iterate over all years for year in range(2000, current_year + 1): print("# year: " + str(year)) try: # get all ude-authors for the year query = { "match": { "year": year } } aggs = { "lsf_ids": { "nested": { "path": "authors" }, "aggs": { "lsf_ids": { "terms": { "field": "authors.lsf_id.keyword", "size": 100000 } } } } } docs = es.search(index="due_index_1.0", query=query, aggs=aggs, size=0) docs = docs.get("aggregations").get("lsf_ids").get("lsf_ids").get("buckets") for doc in docs: # for every author of the year get his informationes lsf_ID = doc.get("key") if lsf_ID == "": continue try: # get his profil query = { "match": { "lsf_id": lsf_ID } } profil = es.search(index="ude_persons_1.0", query=query, size=1) profil = profil.get("hits").get("hits") if len(profil) == 0: continue profil = profil[0].get("_source") name = profil.get("name") firstname = profil.get("firstname") surname = profil.get("surname") title = profil.get("title") affiliation_university = profil.get("affiliation_university") affiliation_faculty = profil.get("affiliation_faculty") affiliation_institute = profil.get("affiliation_institute") affiliation_chair = profil.get("affiliation_chair") # get the first level colaborations for the current author in the current year colabs = [] try: query = { "bool": { "must": [ { "match": { "year": year } }, { "nested": { "path": "authors", "query": { "query_string": { "default_field": "authors.lsf_id", "query": lsf_ID } } } } ] } } inner_docs = es.search(index="due_index_1.0", query=query, aggs=aggs, size=0) inner_docs = inner_docs.get("aggregations").get("lsf_ids").get("lsf_ids").get("buckets") for inner_doc in inner_docs: # for every author of the year get his informationes inner_lsf_ID = inner_doc.get("key") inner_count = inner_doc.get("doc_count") if inner_lsf_ID == "" or inner_lsf_ID == lsf_ID: continue try: query = { "match": { "lsf_id": inner_lsf_ID } } inner_profil = es.search(index="ude_persons_1.0", query=query, size=1) inner_profil = inner_profil.get("hits").get("hits") if len(inner_profil) == 0: continue inner_profil = inner_profil[0].get("_source") inner_name = inner_profil.get("name") inner_firstname = inner_profil.get("firstname") inner_surname = inner_profil.get("surname") inner_title = inner_profil.get("title") inner_affiliation_university = inner_profil.get("affiliation_university") inner_affiliation_faculty = inner_profil.get("affiliation_faculty") inner_affiliation_institute = inner_profil.get("affiliation_institute") inner_affiliation_chair = inner_profil.get("affiliation_chair") current_colab = {} current_colab["lsf_id"] = inner_lsf_ID current_colab["name"] = inner_name current_colab["firstname"] = inner_firstname current_colab["surname"] = inner_surname current_colab["title"] = inner_title current_colab["affiliation_university"] = inner_affiliation_university current_colab["affiliation_faculty"] = inner_affiliation_faculty current_colab["affiliation_institute"] = inner_affiliation_institute current_colab["affiliation_chair"] = inner_affiliation_chair current_colab["count"] = inner_count colabs.append(current_colab) except: print("Error at lsf_id while getting profil from inner_lsf_id: " + str(lsf_ID) + " " + str(inner_lsf_ID)) except: print("Error at lsf_id while getting colabs: " + str(lsf_ID)) current_author_year = {} current_author_year["year"] = year current_author_year["lsf_id"] = lsf_ID current_author_year["name"] = name current_author_year["firstname"] = firstname current_author_year["surname"] = surname current_author_year["title"] = title current_author_year["affiliation_university"] = affiliation_university current_author_year["affiliation_faculty"] = affiliation_faculty current_author_year["affiliation_institute"] = affiliation_institute current_author_year["affiliation_chair"] = affiliation_chair current_author_year["colabs"] = colabs id = str(year) + "_" + lsf_ID es.index(index=index_name, id=id, document=current_author_year) except: print("Error at lsf_id: " + str(lsf_ID)) continue except: print("Error in current year.") continue print("Done")