Как получить все данные индекса с помощью whoosh

Я использую 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%}

person zjm1126    schedule 23.06.2010    source источник
comment
Возможно, вы получите больше, лучше и быстрее ответы, задав список рассылки Whoosh по адресу groups.google.com/. group/whoosh и вы уверены, что это не описано в документации? packages.python.org/Whoosh   -  person Jason Hall    schedule 23.06.2010
comment
Чего вы на самом деле пытаетесь достичь? Попытка получить все данные индекса в запросе — странная вещь.   -  person Nick Johnson    schedule 24.06.2010
comment
возможно, вам поможет следующая ссылка: stackoverflow.com/questions/2395675/whoosh-index-viewer   -  person Steve Harrison    schedule 20.07.2014


Ответы (1)


Это решение протестировано на Whoosh 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 %}
person bigOther    schedule 26.05.2015