Can I use Python to index URLs and Documents?

Yes. Below is a sample Python script that can be used to index URLs or documents (filesystem can be specified only when using the same server as SearchBlox).

Refer to our developer document page on REST API for indexing documents for more information on parameters passed https://developer.searchblox.com/docs/rest-api


def index_document(post_url: str, api_key: str, collection_name: str):
"""
Index a document by sending a POST request to SearchBlox API.

Developer Documentation Reference:
https://developer.searchblox.com/docs/rest-api#index

Args:
post_url (str): SearchBlox API URL to Index Documents
api_key (str): SearchBlox API Key
collection_name (str): Name of the collection

Returns:
Response received from SearchBlox (<Requests Response>)
"""
payload = {
"apikey": api_key,
"document": {
"colname": collection_name,
"url": "http://www.searchblox.com",
"uid": "http://www.searchblox.com",
"location": "http://www.searchblox.com",
"alpha": "SearchBlox Product Features",
"size": "44244",
"title": "SearchBlox Product Features",
"keywords": "SearchBlox, Solr, Lucene, Faceted Search",
"description": "SearchBlox Content Search Software",
"content": "content",
"lastmodified": "14 January 2020 06:19:42 GMT",
"contenttype": "HTML",
"meta": {
"location": "San Francisco",
"temp": "23",
"weather": "sunny"
}
}
}

# Make the POST request to SearchBlox API by supplying JSON
resp = requests.post(post_url, json=payload)

# Returning the response
return resp


if __name__ == '__main__':
# SearchBlox API Key, API URL to Index Documents and Collection Name
apikey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
posturl = "http://localhost:8080/searchblox/rest/v2/api/add"
collection = "HTTPCollection"

# Call the function and pass post url, api key and collection name
response = index_document(
post_url=posturl, api_key=apikey, collection_name=collection
)

try:
if response.status_code != 200:
print("Please check the JSON Payload. Here's the error message: {}"
.format(response.text))
else:
print(response.text)
except Exception:
print("Please check the JSON Payload")


To learn more about SearchBlox please visit our developer document site https://developer.searchblox.com/docs

Have more questions? Submit a request

Comments