Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3232 varun.gupt 1
'''
2
Created on 30-Aug-2011
3
 
4
@author: Varun Gupta
5
'''
6
from lucene import File, StandardAnalyzer, IndexWriter, Version, SimpleFSDirectory, Document, Field
7
from Clients import GAEServletClient
8
from PhonePriceDoc import PhonePriceDoc
9
import lucene, Utils
10
 
11
class IndexBuilder:
12
 
13
    def __init__(self, price_data):
14
        self.indexDir = "/tmp/lucene-index-dir"
15
        lucene.initVM()
16
        dir = SimpleFSDirectory(File(self.indexDir))
17
        self.analyzer = StandardAnalyzer(Version.LUCENE_30)
18
        self.writer = IndexWriter(dir, self.analyzer, True, IndexWriter.MaxFieldLength(512))
19
        self.price_data = price_data
20
 
21
    def build(self):
22
        print "Currently there are %d documents in the index..." % self.writer.numDocs()
23
 
24
        for phone_price in self.price_data:
25
            print phone_price
26
            #doc = PhonePriceDoc(phone_price)
27
            brand, name = Utils.extractBrandAndName(str(phone_price['name']))
28
            doc = Document()
29
            doc.add(Field("name", name, Field.Store.YES, Field.Index.ANALYZED))
30
            doc.add(Field("brand", brand, Field.Store.YES, Field.Index.ANALYZED))
31
            doc.add(Field("source", str(phone_price['source']), Field.Store.YES, Field.Index.ANALYZED))
32
            doc.add(Field("price", str(phone_price['price']), Field.Store.YES, Field.Index.NO))
33
            doc.add(Field("in_stock", str(phone_price['in_stock']), Field.Store.YES, Field.Index.NO))
34
            doc.add(Field("url", str(phone_price['url']), Field.Store.YES, Field.Index.NO))
35
 
36
            self.writer.addDocument(doc)
37
 
38
        print "Indexed lines from stdin (%d documents in index)" % (self.writer.numDocs())
39
        print "About to optimize index of %d documents..." % self.writer.numDocs()
40
        self.writer.optimize()
41
        print "...done optimizing index of %d documents" % self.writer.numDocs()
42
        print "Closing index of %d documents..." % self.writer.numDocs()
43
        self.writer.close()
44
 
45
if __name__ == '__main__':
46
    phones = GAEServletClient.getPhonePricesJSON()
47
    indexer = IndexBuilder(phones)
48
    indexer.build()
49
#    catalog_client = InventoryClient().get_client()
50
#    items = catalog_client.getAllItems(True)
51
#    print phones
52
#    for item in items:  print item.id, item.brand, item.modelName, item.modelNumber