596 lines
24 KiB
Python
596 lines
24 KiB
Python
from elasticsearch import Elasticsearch
|
|
import sys
|
|
import os
|
|
import json
|
|
import shutil
|
|
|
|
# elasticsearch-connection
|
|
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 = "epo_index_1.0"
|
|
|
|
# checks if elastic is up and creates the index if not already done
|
|
def check_elastic():
|
|
if not es.ping():
|
|
sys.exit("No connection to Elasticsearch")
|
|
|
|
if not es.indices.exists(index=index_name):
|
|
settings = {
|
|
"analysis": {
|
|
"analyzer": {
|
|
"searchabilityAnalyzer": {
|
|
"type": "custom",
|
|
"tokenizer": "standard",
|
|
"filter": [
|
|
"lowercase",
|
|
"asciifolding"
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
mappings = {
|
|
"properties": {
|
|
"country": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"doc_number": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer",
|
|
"fields": {
|
|
"keyword": {
|
|
"type": "keyword",
|
|
"ignore_above": 250
|
|
}
|
|
}
|
|
},
|
|
"kind": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"date_publication": {
|
|
"type": "integer"
|
|
},
|
|
"date_publication_year": {
|
|
"type": "integer"
|
|
},
|
|
"application_number": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"application_filing_date": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"date_of_request_for_examination": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"date_of_despatch_of_1st_exam_report": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"date_of_patent_maintained_as_amended": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"request_for_conversion_to_natl_appl_date": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"request_for_conversion_to_natl_appl_country": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"date_of_suspension_interruption": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"indicates_the_type_of_suspension_or_interruption": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"date_of_resumption_of_proceedings": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"ipc": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"ipc_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"cpc": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"cpc_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"section": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"class": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"subclass": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"linked_indexing_code": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"unlinked_indexing_code": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"non_obligatory_suppl_class": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"main_group": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"sub_group": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"version": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"level": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"position": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"classification_value": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"action": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"data1": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"data2": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"office": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"title_de": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"title_en": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"title_fr": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"keywords_and_descriptors": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"applicants": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"applicants_name": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"applicants_name_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"applicants_adr": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"inventors": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"inventors_name": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"inventors_name_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"inventors_name_nested": {
|
|
"type": "nested",
|
|
"properties": {
|
|
"inventor": {
|
|
"type": "text",
|
|
"fields": {
|
|
"keyword": {
|
|
"type": "keyword",
|
|
"ignore_above": 250
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"inventors_adr": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"representatives": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"representatives_name": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"representatives_name_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"representatives_adr": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"states": {
|
|
"type": "text",
|
|
"fielddata": "true",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"states_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"extended_states": {
|
|
"type": "text",
|
|
"fielddata": "true",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"extended_states_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"abstract_lang": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"abstract": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"description_lang": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"description_header": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"description_paragraph": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"description_text": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"claims": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"claims_de": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"claims_en": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"claims_fr": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_lang": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_date_produced": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_type": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_status": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_office": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_file_reference_id": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_established": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_abs_approval": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_abstract": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_invention_title": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_invention_title_approval": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_unity_of_invention": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_unity_of_invention_fee_1": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_unity_of_invention_fee_2": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_unity_of_invention_fee_3": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_unity_of_invention_fee_4": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
},
|
|
"srep_protest_fees": {
|
|
"type": "text",
|
|
"analyzer": "searchabilityAnalyzer",
|
|
"search_analyzer": "searchabilityAnalyzer"
|
|
}
|
|
}
|
|
}
|
|
es.indices.create(index=index_name, mappings=mappings, settings=settings)
|
|
|
|
# takes the given path
|
|
# searches for all files in current directory and all sub-directories
|
|
# filters files for .json
|
|
# loads json into elasticsearch
|
|
def upload(path):
|
|
print("Uploading")
|
|
for root, dirs, files in os.walk(path):
|
|
for file in files:
|
|
with open(os.path.join(root, file), "r", encoding='utf-8') as json_file:
|
|
if json_file.name.endswith('.json'):
|
|
try:
|
|
json_loaded = json.load(json_file)
|
|
id = json_file.name[-16:-5]
|
|
|
|
try:
|
|
year = json_loaded["date_publication"]
|
|
year = year[0: 4]
|
|
json_loaded["date_publication_year"] = year
|
|
except Exception as e:
|
|
pass
|
|
|
|
try:
|
|
ipc = json_loaded["ipc"]
|
|
ipc_fixed = []
|
|
|
|
for i in ipc:
|
|
if i is None:
|
|
continue
|
|
if i.startswith("1") or i.startswith("2") or i.startswith("3") or i.startswith("4") or i.startswith("5") or i.startswith("6") or i.startswith("7") or i.startswith("8") or i.startswith("9") or i.startswith("0"):
|
|
ipc_fixed.append(i[1:])
|
|
else:
|
|
ipc_fixed.append(i)
|
|
|
|
json_loaded["ipc"] = ipc_fixed
|
|
except Exception as e:
|
|
pass
|
|
|
|
try:
|
|
ipc = json_loaded["ipc"]
|
|
ipc_without = []
|
|
|
|
for i in ipc:
|
|
ipc_without.append(i.replace(" ", "").replace("/", ","))
|
|
|
|
json_loaded["ipc_without"] = ipc_without
|
|
except Exception as e:
|
|
pass
|
|
|
|
try:
|
|
ipc_without = json_loaded["ipc_without"]
|
|
ipc_without_fixed = []
|
|
|
|
for i in ipc_without:
|
|
if i is None:
|
|
continue
|
|
if i.startswith("1") or i.startswith("2") or i.startswith("3") or i.startswith("4") or i.startswith("5") or i.startswith("6") or i.startswith("7") or i.startswith("8") or i.startswith("9") or i.startswith("0"):
|
|
ipc_without_fixed.append(i[1:])
|
|
else:
|
|
ipc_without_fixed.append(i)
|
|
|
|
json_loaded["ipc_without"] = ipc_without_fixed
|
|
except Exception as e:
|
|
pass
|
|
|
|
try:
|
|
ipc_without = json_loaded["ipc_without"]
|
|
ipc_text_without = ""
|
|
|
|
for i in ipc_without:
|
|
ipc_text_without += i + " - "
|
|
|
|
ipc_text_without = ipc_text_without[:-3]
|
|
|
|
json_loaded["ipc_text_without"] = ipc_text_without
|
|
except Exception as e:
|
|
pass
|
|
|
|
try:
|
|
temp = []
|
|
for inventor_name in json_loaded["inventors_name"]:
|
|
temp.append({"inventor": inventor_name})
|
|
json_loaded["inventors_name_nested"] = temp
|
|
except Exception as e:
|
|
pass
|
|
|
|
try:
|
|
title_de = json_loaded["title_de"]
|
|
if isinstance(title_de, dict):
|
|
title_de = title_de["#text"]
|
|
json_loaded["title_de"] = title_de
|
|
except Exception as e:
|
|
pass
|
|
try:
|
|
title_en = json_loaded["title_en"]
|
|
if isinstance(title_en, dict):
|
|
title_en = title_en["#text"]
|
|
json_loaded["title_en"] = title_en
|
|
except Exception as e:
|
|
pass
|
|
try:
|
|
title_fr = json_loaded["title_fr"]
|
|
if isinstance(title_fr, dict):
|
|
title_fr = title_fr["#text"]
|
|
json_loaded["title_fr"] = title_fr
|
|
except Exception as e:
|
|
pass
|
|
|
|
es.index(index=index_name, id=id, document=json_loaded)
|
|
except Exception as e:
|
|
print("--- Failed to upload " + json_file.name)
|
|
print(e)
|
|
|
|
if str(e) == "ApiError(413, 'None')":
|
|
folder = "./too_big_jsons"
|
|
if not os.path.exists(folder):
|
|
os.makedirs(folder)
|
|
destination_path = os.path.join(folder, file)
|
|
shutil.copy(os.path.join(root, file), destination_path)
|
|
|
|
|
|
# main - entry point
|
|
if __name__ == '__main__':
|
|
print("Starting")
|
|
check_elastic()
|
|
|
|
if len(sys.argv) != 2:
|
|
sys.exit("No path given.")
|
|
upload(sys.argv[1])
|
|
|
|
print("Done")
|