我正在使用代码,但收到错误消息
此作者已关闭
我正在使用whoosh和python。我正在从json文件中获取数据,然后使用循环来创建搜索引擎索引。
from whoosh.fields import Schema,TEXT,ID
from whoosh import index
from whoosh.qparser import QueryParser
import os.path
import json
if not os.path.exists("indexdir"):
os.mkdir("indexdir")
schema = Schema(title=TEXT(stored=True), content=TEXT(stored=True))
ix = index.create_in("indexdir", schema)
doc_json=json.load(open("review.json",'r'))
for doc in doc_json:
with ix.writer() as w:
for key,value in doc.get('properties').items():
w.add_document(title=str(key), content=str(value[0].get('value')))
w.commit()
w.commit()关闭编写器,因此您可以这样做:
with ix.writer() as w:
for doc in doc_json:
for key,value in doc.get('properties').items():
w.add_document(title=str(key), content=str(value[0].get('value')))
w.commit()