Files
2024-01-06 11:50:36 +01:00

668 lines
24 KiB
Python

from os import name
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)
stat_dict = {}
index_name = "inse_stats"
redakteure = ["Ulrike Eichweber", "M. A. Ulrike Eichweber"]
def get_max_count():
print("Max_Count")
max_dict = {}
max_dict["count_projects_bmbf"] = int(es.count(index='projects_bmbf').get('count'))
max_dict["count_projects_eu"] = int(es.count(index='projects_eu').get('count'))
max_dict["count_projects_dfg"] = int(es.count(index='projects_dfg').get('count'))
max_dict["count_projects_all"] = max_dict["count_projects_bmbf"] + max_dict["count_projects_dfg"] + max_dict["count_projects_eu"]
max_dict["count_wos"] = int(es.count(index='wos_index_1.0').get('count'))
max_dict["count_startups"] = int(es.count(index='startups').get('count'))
max_dict["count_akutell"] = int(es.count(index='aktuell').get('count'))
max_dict["count_common"] = int(es.count(index='common_crawl_news').get('count'))
max_dict["count_epo"] = int(es.count(index='epo_index_1.0').get('count'))
max_dict["count_ude_persons"] = int(es.count(index='ude_persons_1.0').get('count'))
max_dict["count_ude_publications"] = int(es.count(index='due_index_1.0').get('count'))
stat_dict["max_count"] = max_dict
def get_stat_persons():
print("Persons")
# get all persons
aggs = {
"lsf_id": {
"terms": {
"size": 100000,
"field": "lsf_id.keyword"
}
}
}
docs_aggs = es.search(index="ude_persons_1.0", aggs=aggs, size=0)
ids = docs_aggs.get("aggregations").get("lsf_id").get("buckets")
current_year = int(datetime.now().year)
all_names = []
for id in ids:
current_person = {}
lsf_id = id.get("key")
print(" - " + lsf_id)
query = {
"match": {
"lsf_id": lsf_id
}
}
hit = es.search(index="ude_persons_1.0", query=query, size=1).get("hits").get("hits")[0].get("_source")
surname = hit.get("surname")
firstname = hit.get("firstname")
name = hit.get("name")
# this name causes some errors
if surname == "in der Heiden":
continue
# no aktuell editors
if name in redakteure:
continue
# filter out names with unallowed chars like "/" or "+"
#if any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234 -öäüÖÜÄÁáĆćÉéÍíĹĺŃńÓóŐőŔশÚúŰűÝýŹź' for c in firstname):
# continue
#if any(c not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234 -öäüÖÜÄÁáĆćÉéÍíĹĺŃńÓóŐőŔশÚúŰűÝýŹź' for c in surname):
# continue
if any(c in '()!%&/+/,;.' for c in firstname):
continue
if any(c in '()!%&/+/,;.' for c in surname):
continue
# check requirements for following analysis
if firstname == "" or surname == "":
es.index(index=index_name, id=lsf_id, document=current_person)
continue
if name in all_names:
continue
else:
all_names.append(name)
current_person["name"] = name
current_person["surname"] = surname
current_person["firstname"] = firstname
current_person["lsf_id"] = lsf_id
# ude_publications
query = {
"nested": {
"path": "authors",
"query": {
"match": {
"authors.lsf_id": lsf_id
}
}
}
}
current_person["ude_publication_hits"] = es.search(index="due_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# ude_publications_last_year
query = {
"bool": {
"must": [
{
"nested": {
"path": "authors",
"query": {
"match": {
"authors.lsf_id": lsf_id
}
}
}
},
{
"range": {
"year.keyword": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
]
}
}
current_person["ude_publication_hits_last_year"] = es.search(index="due_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# akutell
query = {
"multi_match": {
"query": firstname + " " + surname,
"fields": ["*"],
"type" : "phrase"
}
}
current_person["akutell_hits"] = es.search(index="aktuell", query=query, size=0).get("hits").get("total").get("value")
# akutell_last_year
query = {
"bool": {
"must": [
{
"multi_match": {
"query": firstname + " " + surname,
"fields": ["*"],
"type" : "phrase"
}
},
{
"range": {
"date_year.keyword": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
]
}
}
current_person["akutell_hits_last_year"] = es.search(index="aktuell", query=query, size=0).get("hits").get("total").get("value")
# projects_dfg
query = {
"multi_match": {
"query": firstname + " " + surname,
"fields": ["*"],
"type" : "phrase"
}
}
current_person["projects_dfg_hits"] = es.search(index="projects_dfg", query=query, size=0).get("hits").get("total").get("value")
# wos_publications
# old must block
# "must": [
# {
# "nested": {
# "path": "Author Full Names",
# "query": {
# "query_string": {
# "default_field": "Author Full Names.author",
# "query": firstname + " AND " + surname
# }
# }
# }
# }
# ],
query = {
"bool": {
"must": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"nested": {
"path": "Author Full Names",
"query": {
"match_phrase": {
"Author Full Names.author": surname + ", " + firstname
}
}
}
},
{
"nested": {
"path": "Author Full Names",
"query": {
"match_phrase": {
"Author Full Names.author": surname + ", " + firstname.split(" ")[0]
}
}
}
}
]
}
}
],
"should": [
{
"query_string": {
"default_field": "Email Addresses",
"query": "@uni-due.de"
}
},
{
"match_phrase": {
"Affiliation": "University of Duisburg Essen"
}
}
],
"minimum_should_match": 1
}
}
current_person["wos_publication_hits"] = es.search(index="wos_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# wos_publications_last_year
query = {
"bool": {
"must": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"nested": {
"path": "Author Full Names",
"query": {
"match_phrase": {
"Author Full Names.author": surname + ", " + firstname
}
}
}
},
{
"nested": {
"path": "Author Full Names",
"query": {
"match_phrase": {
"Author Full Names.author": surname + ", " + firstname.split(" ")[0]
}
}
}
}
]
}
},
{
"range": {
"Publication Year.keyword": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
],
"should": [
{
"query_string": {
"default_field": "Email Addresses",
"query": "@uni-due.de"
}
},
{
"match_phrase": {
"Affiliation": "University of Duisburg Essen"
}
}
],
"minimum_should_match": 1
}
}
current_person["wos_publication_hits_last_year"] = es.search(index="wos_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# patents
query = {
"bool": {
"must": [
{
"match_phrase": {
"inventors_name_text": surname + ", " + firstname
}
},
{
"match_phrase": {
"applicants_name_text": "Universität Duisburg-Essen"
}
}
]
}
}
current_person["patent_hits"] = es.search(index="epo_index_1.0", query=query, size=0).get("hits").get("total").get("value")
# patents_last_year
query = {
"bool": {
"must": [
{
"match_phrase": {
"inventors_name_text": surname + ", " + firstname
}
},
{
"match_phrase": {
"applicants_name_text": "Universität Duisburg-Essen"
}
},
{
"range": {
"date_publication_year": {
"gte": current_year - 1,
"lte": current_year - 1
}
}
}
]
}
}
current_person["patent_hits_last_year"] = es.search(index="epo_index_1.0", query=query, size=0).get("hits").get("total").get("value")
es.index(index=index_name, id=lsf_id, document=current_person)
def upload():
print("Uploading")
es.index(index=index_name, id="stats", document=stat_dict)
def check_elastic():
index_name = "inse_stats"
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"
]
}
}
}
}
es.indices.create(index=index_name, settings=settings)
def get_stat_affiliation():
print("Affiliations")
# get all affiliations
# faculty
aggs = {
"aff": {
"terms": {
"size": 100000,
"field": "affiliation_faculty.keyword"
}
}
}
docs_aggs = es.search(index="ude_persons_1.0", aggs=aggs, size=0)
affis = docs_aggs.get("aggregations").get("aff").get("buckets")
for aff in affis:
current_affiliation = {}
aff_name = aff.get("key")
print(" - " + aff_name)
query = {
"match_phrase": {
"path.keyword": aff_name
}
}
current_affiliation["affiliation_name"] = aff_name
current_affiliation["affiliation_hits"] = es.search(index="projects_dfg", query=query, size=0).get("hits").get("total").get("value")
es.index(index=index_name, document=current_affiliation)
# chair
aggs = {
"aff": {
"terms": {
"size": 100000,
"field": "affiliation_chair.keyword"
}
}
}
docs_aggs = es.search(index="ude_persons_1.0", aggs=aggs, size=0)
affis = docs_aggs.get("aggregations").get("aff").get("buckets")
for aff in affis:
current_affiliation = {}
aff_name = aff.get("key")
print(" - " + aff_name)
query = {
"match_phrase": {
"path.keyword": aff_name
}
}
current_affiliation["affiliation_name"] = aff_name
current_affiliation["affiliation_hits"] = es.search(index="projects_dfg", query=query, size=0).get("hits").get("total").get("value")
es.index(index=index_name, document=current_affiliation)
# institute
aggs = {
"aff": {
"terms": {
"size": 100000,
"field": "affiliation_institute.keyword"
}
}
}
docs_aggs = es.search(index="ude_persons_1.0", aggs=aggs, size=0)
affis = docs_aggs.get("aggregations").get("aff").get("buckets")
for aff in affis:
current_affiliation = {}
aff_name = aff.get("key")
print(" - " + aff_name)
query = {
"match_phrase": {
"path.keyword": aff_name
}
}
current_affiliation["affiliation_name"] = aff_name
current_affiliation["affiliation_hits"] = es.search(index="projects_dfg", query=query, size=0).get("hits").get("total").get("value")
es.index(index=index_name, document=current_affiliation)
def get_best_25():
print("Best_25")
best_25 = {}
query = {
"match_all" : {}
}
### best_25_ever
# ude_publication_hits
sort = {
"ude_publication_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_ude_publication_hits"] = temp
# akutell_hits
sort = {
"akutell_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_akutell_hits"] = temp
# projects_dfg_hits
sort = {
"projects_dfg_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_projects_dfg_hits"] = temp
# projects_affiliation_dfg_hits
sort = {
"affiliation_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("affiliation_name"), "number": hit.get("sort")[0]})
best_25["best_25_projects_affiliation_dfg_hits"] = temp
# wos_publication_hits
sort = {
"wos_publication_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_wos_publication_hits"] = temp
# patent_hits
sort = {
"patent_hits": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_patent_hits"] = temp
### best_25_last year
# ude_publication_hits_last_year
sort = {
"ude_publication_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_ude_publication_hits_last_year"] = temp
# akutell_hits_last_year
sort = {
"akutell_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_akutell_hits_last_year"] = temp
# wos_publication_hits_last_year
sort = {
"wos_publication_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_wos_publication_hits_last_year"] = temp
# patent_hits_last_year
sort = {
"patent_hits_last_year": {
"order": "desc"
}
}
hits = es.search(index="inse_stats", query=query, sort=sort, size=25).get("hits").get("hits")
temp = []
for hit in hits:
temp.append({"name": hit.get("_source").get("name"),
"firstname": hit.get("_source").get("firstname"),
"surname": hit.get("_source").get("surname"),
"number": hit.get("sort")[0]})
best_25["best_25_patent_hits_last_year"] = temp
stat_dict["best_25"] = best_25
def get_redakteure():
aggs = {
"name": {
"terms": {
"size": 100000,
"field": "redaktion.keyword"
}
}
}
docs_aggs = es.search(index="aktuell", aggs=aggs, size=0)
names = docs_aggs.get("aggregations").get("name").get("buckets")
for name in names:
name = name["key"]
if name.startswith("Redaktion: "):
name = name[11:]
if name.find(",") != -1:
name = name[0: (name.find(","))]
if name not in redakteure and len(name) > 0 and "\r" not in name and "/" not in name:
redakteure.append(name)
if __name__ == '__main__':
print("Starting")
check_elastic()
get_max_count()
get_redakteure()
get_stat_persons()
get_stat_affiliation()
get_best_25()
upload()
print("Done")