PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/flax_search_service/docs/tutorial/python/t3_index_books.py

http://flaxcode.googlecode.com/
Python | 38 lines | 20 code | 9 blank | 9 comment | 5 complexity | 8982246057482959d2cfb07418ce7f61 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, GPL-2.0, BSD-3-Clause, AGPL-1.0
  1. import sys
  2. import re
  3. import flax.searchclient
  4. # open a connection to Flax Server
  5. conn = flax.searchclient.Client('http://localhost:8080')
  6. # get a reference to the database
  7. db = conn.db('books')
  8. # create a regexp to parse book data
  9. bookfield_re = re.compile('(\w+):\s+(.+)')
  10. # read books data from file
  11. f = open(sys.argv[1])
  12. # documents are dicts with fieldnames as keys and strings (or lists of
  13. # strings) as values
  14. doc = {}
  15. for line in f:
  16. match = bookfield_re.match(line)
  17. if match:
  18. name = match.group(1)
  19. value = match.group(2)
  20. if name == 'isbn':
  21. # add the document, using ISBN as the ID
  22. db.add_document(doc, value)
  23. doc = {}
  24. else:
  25. # add the field to the document
  26. doc[name] = value
  27. f.close()
  28. # commit the documents to the database
  29. db.flush()