Files
opensearch-pyd/elasticsearch/client/__init__.py
T
2013-07-10 14:07:33 +02:00

105 lines
4.6 KiB
Python

from ..transport import Transport
from .indices import IndicesClient
from .cluster import ClusterClient
from .utils import query_params, _make_path
def _normalize_hosts(hosts):
"""
Helper function to transform hosts argument to
:class:`~elasticsearch.Elasticsearch` to a list of dicts.
"""
# if hosts are empty, just defer to defaults down the line
if hosts is None:
return [{}]
out = []
# normalize hosts to dicts
for i, host in enumerate(hosts):
if isinstance(host, (type(''), type(u''))):
h = {"host": host}
if ':' in host:
# TODO: detect auth urls
host, port = host.rsplit(':', 1)
if port.isdigit():
port = int(port)
h = {"host": host, "port": port}
out.append(h)
else:
out.append(host)
return out
class Elasticsearch(object):
"""
Elasticsearch low-level client. Provides a straightforward mapping from
Python to ES REST endpoints.
"""
def __init__(self, hosts=None, transport_class=Transport, **kwargs):
"""
:arg hosts: list of nodes we should connect to. Node should be a
dictionary ({"host": "localhost", "port": 9200}), the entire dictionary
will be passed to the :class:`~elasticsearch.Connection` class as
kwargs, or a string in the format ot ``host[:port]`` which will be
translated to a dictionary automatically. If no value is given the
:class:`~elasticsearch.Connection` class defaults will be used.
:arg transport_class: :class:`~elasticsearch.Transport` subclass to use.
:arg kwargs: any additional arguments will be passed on to the
:class:`~elasticsearch.Transport` class and, subsequently, to the
:class:`~elasticsearch.Connection` instances.
"""
self.transport = transport_class(_normalize_hosts(hosts), **kwargs)
# namespaced clients for compatibility with API names
self.indices = IndicesClient(self)
self.cluster = ClusterClient(self)
@query_params('consistency', 'id', 'parent', 'percolate', 'refresh', 'replication', 'routing', 'timeout', 'timestamp', 'ttl', 'version', 'version_type')
def create(self, index, doc_type, body, id=None, params=None):
"""
The index API adds or updates a typed JSON document in a specific index, making it searchable.
http://elasticsearch.org/guide/reference/api/index_/
:arg index: The name of the index
:arg doc_type: The type of the document
:arg id: Document ID
:arg body: The document
:arg consistency: Explicit write consistency setting for the operation
:arg id: Specific document ID (when the POST method is used)
:arg parent: ID of the parent document
:arg percolate: Percolator queries to execute while indexing the document
:arg refresh: Refresh the index after performing the operation
:arg replication: Specific replication type, default u'sync'
:arg routing: Specific routing value
:arg timeout: Explicit operation timeout
:arg timestamp: Explicit timestamp for the document
:arg ttl: Expiration time for the document
:arg version: Explicit version number for concurrency control
:arg version_type: Specific version type
"""
params['op_type'] = 'create'
status, data = self.transport.perform_request('PUT' if id else 'POST', _make_path(index, doc_type, id), params=params, body=body)
return data
@query_params('fields', 'parent', 'preference', 'realtime', 'refresh', 'routing')
def get(self, index, doc_type, id, params=None):
"""
The get API allows to get a typed JSON document from the index based on its id.
http://elasticsearch.org/guide/reference/api/get/
:arg index: The name of the index
:arg doc_type: The type of the document (use `_all` to fetch the first document matching the ID across all types)
:arg id: The document ID
:arg fields: A comma-separated list of fields to return in the response
:arg parent: The ID of the parent document
:arg preference: Specify the node or shard the operation should be performed on (default: random)
:arg realtime: Specify whether to perform the operation in realtime or search mode
:arg refresh: Refresh the shard containing the document before performing the operation
:arg routing: Specific routing value
"""
status, data = self.transport.perform_request('GET', _make_path(index, doc_type, id), params=params)
return data