Skip to content

Commit

Permalink
[KAN-46] ES 에 메뉴 데이터 및 기타 데이터들 추가 (#7)
Browse files Browse the repository at this point in the history
* [KAN-46] ES 에 메뉴 데이터 및 기타 데이터들 추가

* [KAN-46] ES 에 메뉴 데이터 및 기타 데이터들 추가
  • Loading branch information
sinkyoungdeok authored May 10, 2024
1 parent 4edf376 commit 7f01aec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
45 changes: 42 additions & 3 deletions csv-to-es.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pandas as pd
from elasticsearch import Elasticsearch

file_path = 'restaurants.csv'
df = pd.read_csv(file_path)
restaurant_df = pd.read_csv('restaurants.csv')
menu_df = pd.read_csv('menus.csv')

now = datetime.datetime.now()
index_name = f"restaurant_{now.strftime('%Y_%m_%d_%H-%M')}"
Expand All @@ -18,14 +18,53 @@
"properties": {
"name": {"type": "text"},
"category": {"type": "text"},
"review_count": {"type": "text"},
"address": {"type": "text"},
"rating": {"type": "float"},
"number": {"type": "text"},
"image_url": {"type": "text"},
"custom_category": {"type": "text"},
"menus": {
"type": "nested",
"properties": {
"menu_name": {"type": "text"},
"price": {"type": "text"},
"description": {"type": "text"},
"is_representative": {"type": "text"},
"image_url": {"type": "text"}
}
}
}
})

# 데이터 인덱싱
for _, row in df.iterrows():
for _, row in restaurant_df.iterrows():
menus = menu_df[menu_df['restaurant_id'] == row['id']].to_dict('records')

for menu in menus:
if pd.isna(menu['image_url']):
menu.pop('image_url') # image_url 필드가 NaN이면 제거

if pd.isna(row['image_url']):
restaurant_image_url = None # NaN 값을 None으로 설정
else:
restaurant_image_url = row['image_url']

if pd.notna(row['rating']):
rating = float(row['rating'])
else:
rating = None

response = es.index(index=index_name, document={
"name": row['name'],
"category": row['category'],
"review_count": row['review_count'],
"address": row['address'],
"rating": rating,
"number": row['number'],
"image_url": restaurant_image_url,
"custom_category": row['custom_category'],
"menus": menus,
})
print(f"Indexed document ID: {response['_id']}, Result: {response['result']}")

Expand Down
1 change: 1 addition & 0 deletions es.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM python:3.8-slim
COPY es-requirements.txt es-requirements.txt
COPY restaurants.csv restaurants.csv
COPY csv-to-es.py csv-to-es.py
COPY menus.csv menus.csv

RUN pip install -r es-requirements.txt

Expand Down

0 comments on commit 7f01aec

Please sign in to comment.