0

I am using the code, and I am getting as error message

This writer is closed

I am using whoosh and python. I am fetching data from a json file and I then iterate with a loop for creating the search engine index.

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()
B--rian
  • 5,578
  • 10
  • 38
  • 89

1 Answers1

0

w.commit() closes the writer, so you can do it like this:

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()
artona
  • 1,086
  • 8
  • 13