Ejemplo n.º 1
0
def configure():
    index_path = app.config["SEARCH_INDEX_PATH"]
    models_committed.connect(on_models_committed, sender=app)
    if not os.path.exists(index_path):
        os.mkdir(index_path)
        ix = index.create_in(index_path, search_schema)
        writer = ix.writer()
        # Index everything since this is the first time
        for model in [models.JobType, models.JobCategory, models.JobPost]:
            for ob in model.query.all():
                mapping = ob.search_mapping()
                public = mapping.pop("public")
                if public:
                    writer.add_document(**mapping)
        writer.commit()
Ejemplo n.º 2
0
    created here; this could impose a penalty on the initial commit of a model.
    '''

    bytype = {}  # sort changes by type so we can use per-model writer
    for change in changes:
        update = change[1] in ('update', 'insert')

        if hasattr(change[0].__class__, '__searchable__'):
            bytype.setdefault(change[0].__class__.__name__, []).append((update, change[0]))

    for typ, values in bytype.iteritems():
        index = whoosh_index(app, values[0][1])
        with index.writer() as writer:
            primary_field = values[0][1].search_query.primary
            searchable = values[0][1].__searchable__

            for update, v in values:
                # delete everything. stuff that's updated or inserted will get
                # added as a new doc. Could probably replace this with a whoosh
                # update.

                writer.delete_by_term(primary_field, unicode(getattr(v, primary_field)))

                if update:
                    attrs = dict((key, getattr(v, key)) for key in searchable)
                    attrs[primary_field] = unicode(getattr(v, primary_field))
                    writer.add_document(**attrs)


models_committed.connect(after_flush)