Subversion Repositories SmartDukaan

Rev

Rev 18087 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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