Book / src /index_books.py
anwesh2410's picture
Upload 39 files
54862ee verified
raw
history blame contribute delete
967 Bytes
from elasticsearch import Elasticsearch, helpers
import pandas as pd
# Load books data
books_df = pd.read_csv('data/Books.csv', dtype={'Year-Of-Publication': 'str'}, low_memory=False)
# Elasticsearch setup
es = Elasticsearch("http://localhost:9202")
# Create an index for books (skip if already exists)
index_name = 'books'
if not es.indices.exists(index=index_name):
es.indices.create(index=index_name)
# Bulk indexing books data
actions = [
{
"_index": index_name,
"_id": row['ISBN'],
"_source": {
"Book-Title": row['Book-Title'],
"Book-Author": row['Book-Author'],
"Year-Of-Publication": row['Year-Of-Publication'],
"Publisher": row['Publisher'],
"Image-URL-S": row['Image-URL-S'],
"Image-URL-M": row['Image-URL-M'],
"Image-URL-L": row['Image-URL-L']
}
}
for _, row in books_df.iterrows()
]
# Indexing
helpers.bulk(es, actions)