<s> import time <EOL> import uuid <EOL> import json <EOL> from base64 import b64encode <EOL> from tensor import utils <EOL> class ElasticSearch ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url = '<STR_LIT>' , user = None , password = None , <EOL> index = '<STR_LIT>' ) : <EOL> self . url = url . rstrip ( '<STR_LIT:/>' ) <EOL> self . index = index <EOL> self . user = user <EOL> self . password = password <EOL> def _get_index ( self ) : <EOL> return time . strftime ( self . index ) <EOL> def _request ( self , path , data = None , method = '<STR_LIT:GET>' ) : <EOL> headers = { } <EOL> if self . user : <EOL> authorization = b64encode ( '<STR_LIT>' % ( self . user , self . password ) ) <EOL> headers [ '<STR_LIT>' ] = [ '<STR_LIT>' + authorization ] <EOL> return utils . HTTPRequest ( ) . getJson ( <EOL> self . url + path , method , headers = headers , data = data ) <EOL> def _gen_id ( self ) : <EOL> return b64encode ( uuid . uuid4 ( ) . bytes ) . rstrip ( '<STR_LIT:=>' ) <EOL> def stats ( self ) : <EOL> return self . _request ( '<STR_LIT>' ) <EOL> def node_stats ( self ) : <EOL> return self . _request ( '<STR_LIT>' ) <EOL> def insertIndex ( self , type , data ) : <EOL> return self . _request ( '<STR_LIT>' % ( <EOL> self . _get_index ( ) , type , self . _gen_id ( ) <EOL> ) , json . dumps ( data ) , '<STR_LIT>' ) <EOL> def bulkIndex ( self , data ) : <EOL> serdata = "<STR_LIT>" <EOL> for row in data : <EOL> if '<STR_LIT>' in row : <EOL> id = row [ '<STR_LIT:id>' ] <EOL> del row [ '<STR_LIT:id>' ] <EOL> else : <EOL> id = self . _gen_id ( ) <EOL> d = { <EOL> "<STR_LIT:index>" : { <EOL> "<STR_LIT>" : self . _get_index ( ) , <EOL> "<STR_LIT>" : row . get ( '<STR_LIT:type>' , '<STR_LIT>' ) , <EOL> "<STR_LIT>" : id , <EOL> } <EOL> } <EOL> serdata += json . dumps ( d ) + '<STR_LIT:\n>' <EOL> serdata += json . dumps ( row ) + '<STR_LIT:\n>' <EOL> return self . _request ( '<STR_LIT>' , serdata . rstrip ( '<STR_LIT:\n>' ) , '<STR_LIT>' ) </s>
