-
Notifications
You must be signed in to change notification settings - Fork 1
/
updateindex2.py
247 lines (208 loc) · 8.31 KB
/
updateindex2.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import requests
from nltk.corpus import stopwords
from elasticsearch import Elasticsearch
# api-endpoint
import urllib2
import re
from bs4 import BeautifulSoup
from nltk.corpus import wordnet
from nltk import word_tokenize
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
from collections import Counter
import ahocorasick
from nltk.corpus import stopwords
import os, sys, nltk
from nltk.util import ngrams
import io
import urllib, sys, bs4
import networkx as nx
import numpy as np
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize, word_tokenize
#from searchengine.settings import P
#from searchengine.settings import T
#from searchengine.settings import B
#from searchengine.settings import E
from pymongo import MongoClient
from newclassify import classifier
import math
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
import json
# location given here
location = "delhi technological university"
list_word = ['artificial', 'intelligence']
client = MongoClient()
db = client.test
#db.users.deleteOne( { status: "D" } )
Scursor = db.SearchHistory.find()
search_hist = []
def find_config(user):
client = MongoClient()
db = client.test
Scursor = db.SearchHistory.find({'user':user})
#search_hist = []
for hist in Scursor:
print hist['history']
list_word= hist['history']
print list_word
#csur= db.LikedPosts.find()
#for dc in csur:
# print dc
cursor = db.config.find()
user_id=0
for doc in cursor:
if user==doc['user']:
break
user_id =user_id+1
#print user_id
URL=[]
cursor = db.config.find({'user':user})
for document in cursor:
for key in document['choice']:
#print 'dssd'
doccat = 'doc'+key.lower().replace(' ','_')
#print doccat
URL.append(("http://localhost:9200/"+doccat+"/_search?size=1000&q=*:*",key,doccat))
doccat2 = 'doc'+key.lower().replace(' ','')
#print doccat2
URL.append(("http://localhost:9200/"+doccat2+"/_search?size=1000&q=*:*",key,doccat2))
for val in document['choice'][key]:
#print key.lower().replace(' ','_')+val.lower().replace(' ','_')
doccatsubcat = doccat+val.lower().replace(' ','_')
try:
r = requests.get(url = "http://localhost:9200/"+doccatsubcat+"/_search?size=1000&q=*:*")
rdata = r.json()
#print rdata
data = rdata['hits']['hits']
#print doccatsubcat
URL.append(("http://localhost:9200/"+doccatsubcat+"/_search?size=1000&q=*:*",key+' '+val,doccatsubcat))
except:
continue
if len(list_word)>10:
list_word = list_word[-10:]
#print list_word, "dscscs@@@@@@@@@@@@"
myquery ={
"query":
{
"multi_match":
{
"query": " ".join(list_word),
"fields": [ "data", "header" ]
}
},
"from" : 0, "size" : 100
}
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
result = es.search(index="_all", body=myquery)
max_score =0
for rows in result['hits']['hits']:
if max_score<rows['_score']:
max_score=rows['_score']
for rows in result['hits']['hits']:
score =( rows['_score']/max_score)*0.5
if len(rows['_source']['scores'])>user_id:
rows['_source']['scores'][user_id] =rows['_source']['scores'][user_id]+ score
else:
for i in range(user_id):
rows['_source']['scores'].append(0.15)
rows['_source']['scores'].append(0.15+score)
jsondata = rows['_source'] #json.dumps(dict1, ensure_ascii=False)
es.index(index='doc'+user.lower() +'home', doc_type='peopleimg', id=rows['_source']['link'],body=jsondata)
print URL
for i in URL:
myquery ={
"query":
{
"multi_match":
{
"query": " ".join(list_word),
"fields": [ "data", "header" ]
}
},
"from" : 0, "size" : 100
}
try:
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
result = es.search(index=i[2], body=myquery)
max_score =0
for rows in result['hits']['hits']:
if max_score<rows['_score']:
max_score=rows['_score']
for rows in result['hits']['hits']:
#print rows['_source']['header']
score =( rows['_score']/max_score)*0.5
if len(rows['_source']['scores'])>user_id:
rows['_source']['scores'][user_id] =rows['_source']['scores'][user_id]+ score
else:
for i in range(user_id):
rows['_source']['scores'].append(0.15)
rows['_source']['scores'].append(0.15+score)
jsondata = rows['_source'] #json.dumps(dict1, ensure_ascii=False)
es.update(index=i[2], doc_type='peopleimg', id=rows['_source']['link'],body={"doc":jsondata})
except Exception as e:
pass #print e
'''
for i in URL:
try:
print '#$$##############', i
# sending get request and saving the response as response object
r = requests.get(url = i[0])
#from newclassify import classifier
import math
stop_words = set(stopwords.words('english'))
client = MongoClient()
db = client.test
cursor = db.config.find()
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
# extracting data in json format
rdata = r.json()
data = rdata['hits']['hits']
for d in data:
try:
score = 0.0
#print d['_source']['header']
da = d['_source']['data']
text=''
scoredict={}
for word in list_word:
try:
#if len(word)!=0 or str(word)!='\n' or str(word)!='\t':
#text=text+str(word)
if word.lower() in da:
score = score + 0.01
except Exception as e:
pass
if len(d['_source']['scores'])>user_id:
d['_source']['scores'][user_id] =d['_source']['scores'][user_id]+ score
else:
for i in range(user_id):
d['_source']['scores'].append(0.25)
d['_source']['scores'].append(0.25+score)
d['_source']['flagindex'] = i[1]
#dict_list[user] = d['_source']['scores']
jsondata = d['_source'] #json.dumps(dict1, ensure_ascii=False)
#print "Inserting"
es.update(index=i[2], doc_type='peopleimg', id=d['_source']['link'],body={"doc":jsondata})
except Exception as e:
print e
except Exception as e:
print e
'''
#return dict_list
likecur = db.LikedPosts.find()
for doc in likecur:
print doc
Scursor = db.SearchHistory.find()
import time
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
if True:
for doc in Scursor:
#if True:
#print doc['user']
es.indices.delete(index='doc'+ doc['user']+'home', ignore=[400, 404])
find_config( doc['user'])
#time.sleep(2)
time.sleep(5)