langchain.retrievers.elastic_search_bm25.ElasticSearchBM25Retriever¶

class langchain.retrievers.elastic_search_bm25.ElasticSearchBM25Retriever(*, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, client: Any = None, index_name: str)[source]¶

Bases: BaseRetriever

Retriever for the Elasticsearch using BM25 as a retrieval method.

To connect to an Elasticsearch instance that requires login credentials, including Elastic Cloud, use the Elasticsearch URL format https://username:password@es_host:9243. For example, to connect to Elastic Cloud, create the Elasticsearch URL with the required authentication details and pass it to the ElasticVectorSearch constructor as the named parameter elasticsearch_url.

You can obtain your Elastic Cloud URL and login credentials by logging in to the Elastic Cloud console at https://cloud.elastic.co, selecting your deployment, and navigating to the “Deployments” page.

To obtain your Elastic Cloud password for the default “elastic” user:

  1. Log in to the Elastic Cloud console at https://cloud.elastic.co

  2. Go to “Security” > “Users”

  3. Locate the “elastic” user and click “Edit”

  4. Click “Reset password”

  5. Follow the prompts to reset the password

The format for Elastic Cloud URLs is https://username:password@cluster_id.region_id.gcp.cloud.es.io:9243.

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param client: Any = None¶

Elasticsearch client.

param index_name: str [Required]¶

Name of the index to use in Elasticsearch.

param metadata: Optional[Dict[str, Any]] = None¶

Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a retriever with its use case.

param tags: Optional[List[str]] = None¶

Optional list of tags associated with the retriever. Defaults to None These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks. You can use these to eg identify a specific instance of a retriever with its use case.

add_texts(texts: Iterable[str], refresh_indices: bool = True) List[str][source]¶

Run more texts through the embeddings and add to the retriever.

Parameters
  • texts – Iterable of strings to add to the retriever.

  • refresh_indices – bool to refresh ElasticSearch indices

Returns

List of ids from adding the texts into the retriever.

async aget_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document]¶

Asynchronously get documents relevant to a query. :param query: string to find relevant documents for :param callbacks: Callback manager or list of callbacks :param tags: Optional list of tags associated with the retriever. Defaults to None

These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Parameters

metadata – Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Returns

List of relevant documents

async ainvoke(input: str, config: Optional[RunnableConfig] = None) List[Document]¶
classmethod create(elasticsearch_url: str, index_name: str, k1: float = 2.0, b: float = 0.75) ElasticSearchBM25Retriever[source]¶

Create a ElasticSearchBM25Retriever from a list of texts.

Parameters
  • elasticsearch_url – URL of the Elasticsearch instance to connect to.

  • index_name – Name of the index to use in Elasticsearch.

  • k1 – BM25 parameter k1.

  • b – BM25 parameter b.

Returns:

get_relevant_documents(query: str, *, callbacks: Callbacks = None, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) List[Document]¶

Retrieve documents relevant to a query. :param query: string to find relevant documents for :param callbacks: Callback manager or list of callbacks :param tags: Optional list of tags associated with the retriever. Defaults to None

These tags will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Parameters

metadata – Optional metadata associated with the retriever. Defaults to None This metadata will be associated with each call to this retriever, and passed as arguments to the handlers defined in callbacks.

Returns

List of relevant documents

invoke(input: str, config: Optional[RunnableConfig] = None) List[Document]¶
to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
to_json_not_implemented() SerializedNotImplemented¶
property lc_attributes: Dict¶

Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.

property lc_namespace: List[str]¶

Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]

property lc_secrets: Dict[str, str]¶

Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}

property lc_serializable: bool¶

Return whether or not the class is serializable.

model Config¶

Bases: object

Configuration for this pydantic object.

arbitrary_types_allowed = True¶

Examples using ElasticSearchBM25Retriever¶