2010-06-23 12 views
2

我用whoosh全文搜索,如何讓所有的「指標數據」使用嗖

,我想知道:如何讓已添加的所有「指標數據」。

這是我main.py

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 
from whoosh.qparser import QueryParser, MultifieldParser 
import logging 

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) 
    parser = QueryParser("content", schema = ix.schema) 
    q = parser.parse(self.request.get('query')) 
    results = ix.searcher().search(q) 
    a='' 
    for result in results: 
     a+=('<blockquote>%s</blockquote>' % 
           cgi.escape(result['content'])) 
    all=ix.schema 
    self.render_template('index.html',{'results':a,'all':all}) 

class Guestbook(BaseRequestHandler): 
    def post(self): 
    ix = getdatastoreindex("hello", schema=SEARCHSCHEMA) 
    writer = ix.writer() 
    writer.add_document(content=u"%s" % 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() 

而且我index.html是:

<form action="/search" method="get"> 
<div><input name="query" type="text" value=""><input type="submit" value="Search"></div> 
</form> 


<form action="/sign" method="post"> 
<div><textarea name="content" rows="3" cols="60"></textarea></div> 
<div><input type="submit" value="Sign Guestbook"></div> 
</form> 

{{results}} 

all data: 


{{all}} 
{% for i in all%} 
{{i}} 
{%endfor%} 
+1

你可能會得到更多,更好,更快和答案通過詢問嗖郵件列表在http://groups.google.com/ group/whoosh,你確定這不在文檔中涵蓋? http://packages.python.org/Whoosh/ – 2010-06-23 19:45:07

+0

你究竟在努力實現什麼?試圖在請求中獲得索引數據是一件很奇怪的事情。 – 2010-06-24 08:25:53

+0

可能下面的鏈接可能會幫助你:http://stackoverflow.com/questions/2395675/whoosh-index-viewer – 2014-07-19 22:02:47

回答

0

該解決方案上嗖2.7進行測試,但可能在以前的版本中工作,以及

你可以列出所有結果:

all_docs = ix.searcher().documents() 

在模板中,你可以遍歷它們喜歡:

{% for doc in all_docs %} 
    {{ doc.content }} <!-- or any doc.field as field is in your schema --> 
{% endfor %} 
相關問題