-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_index.py
39 lines (21 loc) · 845 Bytes
/
data_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from elasticsearch import Elasticsearch
import pandas as pd
import re
import configparser
def production_year_extract(text):
results = re.findall('\([0-9]{4,4}\)', text)
if len(results) == 1:
return int(results[0][1:-1])
else:
return pd.NA
movies = pd.read_csv("./data/movies.csv")
reviews = pd.read_csv("./data/ratings.csv")
movies["genres"] = movies["genres"].apply(lambda x: x.split("|"))
movies["year"] = movies["title"].apply(lambda x: production_year_extract(x))
config = configparser.ConfigParser()
config.read('settings.ini')
# Initiate ElasticSearch connection
es = Elasticsearch([{'host': config["ES"]["host"], 'port': config["ES"]["port"]}])
def index_movies(movies, es, index_name):
for i in movies.values:
es.index(index="index_name", body={**dict(zip(movies.columns, i))})