{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install elasticsearch","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:02.430206Z","iopub.execute_input":"2021-11-22T00:09:02.430603Z","iopub.status.idle":"2021-11-22T00:09:15.971956Z","shell.execute_reply.started":"2021-11-22T00:09:02.430556Z","shell.execute_reply":"2021-11-22T00:09:15.971071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from elasticsearch import Elasticsearch\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:15.975806Z","iopub.execute_input":"2021-11-22T00:09:15.976102Z","iopub.status.idle":"2021-11-22T00:09:16.094151Z","shell.execute_reply.started":"2021-11-22T00:09:15.976066Z","shell.execute_reply":"2021-11-22T00:09:16.093217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = Elasticsearch()\n\n\ntry:\n  es.indices.create(index='docs')\nexcept Exception as e:\n  es.indices.delete(index='docs')\n  es.indices.create(index='docs')","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:16.095504Z","iopub.execute_input":"2021-11-22T00:09:16.095759Z","iopub.status.idle":"2021-11-22T00:09:16.187475Z","shell.execute_reply.started":"2021-11-22T00:09:16.095728Z","shell.execute_reply":"2021-11-22T00:09:16.185786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_df = pd.read_csv(\"../input/trec-covid-information-retrieval/CORD-19/CORD-19/metadata.csv\")\ntopic_df = pd.read_csv(\"../input/trec-covid-information-retrieval/topics-rnd3.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:16.188447Z","iopub.status.idle":"2021-11-22T00:09:16.188856Z","shell.execute_reply.started":"2021-11-22T00:09:16.188616Z","shell.execute_reply":"2021-11-22T00:09:16.188641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in metadata_df.iterrows():\n    es.index(index='docs',id=index,document={\n            \"cord_uid\": row[\"cord_uid\"],\n            \"abstract\": row[\"abstract\"] if not pd.isna(row[\"abstract\"]) else ''\n        })","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:16.190577Z","iopub.status.idle":"2021-11-22T00:09:16.190978Z","shell.execute_reply.started":"2021-11-22T00:09:16.190775Z","shell.execute_reply":"2021-11-22T00:09:16.190805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"topic_ids = []\ncord_ids = []\n\nfor index, row in topic_df.iterrows():\n    res= es.search(index='docs', body={\n        'query':{\n            'match':{\n                \"abstract\":row[\"query\"]\n            }\n        }\n    })\n    for hit in res['hits']['hits']:\n        cord_ids.append(hit['_source']['cord_uid'])\n        topic_ids.append(row[\"topic-id\"])","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:16.192382Z","iopub.status.idle":"2021-11-22T00:09:16.193082Z","shell.execute_reply.started":"2021-11-22T00:09:16.192863Z","shell.execute_reply":"2021-11-22T00:09:16.192892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'topic-id': topic_ids, 'cord-id': cord_ids})","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:16.194488Z","iopub.status.idle":"2021-11-22T00:09:16.195078Z","shell.execute_reply.started":"2021-11-22T00:09:16.194759Z","shell.execute_reply":"2021-11-22T00:09:16.19479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-22T00:09:16.196444Z","iopub.status.idle":"2021-11-22T00:09:16.197024Z","shell.execute_reply.started":"2021-11-22T00:09:16.196707Z","shell.execute_reply":"2021-11-22T00:09:16.196738Z"},"trusted":true},"execution_count":null,"outputs":[]}]}