| 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 |
|
| 3313 |
varun.gupt |
13 |
def __init__(self, price_data, new_index = True):
|
| 3232 |
varun.gupt |
14 |
self.indexDir = "/tmp/lucene-index-dir"
|
|
|
15 |
lucene.initVM()
|
|
|
16 |
dir = SimpleFSDirectory(File(self.indexDir))
|
|
|
17 |
self.analyzer = StandardAnalyzer(Version.LUCENE_30)
|
| 3313 |
varun.gupt |
18 |
self.writer = IndexWriter(dir, self.analyzer, new_index, IndexWriter.MaxFieldLength(512))
|
| 3232 |
varun.gupt |
19 |
self.price_data = price_data
|
|
|
20 |
|
|
|
21 |
def build(self):
|
|
|
22 |
print "Currently there are %d documents in the index..." % self.writer.numDocs()
|
| 3313 |
varun.gupt |
23 |
count = 0
|
| 3232 |
varun.gupt |
24 |
|
|
|
25 |
for phone_price in self.price_data:
|
|
|
26 |
print phone_price
|
|
|
27 |
#doc = PhonePriceDoc(phone_price)
|
| 3313 |
varun.gupt |
28 |
count += 1
|
|
|
29 |
|
| 3232 |
varun.gupt |
30 |
brand, name = Utils.extractBrandAndName(str(phone_price['name']))
|
|
|
31 |
doc = Document()
|
|
|
32 |
doc.add(Field("name", name, Field.Store.YES, Field.Index.ANALYZED))
|
|
|
33 |
doc.add(Field("brand", brand, Field.Store.YES, Field.Index.ANALYZED))
|
|
|
34 |
doc.add(Field("source", str(phone_price['source']), Field.Store.YES, Field.Index.ANALYZED))
|
|
|
35 |
doc.add(Field("price", str(phone_price['price']), Field.Store.YES, Field.Index.NO))
|
|
|
36 |
doc.add(Field("in_stock", str(phone_price['in_stock']), Field.Store.YES, Field.Index.NO))
|
|
|
37 |
doc.add(Field("url", str(phone_price['url']), Field.Store.YES, Field.Index.NO))
|
|
|
38 |
|
|
|
39 |
self.writer.addDocument(doc)
|
| 3313 |
varun.gupt |
40 |
|
| 3232 |
varun.gupt |
41 |
print "Indexed lines from stdin (%d documents in index)" % (self.writer.numDocs())
|
|
|
42 |
print "About to optimize index of %d documents..." % self.writer.numDocs()
|
|
|
43 |
self.writer.optimize()
|
|
|
44 |
print "...done optimizing index of %d documents" % self.writer.numDocs()
|
|
|
45 |
print "Closing index of %d documents..." % self.writer.numDocs()
|
| 3313 |
varun.gupt |
46 |
print "%d docs added" % count
|
| 3232 |
varun.gupt |
47 |
self.writer.close()
|
|
|
48 |
|
|
|
49 |
if __name__ == '__main__':
|
|
|
50 |
phones = GAEServletClient.getPhonePricesJSON()
|
| 3313 |
varun.gupt |
51 |
# print phones
|
|
|
52 |
indexer = IndexBuilder(price_data = phones, new_index = False)
|
| 3232 |
varun.gupt |
53 |
indexer.build()
|
|
|
54 |
# catalog_client = InventoryClient().get_client()
|
|
|
55 |
# items = catalog_client.getAllItems(True)
|
|
|
56 |
# print phones
|
|
|
57 |
# for item in items: print item.id, item.brand, item.modelName, item.modelNumber
|