def reindex(index): """ Iterate over all documents we're able to find (even those that are already in the search index) and index them. Note that this may take a lot of time. """ from xappy import errors from inyoka.core.resource import IResourceManager from inyoka.core.search import create_search_document index = IResourceManager.get_search_indexes()[index] # iterate over all search providers... with index.indexer_connection() as indexer: for provider in index.providers.itervalues(): # ... to get all their data for id, obj in provider.prepare_all(): # create a new document for the search index doc = create_search_document('%s-%s' % (provider.name, id), obj) try: # try to create a new search entry indexer.add(doc) except errors.IndexerError: # there's already an exising one, replace it indexer.replace(doc) indexer.flush()
def init_db(**kwargs): kwargs['tables'] = list(IResourceManager.get_models(tables=True)) is_test = kwargs.pop('is_test', False) bind = kwargs.pop('bind', None) if kwargs['tables']: conn = bind or db.get_engine() metadata.create_all(conn, **kwargs) # some essential database things from inyoka.core.auth.models import User, UserProfile anon_name = ctx.cfg['anonymous_name'] if not User.query.filter_by(username=anon_name).first(): anon = User(username=anon_name, email=u'', password=u'') UserProfile(user=anon) admin_credentials = { 'username': u'admin', 'email': u'*****@*****.**' } if is_test and not User.query.filter_by(**admin_credentials).first(): admin = User(password=u'default', **admin_credentials) UserProfile(user=admin) db.session.commit()
def flush_indexer(): """ Flush all indexer connections. """ logger = flush_indexer.get_logger() indexes = IResourceManager.get_search_indexes() for index in indexes.itervalues(): with index.indexer_connection() as indexer: logger.debug('Flush search index: %s' % index.name) indexer.flush()
def get_cls(self, name): """Try to find the right class for `name`""" cls = None models = list(IResourceManager.get_models()) names = [m.__name__ for m in models] if name in names: cls = models[names.index(name)] # check that the class was found. if cls is None: raise AttributeError('Model %s not found' % name) return cls
def search(index, query, count=50): """ Search for `query` in the search index `index` and print `count` results. """ from inyoka.core.resource import IResourceManager index = IResourceManager.get_search_indexes()[index] with index.searcher_connection() as searcher: query = searcher.query_parse(query, allow=index.direct_search_allowed) results = searcher.search(query, 0, int(count)) for result in results: print u'%d. %s' % (result.rank, result.id)
def _process_result_ids(index, result_ids): """ Process a list of result ids to a list of the result itselves in an efficient way. This function does not change the result order which is important for senseful searching. """ results = {} for name, provider in IResourceManager.get_search_providers()[index].iteritems(): # get all ids that belong to this provider ids = [r[1] for r in ifilter(lambda r: r[0] == name, result_ids)] # if there are no ids don't call the `prepare` method to avoid execution # of senseless queries if ids: for idx, obj in enumerate(provider.process(ids)): results['%s-%s' % (name, ids[idx])] = obj return [results['%s-%s' % (area, id)] for area, id in result_ids]
def _process_result_ids(index, result_ids): """ Process a list of result ids to a list of the result itselves in an efficient way. This function does not change the result order which is important for senseful searching. """ results = {} for name, provider in IResourceManager.get_search_providers( )[index].iteritems(): # get all ids that belong to this provider ids = [r[1] for r in ifilter(lambda r: r[0] == name, result_ids)] # if there are no ids don't call the `prepare` method to avoid execution # of senseless queries if ids: for idx, obj in enumerate(provider.process(ids)): results['%s-%s' % (name, ids[idx])] = obj return [results['%s-%s' % (area, id)] for area, id in result_ids]
def init_db(**kwargs): kwargs['tables'] = list(IResourceManager.get_models(tables=True)) is_test = kwargs.pop('is_test', False) bind = kwargs.pop('bind', None) if kwargs['tables']: conn = bind or db.get_engine() metadata.create_all(conn, **kwargs) # some essential database things from inyoka.core.auth.models import User, UserProfile anon_name = ctx.cfg['anonymous_name'] if not User.query.filter_by(username=anon_name).first(): anon = User(username=anon_name, email=u'', password=u'') UserProfile(user=anon) admin_credentials = {'username': u'admin', 'email': u'*****@*****.**'} if is_test and not User.query.filter_by(**admin_credentials).first(): admin = User(password=u'default', **admin_credentials) UserProfile(user=admin) db.session.commit()
def providers(self): return IResourceManager.get_search_providers()[self.name]
def get_index_implementation(name): if isinstance(name, SearchIndex): return name return IResourceManager.get_search_indexes()[name]