from httplib import HTTPConnection
def add(item_id, title, description, text):
"""
Add a document to index
"""
DATA = '''<add><doc>
<field name="id">%d</field>
<field name="title">%s</field>
<field name="description">%s</field>
<field name="text">%s</field>
</doc></add>''' % (item_id, title, description, text)
con = HTTPConnection('0.0.0.0:8983')
con.putrequest('POST', '/solr/update/')
con.putheader('content-length', str(len(DATA)))
con.putheader('content-type', 'text/xml; charset=UTF-8')
con.endheaders()
con.send(DATA)
r = con.getresponse()
if str(r.status) == '200':
print r.read()
else:
print r.status
print r.read()
def commit():
"""
commit changes
"""
DATA = '<commit/>'
con = HTTPConnection('0.0.0.0:8983')
con.putrequest('POST', '/solr/update/')
con.putheader('content-length', str(len(DATA)))
con.putheader('content-type', 'text/xml; charset=UTF-8')
con.endheaders()
con.send(DATA)
r = con.getresponse()
if str(r.status) == '200':
print r.read()
else:
print r.status
print r.read()
def search(key):
"""
perform a search
"""
con = HTTPConnection('0.0.0.0:8983')
con.putrequest('GET', '/solr/select?q=title:%s&start=0&rows=10&fl=id,title,description' % key)
con.endheaders()
con.send('')
r = con.getresponse()
if str(r.status) == '200':
print r.read()
else:
print r.status
print r.read()
add(5, 'Django search engine', 'blaaaa', 'text')
add(6, 'Google search', 'blaaa', 'text')
add(7, 'Solr fulltext search engine', 'blaaaa', 'text')
add(8, 'Car engine', 'blaaaa', 'text')
commit()
search('searching')