-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataFromDB.py
More file actions
143 lines (115 loc) · 4.27 KB
/
DataFromDB.py
File metadata and controls
143 lines (115 loc) · 4.27 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from elasticsearch import Elasticsearch
import elasticsearch.helpers
from collections import OrderedDict
from operator import itemgetter
import json
import random
TOP = 2000000
NUMBER_OF_DATAS = str(TOP)
P2V_DATA_FILE = "data/p2v-data" + NUMBER_OF_DATAS
P2V_QUE_FILE = "data/p2v-question" + NUMBER_OF_DATAS
DUMPED_DATA_FILE = "data/search-query.json"
datas = []
def init_file_name():
global NUMBER_OF_DATAS
global P2V_DATA_FILE
global P2V_QUE_FILE
NUMBER_OF_DATAS = str(TOP)
P2V_DATA_FILE = "data/p2v-data" + NUMBER_OF_DATAS
P2V_QUE_FILE = "data/p2v-question" + NUMBER_OF_DATAS
def get_data(file_load):
init_file_name()
count = dict()
if file_load:
with open(DUMPED_DATA_FILE, 'a') as outfile:
try:
es = Elasticsearch(['http://es.backpackbang.com:9200/'])
results = elasticsearch.helpers.scan(es,
index="search-results-v1",
doc_type="amazon",
request_timeout=30000,
scroll=u'1m',
size="10000",
query={"query": {"match_all": {}}})
cnt = 0
outfile.write('[')
mf = False
for item in results:
cnt += 1
if mf:
outfile.write(',\n')
mf = True
flag = False
outfile.write('[\n')
for data in item['_source']['results']:
if flag:
outfile.write(',\n')
flag = True
json.dump(data, outfile)
outfile.write('\n]')
if cnt % 10000 == 0:
print(cnt)
outfile.write(']')
except Exception as e:
print(e)
with open(DUMPED_DATA_FILE, 'r') as infile:
res = []
loop = 0
for line in infile:
res.append(json.loads(line))
loop += 1
if loop % 1000 == 0:
print("Got search query:", loop)
print("All search Found. len:", len(res))
for doc in res:
tmpData = []
for result in doc:
asin = result['asin']
tmpData.append(asin)
if asin not in count:
count[asin] = 0
count[asin] += 1
datas.append(tmpData)
if len(datas) % 1000 == 0:
print("Data collected:", len(datas))
print("All data collected. len:", len(datas))
count_sorted = OrderedDict(sorted(count.items(), key=itemgetter(1), reverse=True))
count_final = dict()
for ind, asin in enumerate(count_sorted):
if ind >= TOP:
break
count_final[asin] = count_sorted[asin]
del count_sorted
del count
tmpData = datas.copy()
datas.clear()
loop = 0
for data in tmpData:
tmp = []
for asin in data:
if asin in count_final:
tmp.append(asin)
datas.append(tmp)
loop += 1
if loop % 1000 == 0:
print("Final data collected", loop, "in", len(tmpData))
print("Final data collection over. len:", len(datas))
del tmpData
with open(P2V_QUE_FILE, "w") as qp:
qp.write(": a-b-c")
qp.write("\n")
loop = 0
with open(P2V_DATA_FILE, "w") as fp:
with open(P2V_QUE_FILE, "a") as qp:
for data in datas:
if len(data) > 3:
fp.write(" ".join(data))
fp.write('\n')
if random.randint(0, 1) == 1:
qp.write(" ".join(data[0:4]))
qp.write("\n")
loop += 1
if loop % 1000 == 0:
print("File written", loop, "in", len(datas))
if __name__ == '__main__':
get_data(False)