
    h                     j    d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ  G d d	e      Zy)
z-Wrapper around Elasticsearch vector database.    )annotationsN)AnyIterableList)CallbackManagerForRetrieverRun)Document)BaseRetrieverc                  |    e Zd ZU dZded<   	 ded<   	 e	 d
	 	 	 	 	 	 	 	 	 dd       Z	 d	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy	)ElasticSearchBM25Retrievera?  `Elasticsearch` retriever that uses `BM25`.

    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.
    r   clientstr
index_namec                    ddl m}  ||      }ddddiiidd||d	id
}dddddii}|j                  j                  |||        | ||      S )aD  
        Create a ElasticSearchBM25Retriever from a list of texts.

        Args:
            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:

        r   )Elasticsearchanalyzerdefaulttypestandardcustom_bm25BM25)r   k1b)analysis
similarity
propertiescontenttext)r   r   )indexmappingssettings)r   r   )elasticsearchr   indicescreate)	clselasticsearch_urlr   r   r   r   esr    r   s	            p/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/retrievers/elastic_search_bm25.pyr#   z!ElasticSearchBM25Retriever.create,   s      	0 ,- $i&*1E%FG" 	
 ""/
 	


XQ"44    c                   	 ddl m} g }g }t        |      D ]U  \  }}t	        t        j                               }d| j                  ||d}	|j                  |       |j                  |	       W  || j                  |       |r0| j                  j                  j                  | j                         |S # t        $ r t        d      w xY w)a.  Run more texts through the embeddings and add to the retriever.

        Args:
            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.
        r   )bulkzbCould not import elasticsearch python package. Please install it with `pip install elasticsearch`.r   )_op_type_indexr   _id)r   )elasticsearch.helpersr*   ImportError	enumerater   uuiduuid4r   appendr   r"   refresh)
selftextsrefresh_indicesr*   requestsidsir   r-   requests
             r'   	add_textsz$ElasticSearchBM25Retriever.add_textsY   s    	2  ' 		%GAtdjjl#C#//	G JJsOOOG$		% 	T[[(#KK''doo'>
+  	F 	s   B6 6Cc                   ddd|iii}| j                   j                  | j                  |      }g }|d   d   D ]#  }|j                  t	        |d   d                % |S )Nquerymatchr   )r   bodyhits_source)page_content)r   searchr   r3   r   )r5   r>   run_manager
query_dictresdocsrs          r'   _get_relevant_documentsz2ElasticSearchBM25Retriever._get_relevant_documents   ss     )U);<=
kk  tZ HVV$ 	HAKKa	l9.EFG	Hr(   N)g       @g      ?)
r%   r   r   r   r   floatr   rK   returnr   )T)r6   zIterable[str]r7   boolrL   z	List[str])r>   r   rE   r   rL   zList[Document])	__name__
__module____qualname____doc____annotations__classmethodr#   r<   rJ    r(   r'   r   r      s    2 KO4RV*5 #*514*5:?*5JO*5	#*5 *5^ !%%% % 
	%N		*H			r(   r   )rQ   
__future__r   r1   typingr   r   r   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr	   r   rT   r(   r'   <module>rZ      s+    3 "  & & C - 3| |r(   