
    Ng                     r    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dS )
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dS ) 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_name       @      ?elasticsearch_urlk1floatbreturnc                    ddl m}  ||          }ddddiiidd||d	id
}dddddii}|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   r   r   )analysis
similarity
propertiescontenttext)r   r   )indexmappingssettings)r   r   )elasticsearchr   indicescreate)	clsr   r   r   r   r   esr%   r$   s	            n/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/retrievers/elastic_search_bm25.pyr(   z!ElasticSearchBM25Retriever.create,   s      	0///// ],-- $i&*1E%FG"   	
 	
 ""/ 
 	

XQQQs"4444    TtextsIterable[str]refresh_indicesbool	List[str]c                   	 ddl m} n# t          $ r t          d          w xY wg }g }t          |          D ][\  }}t	          t          j                              }d| j        ||d}	|                    |           |                    |	           \ || j	        |           |r%| j	        j
                            | j                   |S )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.helpersr3   ImportError	enumerater   uuiduuid4r   appendr   r'   refresh)
selfr-   r/   r3   requestsidsir"   r6   requests
             r+   	add_textsz$ElasticSearchBM25Retriever.add_textsY   s   	2222222 	 	 	F  	
  '' 		% 		%GAtdjll##C#/	 G JJsOOOOOG$$$$T[(### 	?K''do'>>>
s   	 #queryrun_managerr   List[Document]c                   ddd|iii}| j                             | j        |          }g }|d         d         D ]1}|                    t	          |d         d                              2|S )NrD   matchr!   )r#   bodyhits_source)page_content)r   searchr   r<   r   )r>   rD   rE   
query_dictresdocsrs          r+   _get_relevant_documentsz2ElasticSearchBM25Retriever._get_relevant_documents   s     )U);<=
k  tZ HHVV$ 	H 	HAKKa	l9.EFFFGGGGr,   N)r   r   )
r   r   r   r   r   r   r   r   r   r   )T)r-   r.   r/   r0   r   r1   )rD   r   rE   r   r   rF   )	__name__
__module____qualname____doc____annotations__classmethodr(   rC   rR    r,   r+   r   r      s          2 KKKOOO4RV*5 *5 *5 *5 [*5^ !%% % % % %N	 	 	 	 	 	r,   r   )rV   
__future__r   r:   typingr   r   r   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr	   r   rY   r,   r+   <module>r_      s    3 3 " " " " " "  & & & & & & & & & & C C C C C C - - - - - - 3 3 3 3 3 3| | | | | | | | | |r,   