-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteachuser.py
More file actions
36 lines (31 loc) · 1.23 KB
/
Copy pathteachuser.py
File metadata and controls
36 lines (31 loc) · 1.23 KB
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
from conn import connElasticSearch
import pandas as pd
def teach_user(userId):
cn = connElasticSearch()
rs = cn.search(index='bratings', query={"match": {"uid": userId}}, size=10000)
array = []
# get all the books(isbn) for a user into a list
for hit in rs['hits']['hits']:
# keep rated by user books
userRating = int(hit['_source']['rating'])
if userRating:
isbn_rating = (hit['_source']['isbn'], userRating)
array.append(isbn_rating)
book_titles = []
summaries = []
ratings = []
book_isbn = []
# checking if all books exists in index 'books'
for t in array:
rs = cn.search(index='books', query={"match": {"isbn": t[0]}})
# if specific book exists in index 'books'
if rs['hits']['total']['value']:
# get the rating
ratings.append(int(t[1]))
for hit in rs['hits']['hits']:
book_titles += [hit['_source']['book_title']]
summaries += [hit['_source']['summary']]
book_isbn += [hit['_source']['isbn']]
# convert data to pandas df
d = {'isbn': book_isbn, 'book_title': book_titles, 'summary': summaries, 'rating': ratings}
return pd.DataFrame(d)