def deleteIndex(url): """Delete all documents in an L{ObjectIndex}. @param url: The URL of the Solr index to delete. @return: A C{Deferred} that will fire when all documents have been deleted. """ client = SolrClient(url) yield client.deleteByQuery('*:*') yield client.commit()
def buildIndex(url, stream=sys.stderr): """Build documents in an L{ObjectIndex} for data in the main store. @param url: The URL of the Solr index to create documents in. @param stream: The file descriptor to send progress updates to. Defaults to C{sys.stderr}. @raise RuntimeError: Raised if the Solr index is not empty. @return: A C{Deferred} that will fire with the number of new documents that were created in the index. """ client = SolrClient(url) response = yield client.search('*:*', rows=1) if response.results.docs: raise RuntimeError('Index is not empty!') yield updateIndex(url, stream=stream)
def updateIndex(url, createdAfterTime=datetime.min, stream=sys.stderr): """ Build documents in an L{ObjectIndex} for data in the main store that has been updated since the provided C{datetime}. @param url: The URL of the Solr index to create documents in. @param createdAfterTime: An inclusive C{datetime} offset from which to update new tag-values. @param stream: The file descriptor to send progress updates to. Defaults to C{sys.stderr}. @return: A C{Deferred} that will fire with the number of new documents that were created in the index. """ client = SolrClient(url) index = ObjectIndex(client) MAX_DOCUMENTS = 1000 # setup progress bar progressbarWidth = 78 totalRows = getMainStore().find(TagValue).count() documentsPerDash = totalRows / progressbarWidth stream.write("[%s]" % (" " * progressbarWidth)) stream.flush() stream.write("\b" * (progressbarWidth + 1)) # return to start of bar documents = {} documentsProcessed = 0 result = groupby(_getAllTagValues(createdAfterTime), itemgetter(2)) for objectID, values in result: tagValues = dict((path, value) for path, value, _ in values) documents.update({objectID: tagValues}) if len(documents) >= MAX_DOCUMENTS: yield index.update(documents) documents = {} documentsProcessed += 1 if documentsProcessed == documentsPerDash: stream.write("-") stream.flush() documentsProcessed = 0 if documents: yield index.update(documents) yield client.commit()