How do I index a CSV file for a faceted search?

To index a CSV file for a faceted search, follow the sample Python script below. It will read your text file and index each row to SearchBlox through the REST-API.

After installing SearchBlox, you will need to create a custom collection before you run this script.

import csv
import urllib2

#Update with the URL where SearchBlox is running.
url = "http://localhost:8080/searchblox/api/rest/add"

#Update the path where you have placed your text file
with open ('/Users/Downloads/textfile.csv', 'rb') as f:
reader = csv.reader(f)
docid = 0
for row in reader:
docid = docid + 1

#Update the SearchBlox API Key with the key from your SearchBlox admin console.

#Use the collection name created instead of text index name.

xml = """<?xml version="1.0" encoding="utf-8"?>
<searchblox apikey="57836EABBDF80CCA0632AF4D8D0A6DE3"><document colname="textindex">
<description>"""+row[1] +"""</description>
<content>"""+row[0] + " - " +row[1] +"""</content>
<meta name="price">"""+str(row[2])+"""</meta>
<meta name="product_id">"""+str(row[3])+"""</meta>
<meta name="size">"""+str(row[4])+"""</meta>
<meta name="color">"""+str(row[5])+"""</meta>

req = urllib2.Request(url)
req.add_header('Content-Type', 'application/xml')
res = urllib2.urlopen(req, xml)

response = res.readlines()
for line in response:
print line

Have more questions? Submit a request