gpt4 book ai didi

python - 如何使用 whoosh 获取所有 "index data"

转载 作者:行者123 更新时间:2023-11-28 18:55:25 26 4
gpt4 key购买 nike

我使用 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%}

最佳答案

此解决方案已在 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 %}

关于python - 如何使用 whoosh 获取所有 "index data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3104718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com