In [1]:
import elasticsearch
from elasticsearch import Elasticsearch
from jsmin import jsmin
import os
In [3]:
%install_ext https://raw.githubusercontent.com/szeitlin/watermark/master/watermark.py
In [4]:
%load_ext watermark
In [5]:
%watermark -a "Samantha Zeitlin" -d -u -p elasticsearch,jsmin,python
In [6]:
es = Elasticsearch()
In [8]:
try:
es.indices.create(index='official_test')
except elasticsearch.RequestError as re:
print re
In [17]:
current_map = es.indices.get_mapping(index='official_test', doc_type='notebook')
current_map
Out[17]:
In [14]:
with open('mapping_files/mapping_official.json', 'r') as map:
body = jsmin(map.read())# + '\n'
#the trick is that the mapping starts at the level of the document type, not the index, and doesn't need the word
#'mappings' in it, as was shown in some (misleading!) examples
try:
es.indices.put_mapping(index ='official_test',doc_type='notebook',body=body)
except elasticsearch.RequestError as re:
print re
In [97]:
print es.indices.put_mapping.__doc__
In [108]:
path = '/Users/szeitlin/mystuff/projects/nbindex/nbindex/testdir'
prefix = '{"index": {"_index": "official_test", "_type":"notebook"}}\n'
newline = '\n'
with open('test_temp.json', 'wb') as body:
for eachfile in os.listdir(path):
fullname = os.path.join(path,eachfile)
with open(fullname,'r') as each:
body.write(prefix)
body.write(jsmin(each.read()) + newline)
In [110]:
#for some reason, looks like test3 mapping 'sort of' worked, not sure about official_test mapping yet
#next try adding files that actually do contain png
path = '/Users/szeitlin/mystuff/projects/nbindex/nbindex/pngdir'
prefix = '{"index": {"_index": "official_test", "_type":"notebook"}}\n'
newline = '\n'
with open('png_temp.json', 'wb') as body:
for eachfile in os.listdir(path):
fullname = os.path.join(path,eachfile)
with open(fullname,'r') as each:
body.write(prefix)
body.write(jsmin(each.read()) + newline)
with open('png_temp.json', 'r') as body:
obj = body.read()
es.bulk(obj, index='official_test', doc_type='notebook')
In [109]:
with open('test_temp.json', 'r') as body:
obj = body.read()
es.bulk(obj, index='official_test', doc_type='notebook')
In [34]:
print es.cat.help()
In [15]:
print es.cat.indices()
In [ ]:
In [ ]: