def add_or_update_items(items, solr_url=settings.SOLR_OPINION_URL): """Adds an item to a solr index. This function is for use with the update_index command. It's slightly different than the commands below because it expects a Django object, rather than a primary key. This rejects the standard Celery advice about not passing objects around, but thread safety shouldn't be an issue since this is only used by the update_index command, and we want to query and build the SearchDocument objects in the task, not in its caller. """ si = sunburnt.SolrInterface(solr_url, mode='w') if hasattr(items, "items") or not hasattr(items, "__iter__"): # If it's a dict or a single item make it a list items = [items] search_item_list = [] for item in items: try: if type(item) == Audio: search_item_list.append(SearchAudioFile(item)) elif type(item) == Document: search_item_list.append(SearchDocument(item)) except AttributeError: print "AttributeError trying to add doc.pk: %s" % item.pk except InvalidDocumentError: print "Unable to parse document %s" % item.pk try: si.add(search_item_list) except socket.error, exc: add_or_update_items.retry(exc=exc, countdown=120)
def add_or_update_audio_files(item_pks): si = sunburnt.SolrInterface(settings.SOLR_OPINION_URL, mode='w') item_list = [] for pk in item_pks: item = Audio.objects.get(pk=pk) item_list.append(SearchDocument(item)) si.add(item_list) si.commit()
def add_or_update_doc(pk, commit=True): """Updates the document in the index. Called by Document save function. """ si = sunburnt.SolrInterface(settings.SOLR_OPINION_URL, mode='w') try: si.add(SearchDocument(Document.objects.get(pk=pk))) if commit: si.commit() except SolrError, exc: add_or_update_doc.retry(exc=exc, countdown=30)
def update_cite(citation_id, commit=True): """If a citation and a document are both updated simultaneously, we will needlessly update the index twice. No easy way around it. """ si = sunburnt.SolrInterface(settings.SOLR_OPINION_URL, mode='w') cite = Citation.objects.get(pk=citation_id) for doc in cite.parent_documents.all(): search_doc = SearchDocument(doc) si.add(search_doc) if commit: si.commit()