def activate(self): if not self.to_be_indexed: return log.info("Indexing the quotes database for the first time") session = Session() writer = self.ix.writer() for quote in session.query(Quote).all(): self.index_quote(quote, writer, commit=False) writer.commit()
def on_cmd_search(self, event): limit = 5 if not event.text: event.reply(u"Eh sì, stocazzo") return match = r_search_page.match(event.text) if match: page = int(match.group(1)) else: page = 1 session = Session() results_id = [] with self.ix.searcher() as searcher: qp = QueryParser("quote", self.ix.schema) query = qp.parse(event.text) # results = searcher.search(query, limit=limit) try: results = searcher.search_page(query, page, pagelen=limit) except ValueError: event.reply(u"La tua ricerca mi ha inibito il cervello") return found = results.scored_length() if not found: event.reply(u"Non ho trovato un cazzo!") return event.reply( u"Risultati pagina %d di %d, risultati %d-%d di %d" % ( results.pagenum, results.pagecount, results.offset + 1, results.offset + results.pagelen + 1, len(results), ) ) for result in results: results_id.append(result["id"]) for quote in session.query(Quote).filter(Quote.id.in_(results_id)).all(): event.reply(u"(%i) %s" % (quote.id, quote.quote))