def main():
    # make sure we are using the same database as the rest of the code
    from dbapi import conn
    cursor = conn.cursor()

    searchindex.create_table(conn, True)

    # do the search function
    results = search(cursor, conn, input('Q? '))
    print()
    for result in results:
        print(Story.find('id', result[0])[0].title, '-->', result[1])

    conn.close()
# import logging
import template
from dbapi import conn
from dbapi.user import User
from dbapi.story import Story
from collections import defaultdict
from utils import BaseRequestHandler
from search import search, load_index
from dbapi.searchindex import create_table

create_table(conn, if_exists=False)


class SearchStories(BaseRequestHandler):
    def get(self):
        context = defaultdict()

        user = self.get_secure_cookie('username')
        stories = Story.find('all', '')

        try:
            context.update({
                'current_user': User.find('username', str(user, 'utf-8'))[0]})
        except TypeError:
            context.update({
                'current_user': None})

        cursor = conn.cursor()

        if self.get_arguments('storyquery') != []:
            query = self.get_argument('storyquery')