Add/Update a document in the index

def _update_doc_in_index(self, index_writer, doc):
        """
        Add/Update a document in the index
        """
        all_labels = set(self.label_list)
        doc_labels = set(doc.labels)
        new_labels = doc_labels.difference(all_labels)

        # can happen when we recreate the index from scratch
        for label in new_labels:
            self.create_label(label)

        last_mod = datetime.datetime.fromtimestamp(doc.last_mod)
        docid = str(doc.docid)

        dochash = doc.get_docfilehash()
        dochash = (u"%X" % dochash)

        doc_txt = doc.get_index_text()
        assert(isinstance(doc_txt, str))
        labels_txt = doc.get_index_labels()
        assert(isinstance(labels_txt, str))

        # append labels to doc txt, because we usually search on doc_txt
        doc_txt += " " + labels_txt

        query = whoosh.query.Term("docid", docid)
        index_writer.delete_by_query(query)

        index_writer.update_document(
            docid=docid,
            doctype=doc.doctype,
            docfilehash=dochash,
            content=strip_accents(doc_txt),
            label=strip_accents(labels_txt),
            date=doc.date,
            last_read=last_mod
        )
        return True