this is my code :
#coding:utf-8
import cgi,os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from whoosh import store
from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
from whoosh.index import getdatastoreindex,create_in
from whoosh.qparser import QueryParser, MultifieldParser
from whoosh.analysis import RegexAnalyzer
import logging
#analyzer = RegexAnalyzer(ur"([\u4e00-\u9fa5])|(\w+(\.?\w+)*)")
SEARCHSCHEMA = Schema(content=TEXT(stored=True))
class BaseRequestHandler(webapp.RequestHandler):
def render_template(self, filename, template_args=None):
if not template_args:
template_args = {}
path = os.path.join(os.path.dirname(__file__), 'templates', filename)
self.response.out.write(template.render(path, template_args))
class MainPage(BaseRequestHandler):
def get(self):
self.render_template('index.html')
class SearchPage(BaseRequestHandler):
def get(self):
ix = getdatastoreindex("hello", schema=SEARCHSCHEMA)
#ix = create_in("indexdir", schema=SEARCHSCHEMA)
parser = QueryParser("content", schema = ix.schema)
#q = parser.parse(u"%s" % self.request.get('query'))
q = parser.parse("%s" % u'靠'.encode('utf-8'))
a=''
#raise Exception(self.request.get('query').encode('utf-8'))
#if searcher!=None:
results = ix.searcher().search(query=q)
for result in results:
a+=('<blockquote>%s</blockquote>' %
cgi.escape(result['content']))
self.render_template('index.html',{'results':a})
class Guestbook(BaseRequestHandler):
def post(self):
ix = getdatastoreindex("hello", schema=SEARCHSCHEMA)
#content=TEXT(stored=True, analyzer=analyzer)
#ix = create_in("indexdir", schema=SEARCHSCHEMA)
writer = ix.writer()
#writer.add_document(content=u"%s" % self.request.get('content'))
writer.add_document(content=self.request.get('content'))
writer.commit()
self.redirect('/')
application = webapp.WSGIApplication(
[('/', MainPage),
('/search', SearchPage),
('/sign', Guestbook)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()