
    dh                     j    S r SSKJr  SSKrSSKJrJrJr  SSKJ	r	  SSK
Jr  SSKJr   " S S	\5      rg)
z-Wrapper around Elasticsearch vector database.    )annotationsN)AnyIterableList)CallbackManagerForRetrieverRun)Document)BaseRetrieverc                      \ rS rSr% SrS\S'    S\S'    \ S         SS jj5       r S     SS jjr      SS	 jr	S
r
g)ElasticSearchBM25Retriever   a  `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                    SSK Jn  U" U5      nSSSS000SSUUS	.0S
.nSSSSS.00nUR                  R                  X(US9  U " XbS9$ )a  
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	            j/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/retrievers/elastic_search_bm25.pyr$   !ElasticSearchBM25Retriever.create,   s      	0 ,- $i&*1E%FG" 	
 ""/
 	


Q"44    c                    SSK Jn  / n/ n[        U5       HV  u  pg[	        [
        R                  " 5       5      nSU R                  UUS.n	UR                  U5        UR                  U	5        MX     U" U R                  U5        U(       a-  U R                  R                  R                  U R                  S9  U$ ! [         a    [        S5      ef = f)zRun 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_texts$ElasticSearchBM25Retriever.add_textsY   s    	2  'GAdjjl#C#//	G JJsOOOG$ ( 	T[[(#KK''doo'>
+  	F 	s   B: :Cc                   SSSU000nU R                   R                  U R                  US9n/ nUS   S    H!  nUR                  [	        US   S   S95        M#     U$ )Nquerymatchr   )r   bodyhits_source)page_content)r   searchr   r5   r   )r7   rA   run_manager
query_dictresdocsrs          r(   _get_relevant_documents2ElasticSearchBM25Retriever._get_relevant_documents   sl     )U);<=
kk  tZ HVV$AKKa	l9.EFG %r*    N)g       @g      ?)
r&   r   r   r   r   floatr   rP   returnr   )T)r8   zIterable[str]r9   boolrQ   z	List[str])rA   r   rH   r   rQ   zList[Document])__name__
__module____qualname____firstlineno____doc____annotations__classmethodr$   r>   rM   __static_attributes__rO   r*   r(   r   r      s    2 KO4RV*5 #*514*5:?*5JO*5	#*5 *5^ !%%% % 
	%N		*H			r*   r   )rW   
__future__r   r3   typingr   r   r   langchain_core.callbacksr   langchain_core.documentsr   langchain_core.retrieversr	   r   rO   r*   r(   <module>r`      s+    3 "  & & C - 3| |r*   