:orphan:

===========================
Connecting to Elasticsearch
===========================

Data Science & AI Workbench enables you to connect to an Elasticsearch NoSQL database to access JSON data stored there.

Before you can do so, however, you'll need to import the library that's required to connect to Elasticsearch.::

   conda install elasticsearch

When you use ``conda install`` to add a package during a session, the project is impacted temporarily, *during the current session only*. If you want the change to persist for future project sessions and deployments, be sure to add the package to the project's ``anaconda-project.yml`` file. For more information, see :doc:`../projects/project-config`.

After you've installed the correct driver, you can then use code such as this to access the cluster from within a notebook session:

.. code-block:: python

   from elasticsearch import Elasticsearch
   
   # Connection String
   es = Elasticsearch([{'host': 'elasticsearch.dev.anaconda.com', 'port': 9200}])
   
   # A sample get query
   es.get(index='sw', doc_type='people', id=65)

   # A sample search
   es.search(index="sw", body={"query": {"prefix" : { "name" : "lu" }}})
   
   # An advanced search example
   es.search(index="sw", body={"query": {"fuzzy_like_this_field" : { "name" : {"like_text": "jaba", "max_query_terms":5}}}})

For more advanced connection options, such as SSL authentication, see the `Elasticsearch documentation <https://elasticsearch-py.readthedocs.io/en/master/#ssl-and-authentication>`_.

See :doc:`Secrets <../secrets>` for information about adding credentials to the platform, to make them available in your projects. Any secrets you add will be available across all sessions and deployments associated with your user account.