商品推薦範例:應用 Solr 與 Python

範例程式

# -*- coding: utf-8 -*- import sys import solr from urllib2 import * from operator import itemgetter, attrgetter, methodcaller class SolrTesting: server = 'localhost' tcpPort = 8983 nameRepository = 'collection1' def __init__(self): pass def setParams(self, server, tcpPort, nameRepository): self.server = server self.tcpPort = tcpPort self.nameRepository = nameRepository def setParams2(self, nameRepository): self.nameRepository = nameRepository def getURL(self): return ("http://%s:%d/solr/%s" % (self.server, self.tcpPort, self.nameRepository)) def showProductInfo(self, hit, isProduct): if isProduct: print "\t%s\t%s\t%s\t%.2f" % (hit['id'], hit['idProduct'], hit['nameProduct'], hit['priceProduct']), else: print "\t\t%s\t%s\t%s\t%.2f" % (hit['id'], hit['idProduct'], hit['nameProduct'], hit['priceProduct']), try: print "\t%.2f" % hit['scoreRecommendation'] except: print def usingSolrPyWay(self): solrInst = solr.SolrConnection(self.getURL()) # Find Product Packages response = solrInst.query('isProduct:false') print "Package List:" for hit in response.results: print "\tPackage ID: %s" % (hit['id']) productList = hit['idProduct'].split("|") for productID in productList: response2 = solrInst.query("id:%s" % (productID)) for hit2 in response2.results: self.showProductInfo(hit2, False) # Find Products response = solrInst.query('isProduct:true') print "Product List:" for hit in response.results: self.showProductInfo(hit, True) # Find Priced Products response = solrInst.query('isProduct:true AND priceProduct:[* TO 6000]') print "Product List (priceProduct < 6000):" for hit in response.results: self.showProductInfo(hit, True) # Find Promoting Products response = solrInst.query('isProduct:true AND levelPromotion:[* TO 1]') print "Promoting Product List (levelPromotion = 1):" for hit in response.results: self.showProductInfo(hit, True) # Find Hotest Products response = solrInst.query('isProduct:true AND freqencyVisit:[500 TO *]') print "Hotest Product List (freqencyVisit > 500):" for hit in response.results: self.showProductInfo(hit, True) def usingUrlLib2Way(self): # Complex Product Recommendation Score # User Defined Scoring Function # fl=*,scoreRecommandation:sqrt(sum(pow(log(freqencyVisit),2),pow(levelPromotion,2))) solrURL2 = "%s/select?q=%s&fl=%s&wt=python" % (self.getURL(), \ "isProduct:true", \ "*,scoreRecommendation:" \ "sqrt(sum(pow(log(freqencyVisit),2),pow(levelPromotion,2)))") solrInst2 = urlopen(solrURL2) resultList = eval( solrInst2.read() ) sortDocs = sorted(resultList['response']['docs'], key=itemgetter('scoreRecommendation'), reverse=True) print "Recommendation scores:" for hit in sortDocs: self.showProductInfo(hit, True) if __name__ == '__main__': server = 'localhost' tcpPort = 8983 nameRepository = 'merchandise' solrTesting = SolrTesting() solrTesting.setParams2(nameRepository) solrTesting.usingSolrPyWay() solrTesting.usingUrlLib2Way()

程式輸出

網頁程式

# -*- coding: utf-8 -*- import sys import cgi import time from operator import itemgetter, attrgetter, methodcaller import solr from urllib2 import * class SolrTesting: server = 'localhost' tcpPort = 8983 nameRepository = 'collection1' def __init__(self): pass def setParams(self, server, tcpPort, nameRepository): self.server = server self.tcpPort = tcpPort self.nameRepository = nameRepository def setParams2(self, nameRepository): self.nameRepository = nameRepository def getURL(self): return ("http://%s:%d/solr/%s" % (self.server, self.tcpPort, self.nameRepository)) def showProductInfo(self, hit): print "<tr><td>%s</td><td>%s</td><td>%s</td><td align='right'>%.2f" % (hit['id'], hit['idProduct'], hit['nameProduct'],

hit['priceProduct']), try: print "</td><td>%.2f</td></tr>" % hit['scoreRecommendation'] except: print "</td><td>&nbsp</td></tr>" def usingSolrPyWay(self): solrInst = solr.SolrConnection(self.getURL()) # Find Product Packages response = solrInst.query('isProduct:false') print "<tr><td colspan='5' align='center' bgcolor='silver'><strong style='font-size:25px'>Package List</strong></td></tr>" for hit in response.results: print "<tr><td colspan='5' bgcolor='yellow'>Package ID: %s</td></tr>" % (hit['id']) productList = hit['idProduct'].split("|") for productID in productList: response2 = solrInst.query("id:%s" % (productID)) for hit2 in response2.results: self.showProductInfo(hit2) # Find Products response = solrInst.query('isProduct:true') print "<tr><td colspan='5' align='center' bgcolor='silver'><strong style='font-size:25px'>Product List</stong></td></tr>" for hit in response.results: self.showProductInfo(hit) # Find Priced Products response = solrInst.query('isProduct:true AND priceProduct:[* TO 6000]') print "<tr><td colspan='5' align='center' bgcolor='silver'><strong style='font-size:25px'>Product List (priceProduct < 6000)

</strong></td></tr>" for hit in response.results: self.showProductInfo(hit) # Find Promoting Products response = solrInst.query('isProduct:true AND levelPromotion:[* TO 1]') print "<tr><td colspan='5' align='center' bgcolor='silver'><strong style='font-size:25px'>Promoting Product List (levelPromotion = 1)

</strong></td></tr>" for hit in response.results: self.showProductInfo(hit) # Find Hotest Products response = solrInst.query('isProduct:true AND freqencyVisit:[500 TO *]') print "<tr><td colspan='5' align='center' bgcolor='silver'><strong style='font-size:25px'>Hotest Product List (freqencyVisit > 500)

</strong></td></tr>" for hit in response.results: self.showProductInfo(hit) def usingUrlLib2Way(self): # Complex Product Recommendation Score # User Defined Scoring Function # fl=*,scoreRecommandation:sqrt(sum(pow(log(freqencyVisit),2),pow(levelPromotion,2))) solrURL2 = "%s/select?q=%s&fl=%s&wt=python" % (self.getURL(), \ "isProduct:true", \ "*,scoreRecommendation:" \ "sqrt(sum(pow(log(freqencyVisit),2),pow(levelPromotion,2)))") solrInst2 = urlopen(solrURL2) resultList = eval( solrInst2.read() ) sortDocs = sorted(resultList['response']['docs'], key=itemgetter('scoreRecommendation'), reverse=True) print "<tr><td colspan='5' align='center' bgcolor='silver'><strong style='font-size:25px'>Recommendation scores:</strong></td></tr>" for hit in sortDocs: self.showProductInfo(hit) server = 'localhost' tcpPort = 8080 nameRepository = 'merchandise' print "Content-type: text/html\n\n" print "<p><h3><a href='#' onclick='history.go(-1); return false;'>%s</a></h3></p>" % ("Go Back") print "<div style='margin-left:5em;font-family:courier''><table border='1' width='800'>" solrTesting = SolrTesting() solrTesting.setParams(server, tcpPort, nameRepository) solrTesting.usingSolrPyWay() solrTesting.usingUrlLib2Way() print "</table></div>" print "<p><h3><a href='#' onclick='history.go(-1); return false;'>%s</a></h3></p>" % ("Go Back")