| 17935 |
kshitij.so |
1 |
from elasticsearch import Elasticsearch
|
|
|
2 |
from dtr.utils.utils import get_mongo_connection
|
|
|
3 |
import optparse
|
|
|
4 |
|
|
|
5 |
parser = optparse.OptionParser()
|
|
|
6 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
7 |
default="localhost",
|
|
|
8 |
type="string", help="The HOST where the mongo server is running",
|
|
|
9 |
metavar="HOST")
|
| 18195 |
kshitij.so |
10 |
parser.add_option("-e", "--e", dest="elastic_search_host",
|
|
|
11 |
default="localhost",
|
|
|
12 |
type="string", help="The HOST where the elastic server is running",
|
|
|
13 |
metavar="HOST")
|
|
|
14 |
parser.add_option("-p", "--p", dest="elastic_search_port",
|
|
|
15 |
default="localhost",
|
|
|
16 |
type="string", help="The PORT where the elastic server is running",
|
|
|
17 |
metavar="HOST")
|
| 17935 |
kshitij.so |
18 |
|
| 18195 |
kshitij.so |
19 |
|
| 17935 |
kshitij.so |
20 |
(options, args) = parser.parse_args()
|
|
|
21 |
|
|
|
22 |
|
| 18194 |
kshitij.so |
23 |
subCategoryList = [19, 20, 27]
|
| 18195 |
kshitij.so |
24 |
es = Elasticsearch([{'host': options.elastic_search_host, 'port': options.elastic_search_port}])
|
| 17935 |
kshitij.so |
25 |
xstr = lambda s: s or ""
|
|
|
26 |
|
|
|
27 |
class __SkuInfo:
|
|
|
28 |
|
|
|
29 |
def __init__(self, id, title, category_id, subCategoryId, subCategory):
|
|
|
30 |
self.id = id
|
|
|
31 |
self.title = title
|
|
|
32 |
self.category_id = category_id
|
|
|
33 |
self.subCategoryId = subCategoryId
|
|
|
34 |
self.subCategory = subCategory
|
|
|
35 |
|
|
|
36 |
def main():
|
|
|
37 |
added = []
|
|
|
38 |
items = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'subCategoryId':{"$in":subCategoryList}}))
|
|
|
39 |
for item in items:
|
|
|
40 |
if item['skuBundleId'] in added:
|
|
|
41 |
continue
|
|
|
42 |
title = xstr(item['brand'])+" "+xstr(item['model_name'])
|
|
|
43 |
s_info = __SkuInfo(int(item['skuBundleId']),title,int(item['category_id']),int(item['subCategoryId']),item['subCategory'])
|
|
|
44 |
es.index(index='my_index', doc_type='my_type', id=s_info.id,body=s_info.__dict__)
|
|
|
45 |
added.append(int(item['skuBundleId']))
|
|
|
46 |
|
|
|
47 |
def validateListings():
|
|
|
48 |
offset , limit = 0, 100
|
|
|
49 |
|
|
|
50 |
body = {
|
|
|
51 |
"query" : {
|
|
|
52 |
"match_all" : {}
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
toDelete = []
|
|
|
56 |
while(True):
|
|
|
57 |
result = es.search("my_index", "my_type", body,from_=offset,size=limit)
|
|
|
58 |
print result
|
|
|
59 |
if len(result['hits']['hits']) > 0:
|
|
|
60 |
for x in result['hits']['hits']:
|
|
|
61 |
skuBundleId =x['_source']['id']
|
|
|
62 |
subCategoryId = x['_source']['subCategoryId']
|
|
|
63 |
category_id = x['_source']['category_id']
|
|
|
64 |
exist = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'skuBundleId':skuBundleId}))
|
|
|
65 |
if len(exist) ==0:
|
|
|
66 |
toDelete.append(skuBundleId)
|
|
|
67 |
else:
|
|
|
68 |
for item in exist:
|
|
|
69 |
if item['subCategoryId']!=subCategoryId or item['category_id'] !=category_id:
|
|
|
70 |
if skuBundleId not in toDelete:
|
|
|
71 |
print "Deleting item ",skuBundleId
|
|
|
72 |
toDelete.append(skuBundleId)
|
|
|
73 |
offset = offset+limit
|
|
|
74 |
else:
|
|
|
75 |
break
|
|
|
76 |
|
|
|
77 |
for id in toDelete:
|
|
|
78 |
print "Deleting id ",id
|
|
|
79 |
es.delete(index='my_index', doc_type='my_type', id=id)
|
| 18087 |
kshitij.so |
80 |
|
| 17935 |
kshitij.so |
81 |
|
|
|
82 |
if __name__ == '__main__':
|
|
|
83 |
main()
|
|
|
84 |
validateListings()
|