Advertisement
Guest User

Untitled

a guest
May 8th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import pyes
  5. import cPickle
  6.  
  7.  
  8. INDEX_NAME = "test_index"
  9. INDEX_TYPE = "test_type"
  10.  
  11. def multi_field(name):
  12.     return {
  13.         name: {
  14.            "type" : "string"
  15.         },
  16.         "untouched" : {
  17.             "index" : "not_analyzed",
  18.             "type": "string"
  19.         }
  20.     }
  21.  
  22. conn = pyes.ES("localhost:9200", bulk_size=2000, autorefresh=True)
  23.  
  24. # create index
  25. conn.delete_index_if_exists(INDEX_NAME)
  26. conn.create_index(INDEX_NAME)
  27.  
  28. # create mapping
  29. mapping = {u'author': {"type": "multi_field", "fields": multi_field('author')}}
  30. conn.put_mapping(INDEX_TYPE, {'properties': mapping}, indexes=[INDEX_NAME])
  31.  
  32. for doc in cPickle.load(open('authors.pkl')):
  33.     doc['target'] = INDEX_TYPE
  34.     conn.index(doc, INDEX_NAME, INDEX_TYPE, bulk=True)
  35. conn.refresh() # flushes bulk & refreshes all indexes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement