elasticsearch.py 2.1 KB

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