def get_es(**overrides): """Return one pyes.es.ES object :arg overrides: Allows you to override defaults to create the ES. Things you can override: * default_indexes * timeout * dump_curl Values for these correspond with the arguments to pyes.es.ES. For example, if you wanted to create an ES for indexing with a timeout of 30 seconds, you'd do: >>> es = get_es(timeout=30) If you wanted to create an ES for debugging that dumps curl commands to stdout, you could do: >>> class CurlDumper(object): ... def write(self, s): ... print s ... >>> es = get_es(dump_curl=CurlDumper()) """ if overrides or not hasattr(_local, 'es'): defaults = { 'default_indexes': DEFAULT_INDEXES, 'timeout': DEFAULT_TIMEOUT, 'dump_curl': DEFAULT_DUMP_CURL, } defaults.update(overrides) if (not thrift_enable and not settings.ES_HOSTS[0].split(':')[1].startswith('92')): raise ValueError('ES_HOSTS is not set to a valid port starting ' 'with 9200-9299 range. Other ports are valid ' 'if using pythrift.') es = ES(settings.ES_HOSTS, **defaults) # pyes 0.15 does this lame thing where it ignores dump_curl in # the ES constructor and always sets it to None. So what we do # is set it manually after the ES has been created and # defaults['dump_curl'] is truthy. This might not work for all # values of dump_curl. if VERSION[0:2] == (0, 15): es.dump_curl = (defaults['dump_curl'] if defaults['dump_curl'] else None) # Cache the es if there weren't any overrides. if not overrides: _local.es = es else: es = _local.es return es
def get_es(hosts=None, default_indexes=None, timeout=None, dump_curl=None, **settings): """Create an ES object and return it. :arg hosts: list of uris; ES hosts to connect to, defaults to ``['localhost:9200']`` :arg default_indexes: list of strings; the default indexes to use, defaults to 'default' :arg timeout: int; the timeout in seconds, defaults to 5 :arg dump_curl: function or None; function that dumps curl output, see docs, defaults to None :arg settings: other settings to pass into `pyes.es.ES` Examples: >>> es = get_es() >>> es = get_es(hosts=['localhost:9200']) >>> es = get_es(timeout=30) # good for indexing >>> es = get_es(default_indexes=['sumo_prod_20120627'] >>> class CurlDumper(object): ... def write(self, text): ... print text ... >>> es = get_es(dump_curl=CurlDumper()) """ # Cheap way of de-None-ifying things hosts = hosts or DEFAULT_HOSTS default_indexes = default_indexes or DEFAULT_INDEXES timeout = timeout if timeout is not None else DEFAULT_TIMEOUT dump_curl = dump_curl or DEFAULT_DUMP_CURL if not isinstance(default_indexes, list): default_indexes = [default_indexes] es = ES(hosts, default_indexes=default_indexes, timeout=timeout, dump_curl=dump_curl, **settings) # pyes 0.15 does this lame thing where it ignores dump_curl in # the ES constructor and always sets it to None. So what we do # is set it manually after the ES has been created and # defaults['dump_curl'] is truthy. This might not work for all # values of dump_curl. if PYES_VERSION[0:2] == (0, 15) and dump_curl is not None: es.dump_curl = dump_curl return es