def search_article(request): start_time = time.time() from search.whoosh_schema import ArticleSchema query = request.GET.get("q") or "" data = {} ix = get_whoosh_ix("article", ArticleSchema) with ix.searcher() as searcher: parser = qparser.QueryParser("content", schema=ix.schema, group=qparser.OrGroup) search_expression = parser.parse(query) # app.logger.info("search_expression: %s", search_expression) results = searcher.search(search_expression, limit=None) # result_list = filter(None, [Article.objects.filter(id=r["article_id"]).first() \ # for r in results]) data["articles"] = [] for r in results: data["articles"].append(dict(title=r["title"], id=r["article_id"])) data["meta"] = { "total_time": time.time() - start_time, } return data
def make_keyword_index(self): from search.whoosh_schema import ArticleSchema ix = get_whoosh_ix("article", ArticleSchema) writer = ix.writer() writer.add_document(content=unicode(self.content), article_id=unicode(self.id), title=unicode(self.title)) writer.commit()