elasticsearch.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. from typing import Optional, Union
  3. from embedchain.config.vectordb.base import BaseVectorDbConfig
  4. from embedchain.helpers.json_serializable import register_deserializable
  5. @register_deserializable
  6. class ElasticsearchDBConfig(BaseVectorDbConfig):
  7. def __init__(
  8. self,
  9. collection_name: Optional[str] = None,
  10. dir: Optional[str] = None,
  11. es_url: Union[str, list[str]] = None,
  12. cloud_id: Optional[str] = None,
  13. **ES_EXTRA_PARAMS: dict[str, any],
  14. ):
  15. """
  16. Initializes a configuration class instance for an Elasticsearch client.
  17. :param collection_name: Default name for the collection, defaults to None
  18. :type collection_name: Optional[str], optional
  19. :param dir: Path to the database directory, where the database is stored, defaults to None
  20. :type dir: Optional[str], optional
  21. :param es_url: elasticsearch url or list of nodes url to be used for connection, defaults to None
  22. :type es_url: Union[str, list[str]], optional
  23. :param ES_EXTRA_PARAMS: extra params dict that can be passed to elasticsearch.
  24. :type ES_EXTRA_PARAMS: dict[str, Any], optional
  25. """
  26. if es_url and cloud_id:
  27. raise ValueError("Only one of `es_url` and `cloud_id` can be set.")
  28. # self, es_url: Union[str, list[str]] = None, **ES_EXTRA_PARAMS: dict[str, any]):
  29. self.ES_URL = es_url or os.environ.get("ELASTICSEARCH_URL")
  30. self.CLOUD_ID = cloud_id or os.environ.get("ELASTICSEARCH_CLOUD_ID")
  31. if not self.ES_URL and not self.CLOUD_ID:
  32. raise AttributeError(
  33. "Elasticsearch needs a URL or CLOUD_ID attribute, "
  34. "this can either be passed to `ElasticsearchDBConfig` or as `ELASTICSEARCH_URL` or `ELASTICSEARCH_CLOUD_ID` in `.env`" # noqa: E501
  35. )
  36. self.ES_EXTRA_PARAMS = ES_EXTRA_PARAMS
  37. # Load API key from .env if it's not explicitly passed.
  38. # Can only set one of 'api_key', 'basic_auth', and 'bearer_auth'
  39. if (
  40. not self.ES_EXTRA_PARAMS.get("api_key")
  41. and not self.ES_EXTRA_PARAMS.get("basic_auth")
  42. and not self.ES_EXTRA_PARAMS.get("bearer_auth")
  43. ):
  44. self.ES_EXTRA_PARAMS["api_key"] = os.environ.get("ELASTICSEARCH_API_KEY")
  45. super().__init__(collection_name=collection_name, dir=dir)