예제 #1
0
파일: views.py 프로젝트: MYCLRY/random-read
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
예제 #2
0
 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()