o
    hG                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- d dl.m/Z/ edkZ0G dd dZ1G dd dZ2G dd dZ3G dd deZ4G dd deZ5G dd dZ6G dd de6e4Z7G dd  d Z8G d!d" d"e8Z9G d#d$ d$eZ:e:Z;dS )%    N)OrderedDict)deepcopyurlparse)DEFAULT_DB_ALIASmodels)Query)MULTI)get_random_string)VERSION)ElasticsearchNotFoundError)bulk)BaseSearchBackendBaseSearchQueryCompilerBaseSearchResultsFilterFieldErrorget_model_root)AutocompleteFieldFilterFieldIndexedRelatedFieldsSearchFieldclass_is_indexedget_indexed_models)AndBoostFuzzyMatchAllNotOrPhrase	PlainText)deep_update)      c                   @   s"   e Zd ZdddZedd ZdS )Field   c                 C   s   || _ || _d S N)
field_nameboost)selfr)   r*    r,   f/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/search/backends/elasticsearch7.py__init__%   s   
zField.__init__c                 C   s"   | j dkr| jS | j d| j  S )Nr'   ^)r*   r)   r+   r,   r,   r-   field_name_with_boost)   s   
zField.field_name_with_boostN)r'   )__name__
__module____qualname__r.   propertyr1   r,   r,   r,   r-   r&   $   s    
r&   c                   @   s  e Zd ZdZdZi dddddddd	d
ddddddddddddddddddddddddddddddddddd	ZdZdZddd Zd!d" Z	d#d$ Z
d%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;S )<Elasticsearch7Mapping	_all_text_edgengrams	AutoFieldintegerSmallAutoFieldBigAutoFieldlongBinaryFieldbinaryBooleanFieldboolean	CharFieldstringCommaSeparatedIntegerField	DateFielddateDateTimeFieldDecimalFielddouble	FileFieldFilePathField
FloatFieldIntegerFieldBigIntegerFieldIPAddressFieldGenericIPAddressField)	NullBooleanFieldPositiveIntegerFieldPositiveSmallIntegerFieldPositiveBigIntegerField	SlugFieldSmallIntegerField	TextField	TimeFieldURLFieldkeywordtextedgengram_analyzerstandard)analyzersearch_analyzerc                 C   
   || _ d S r(   )model)r+   ra   r,   r,   r-   r.   Y      
zElasticsearch7Mapping.__init__c                 C   s8   | j jD ]}t|trt|tjrt| |  S qd S r(   )ra   	__bases__
issubclassr   r   Modeltype)r+   baser,   r,   r-   
get_parent\   s
   z Elasticsearch7Mapping.get_parentc                 C   s   dS )Ndocr,   r0   r,   r,   r-   get_document_typea   s   z'Elasticsearch7Mapping.get_document_typec                 C   s   t | j}|| j}||kr|jj d |j  d }nd}t|tr0||	| j d S t|t
r?||	| j d S t|trL||	| j S t|trV||j S d S )N___ _filterr8   )r   ra   get_definition_model_meta	app_labellowerr2   
isinstancer   get_attnamer   r   r   r)   )r+   field
root_modeldefinition_modelprefixr,   r,   r-   get_field_column_named   s*   






z+Elasticsearch7Mapping.get_field_column_namec                 C   s$   t t|dd}| j d| S )N.rk   _boost_)strfloatreplaceall_field_name)r+   r*   r,   r,   r-   get_boost_field_name   s   z*Elasticsearch7Mapping.get_boost_field_namec                 C   s   | j jjd | j j S )z
        Returns the content type as a string for the model.

        For example: "wagtailcore.Page"
                     "myapp.MyModel"
        rz   )ra   rp   rq   r2   r0   r,   r,   r-   get_content_type   s   z&Elasticsearch7Mapping.get_content_typec                 C   s4   |   g}|  }|r||   | }|s|S )a*  
        Returns all the content type strings that apply to this model.
        This includes the models' content type and all concrete ancestor
        models that inherit from Indexed.

        For example: ["myapp.MyPageModel", "wagtailcore.Page"]
                     ["myapp.MyModel"]
        )r   rh   append)r+   content_typesancestorr,   r,   r-   get_all_content_types   s   

z+Elasticsearch7Mapping.get_all_content_typesc           
      C   s.  t |tr1di d}|| jj}t| |}|jD ]}||\}}||d |< q| ||fS d| j	
|| jdi}t |trZ|d dkrN| j|d< |jrV|j|d< d|d< t |trk| j|d< || j nt |tr{|d dkr{| j|d< d	|jv r|jd	  D ]\}}	|	||< q| ||fS )
Nnested)rf   
propertiesr   rf   rC   r*   Tinclude_in_alles_extra)rs   r   	get_fieldra   related_modelrf   fieldsget_field_mappingry   type_mapgetget_typer   	text_typer*   r   updateedgengram_analyzer_configr   keyword_typekwargsitems)
r+   ru   mappingnested_modelnested_mapping	sub_fieldsub_field_namesub_field_mappingkeyvaluer,   r,   r-   r      s6   











z'Elasticsearch7Mapping.get_field_mappingc                    s   dj ddddj ijdjii}|j j j D ]}|\}}|||< q!ddi|j< t	  fdd  | D ]}ddi|
|< qGd	|iS )
NpkT)rf   storecontent_typerf   r[   c                    s   |   D ]9}d|v r1|d r.j|d< d|v r.|d  |d |d g|d< |d= |d= |d dkr= |d  qd S )Nr   copy_tor*   rf   r   r   )valuesr   addr   )r   field_mappingreplace_include_in_allr+   unique_boostsr,   r-   r      s   
zAElasticsearch7Mapping.get_mapping.<locals>.replace_include_in_allr   )r   edgengrams_field_namer   r   r   ra   get_search_fieldsr   r   setr   )r+   r   ru   r   valr*   r,   r   r-   get_mapping   s    

z!Elasticsearch7Mapping.get_mappingc                 C   s
   t |jS r(   )r|   r   )r+   objr,   r,   r-   get_document_id   rb   z%Elasticsearch7Mapping.get_document_idc           	      C   sZ   i }g }t |}t | |}|D ]}||}||||< t|tr(|| q||fS r(   )rf   	get_valuery   rs   r   r   )	r+   r   r   ri   
edgengramsra   r   ru   r   r,   r,   r-   _get_nested_document   s   


z*Elasticsearch7Mapping._get_nested_documentc           
      C   sF  t |j|  d}g }| j D ]}||}t|trZt|tj	tj
frEg }| D ]}| |j|\}}	|| ||	 q,|}nEt|tjrY| |j|\}}	||	 n0t|trt|tj	tj
frrt|jddd}nt|tjr||j}nt|ttfrdd |D }||| |< t|tr|| q||| j< |S )N)r   r   r   T)flatc                 S   s"   g | ]}t |tjr|jn|qS r,   )rs   r   re   r   ).0itemr,   r,   r-   
<listcomp>(  s    z6Elasticsearch7Mapping.get_document.<locals>.<listcomp>)r|   r   r   ra   r   r   rs   r   r   ManagerQuerySetallr   r   r   extendre   r   listvalues_listtuplery   r   r   )
r+   r   ri   r   ru   r   nested_docs
nested_obj
nested_docextra_edgengramsr,   r,   r-   get_document
  sF   







z"Elasticsearch7Mapping.get_documentc                 C   s   d| j j dS )Nz<ElasticsearchMapping: >)ra   r2   r0   r,   r,   r-   __repr__8     zElasticsearch7Mapping.__repr__N)r2   r3   r4   r   r   r   r   r   r   r.   rh   rj   ry   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   r6   1   s    	
	'/.r6   c                   @   s   e Zd Zdd Zerdd Zdd Zdd Znd	d Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zer>dd Zndd Zdd Zdd Zdd ZdS ) Elasticsearch7Indexc                 C   s    || _ |j| _|j| _|| _d S r(   )backendesmapping_classname)r+   r   r   r,   r,   r-   r.   =  s   
zElasticsearch7Index.__init__c                 C   s"   | j jjdd| ji| jj d S )Nindexr,   r   indicescreater   r   settingsr0   r,   r,   r-   putE  s   "zElasticsearch7Index.putc                 C   s.   z| j jj| jd W d S  ty   Y d S w Nr   r   r   deleter   r   r0   r,   r,   r-   r   H  s
   zElasticsearch7Index.deletec                 C   s   | j jj| jd d S r   r   r   refreshr   r0   r,   r,   r-   r   N  s   zElasticsearch7Index.refreshc                 C   s   | j j| j| jj d S r(   r   r0   r,   r,   r-   r   S  s   c                 C   s,   z| j j| j W d S  ty   Y d S w r(   r   r0   r,   r,   r-   r   V  s
   c                 C   s   | j j| j d S r(   r   r0   r,   r,   r-   r   \  s   c                 C   s   | j j| jS r(   )r   r   existsr   r0   r,   r,   r-   r   _  r   zElasticsearch7Index.existsc                 C   s   | j jj| jdS )Nr   )r   r   exists_aliasr   r0   r,   r,   r-   is_aliasb  s   zElasticsearch7Index.is_aliasc                    s$    fdd j jj jd D S )aK  
        If this index object represents an alias (which appear the same in the
        Elasticsearch API), this method can be used to fetch the list of indices
        the alias points to.

        Use the is_alias method if you need to find out if this an alias. This
        returns an empty list if called on an index.
        c                    s   g | ]
} j  j |qS r,   )r   index_class)r   
index_namer0   r,   r-   r   n      z7Elasticsearch7Index.aliased_indices.<locals>.<listcomp>r   )r   r   	get_aliasr   keysr0   r,   r0   r-   aliased_indicese  s   
	z#Elasticsearch7Index.aliased_indicesc                 C   s   | j jj|| jd dS )z|
        Creates a new alias to this index. If the alias already exists it will
        be repointed to this index.
        )r   r   N)r   r   	put_aliasr   )r+   r   r,   r,   r-   r   s  s   zElasticsearch7Index.put_aliasc                 C   s&   |  |}| jjj| j| d d S )Nr   body)r   r   r   put_mappingr   r   )r+   ra   r   r,   r,   r-   	add_modelz  s   
zElasticsearch7Index.add_modelc                 C   >   t |jsd S | |j}| jj| j||||d d S )N)r   documentidr   	__class__r   r   r   r   r   r   r+   r   r   r,   r,   r-   add_item  s   

zElasticsearch7Index.add_itemc                 C   r   )N)r   r   r   r,   r,   r-   r     s   

c                 C   sb   t |sd S | |}g }|D ]}d||i}||| || qt| j|| jd d S )N_idr   )	r   r   r   r   r   r   r   r   r   )r+   ra   r   r   actionsr   actionr,   r,   r-   	add_items  s   
zElasticsearch7Index.add_itemsc                 C   sN   t |jsd S | |j}z| jj| j||d W d S  ty&   Y d S w )N)r   r   )r   r   r   r   r   r   r   r   r   r,   r,   r-   delete_item  s   
zElasticsearch7Index.delete_itemc                 C   s   |    |   d S r(   )r   r   r0   r,   r,   r-   reset  s   zElasticsearch7Index.resetN)r2   r3   r4   r.   use_new_elasticsearch_apir   r   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   r   <  s(    

r   c                       s   e Zd ZeZdZ fddZdd Zdd Zdd	 Z	d
d Z
d%ddZdd Zdd Zd%ddZdd Zd%ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Z  ZS )&!Elasticsearch7SearchQueryCompilerorc                    s4   t  j|i | | | jj| _| | j| _d S r(   )	superr.   r   querysetra   r   _remap_fieldsr   remapped_fields)r+   argsr   r   r,   r-   r.     s   z*Elasticsearch7SearchQueryCompiler.__init__c           	         s   g }|r,dd    D }|D ]}||}|r) j|}|t||jp&d q|S |t jj t }t	 }|D ]}t
| jjsGq=| D ]}|jrX|t|j qKq=| fdd|D  |S )z;Convert field names into index column names and add boosts.c                 S      i | ]}|j |qS r,   r)   r   fr,   r,   r-   
<dictcomp>      zCElasticsearch7SearchQueryCompiler._remap_fields.<locals>.<dictcomp>r'   c                    s   g | ]}t  j||qS r,   )r&   r   r   )r   r*   r0   r,   r-   r     s    zCElasticsearch7SearchQueryCompiler._remap_fields.<locals>.<listcomp>)get_searchable_fieldsr   r   ry   r   r&   r*   r   r   r   rd   r   ra   get_searchable_search_fieldsr   r}   r   )	r+   r   r   searchable_fieldsr)   ru   r   r   ra   r,   r0   r-   r     s6   

z/Elasticsearch7SearchQueryCompiler._remap_fieldsc           
      C   s  | j |}|dkr|d u rdd|iiS d||iiS |dkr.dd|ii}|r,dd|ii}|S |d	v r8d
||iiS |dv rDd|||iiiS |dkrU|\}}d|||diiS |dkrt|tru| jjpct}||jt	d}	dd |	D }n	t|t
s~t
|}d||iiS d S )Nexactmissingru   termisnullr   boolmustNot)
startswithrx   rx   )gtgteltlterange)r  r  in)result_typec                 S   s   g | ]}|D ]}|d  qqS )r   r,   )r   chunkrowr,   r,   r-   r   .  s    zEElasticsearch7SearchQueryCompiler._process_lookup.<locals>.<listcomp>terms)r   ry   rs   r   r   _dbr   get_compilerexecute_sqlr	   r   )
r+   ru   lookupr   column_namequeryrr   upperdb_alias	resultsetr,   r,   r-   _process_lookup  sh   	

z1Elasticsearch7SearchQueryCompiler._process_lookupc                 C   s   dddi iiiS )Nr	  r
  	match_allr,   r0   r,   r,   r-   _process_match_none8  r   z5Elasticsearch7SearchQueryCompiler._process_match_nonec                 C   sp   |r6t |dkr|d }n|dkrdddd |D ii}n|dkr,dd	d
d |D ii}|r4dd|ii}|S d S )Nr'   r   ANDr	  mustc                 S      g | ]}|d ur|qS r(   r,   r   filr,   r,   r-   r   A      zFElasticsearch7SearchQueryCompiler._connect_filters.<locals>.<listcomp>ORshouldc                 S   r$  r(   r,   r%  r,   r,   r-   r   E  r'  r
  )len)r+   filters	connectornegated
filter_outr,   r,   r-   _connect_filters;  s   
z2Elasticsearch7SearchQueryCompiler._connect_filters      ?c                 C   s   d|j i}|jdkr|j|d< t|dkr2|dks |d jdkr)||d j |d< d|d j|iiS |dkr:||d< d	d
 |D |d< d|iS )Nr  r   operatorr'   r0  r   r*   matchc                 S      g | ]}|j qS r,   r1   r   ru   r,   r,   r-   r   Z      zNElasticsearch7SearchQueryCompiler._compile_plaintext_query.<locals>.<listcomp>r   multi_matchquery_stringr1  r*  r*   r)   )r+   r  r   r*   match_queryr,   r,   r-   _compile_plaintext_queryM  s   


z:Elasticsearch7SearchQueryCompiler._compile_plaintext_queryc                 C   st   |j dd}|jdkr|j|d< t|dkr-|d jdkr$|d j|d< d	|d j|iiS d
d |D |d< d|iS )NAUTO)r  	fuzzinessr   r1  r'   r   r0  r*   r2  c                 S   r3  r,   r4  r5  r,   r,   r-   r   l  r6  zJElasticsearch7SearchQueryCompiler._compile_fuzzy_query.<locals>.<listcomp>r   r7  r8  )r+   r  r   r:  r,   r,   r-   _compile_fuzzy_query^  s   

z6Elasticsearch7SearchQueryCompiler._compile_fuzzy_queryc                 C   sj   t |dkr'|d jdkrd|d j|j|d jdiiS d|d j|jiiS d|jdd |D d	d
iS )Nr'   r   r0  match_phrase)r  r*   r7  c                 S   r3  r,   r4  r5  r,   r,   r-   r     r6  zKElasticsearch7SearchQueryCompiler._compile_phrase_query.<locals>.<listcomp>phrase)r  r   rf   )r*  r*   r)   r9  )r+   r  r   r,   r,   r-   _compile_phrase_queryo  s    	z7Elasticsearch7SearchQueryCompiler._compile_phrase_queryc                    s  t |tri } dkr |d< d|iS t |tr(dd fdd|jD iiS t |tr=dd fd	d|jD iiS t |trNdd
|j iiS t |tr[	|g S t |t
rg|gS t |trs|gS t |tr|j |j S td|jj )Nr0  r*   r   r	  r#  c                       g | ]	} | qS r,   _compile_queryr   child_queryr*   ru   r+   r,   r-   r         zDElasticsearch7SearchQueryCompiler._compile_query.<locals>.<listcomp>r)  c                    rB  r,   rC  rE  rG  r,   r-   r     rH  r
  z:`%s` is not supported by the Elasticsearch search backend.)rs   r   r   
subqueriesr    r   rD  subqueryr"   r;  r   r>  r!   rA  r   r*   NotImplementedErrorr   r2   )r+   r  ru   r*   match_all_queryr,   rG  r-   rD    sF   


	
	



z0Elasticsearch7SearchQueryCompiler._compile_queryc                    s    j r j }n jjg}t|dkrdddi iiiS t jtr$di iS t jtr1  j|S t jt	r> 
 j|S t jtrK  j|S t jtr^dd fdd|D iiS   j|S )Nr   r	  r
  r   c                    s   g | ]
}   jj|qS r,   )rD  r  rJ  r5  r0   r,   r-   r     r   zEElasticsearch7SearchQueryCompiler.get_inner_query.<locals>.<listcomp>)r   r   r   r*  rs   r  r   r"   r;  r!   rA  r   r>  r   _join_and_compile_queriesr+   r   r,   r0   r-   get_inner_query  s*   

z1Elasticsearch7SearchQueryCompiler.get_inner_queryc                 C   sL   t |dkr| ||d |S g }|D ]}|| ||| qdd|iiS )Nr'   r   dis_maxqueries)r*  rD  r   )r+   r  r   r*   field_queriesru   r,   r,   r-   rM    s   z;Elasticsearch7SearchQueryCompiler._join_and_compile_queriesc                 C   s   |  | jj }dd|iiS )Nr2  r   )r   r   ra   r   )r+   r   r,   r,   r-   get_content_type_filter  s   z9Elasticsearch7SearchQueryCompiler.get_content_type_filterc                 C   s$   |   g}|  }|r|| |S r(   )rS  _get_filters_from_querysetr   )r+   r+  queryset_filtersr,   r,   r-   get_filters  s
   

z-Elasticsearch7SearchQueryCompiler.get_filtersc                 C   sL   |   }|  }t|dkrd||d diS t|dkr$d||diS |S )Nr'   r	  r   )r#  filter)rO  rV  r*  )r+   inner_queryr+  r,   r,   r-   	get_query  s   z+Elasticsearch7SearchQueryCompiler.get_queryc                 C   s   | j j S r(   )r   ra   r  r0   r,   r,   r-   r    s   z7Elasticsearch7SearchQueryCompiler.get_searchable_fieldsc                 C   sT   | j rd S | jjr'g }|  D ]\}}| j|}|||r dndi q|S dgS )Ndescascr   )order_by_relevancer   ordered_get_order_byr   ry   r   )r+   sortreverseru   r  r,   r,   r-   get_sort  s   z*Elasticsearch7SearchQueryCompiler.get_sortc                 C   s   t |  S r(   )jsondumpsrY  r0   r,   r,   r-   r   *     z*Elasticsearch7SearchQueryCompiler.__repr__)r0  )r2   r3   r4   r6   r   DEFAULT_OPERATORr.   r   r  r!  r/  r;  r>  rA  rD  rO  rM  rS  rV  rY  r  ra  r   __classcell__r,   r,   r   r-   r     s(     G

4
&r   c                   @   sT   e Zd ZdZdZdd ZdddZdd	 Zerd
d Z	ndd Z	dd Z
dd ZdS )Elasticsearch7SearchResultsstored_fieldsTc                 C   s   | j |}|d u r td| d | d | j jjj d |d|  }| j j|}|d|ddii|d	< | j	|| j
| j jjjdd
}tdd |d	 | d D S )Nz(Cannot facet search results with field "z!". Please add index.FilterField('z') to z.search_fields.r   r  r   )ru   r  aggregations)r   sizec                 S   s,   g | ]}|d  dkr|d  nd|d fqS )r   r   N	doc_countr,   )r   bucketr,   r,   r-   r   X  s    z5Elasticsearch7SearchResults.facet.<locals>.<listcomp>buckets)query_compiler_get_filterable_fieldr   r   ra   r2   _get_es_bodyr   ry   _backend_do_searchr   get_index_for_modelr   r   )r+   r)   ru   r   r  responser,   r,   r-   facet2  sP   

z!Elasticsearch7SearchResults.facetFc                 C   s0   d| j  i}|s| j  }|d ur||d< |S )Nr  r_  )rn  rY  ra  )r+   	for_countr   r_  r,   r,   r-   rp  ^  s   
z(Elasticsearch7SearchResults._get_es_bodyc                 c   s    dd |D }dd |D }dd |D }| j jj|dD ]}||t|j< | jr7t|| j|t|j q|D ]}|t| }|rG|V  q:dS )z]
        Yields Django model instances from a page of hits returned by Elasticsearch
        c                 S   s   g | ]
}|d  d d qS )r   r   r   r,   r   hitr,   r,   r-   r   n  s    zFElasticsearch7SearchResults._get_results_from_hits.<locals>.<listcomp>c                 S   s&   i | ]}t |d  d d |d qS )r   r   r   _scorer|   rv  r,   r,   r-   r   o  s   & zFElasticsearch7SearchResults._get_results_from_hits.<locals>.<dictcomp>c                 S   s   i | ]}t |d qS r(   ry  )r   r   r,   r,   r-   r   r      )pk__inN)rn  r   rW  r|   r   _score_fieldsetattrr   )r+   hitspksscoresresultsr   r   resultr,   r,   r-   _get_results_from_hitsi  s   z2Elasticsearch7SearchResults._get_results_from_hitsc                 K   s   | j jjdi ||S )Nr,   r   r   searchr+   r   r   r,   r,   r-   rq    s   z.Elasticsearch7SearchResults._backend_do_searchc                 K   s   | j jjdd|i|S )Nr   r,   r  r  r,   r,   r-   rq    s   c           
      c   s   d}| j d ur| j | j }nd }|d u p||k}|  }d| j| jjjjdd| j	di}|r|
d|d | j}| j|fi |}	 |d	 d	 }t|d
krRnM|t|k r| |D ] }	|d uri|d
kri n|d
kry|	V  |d urx|d8 }q]|d8 }q]|d ur|d
krnn|t|8 }d|vrn| jjj|d dd}qEd|v r| jjj|d d d S d S |
| j|p|d | j|fi |d	 d	 }| |D ]}	|	V  qd S )Nd   r   _sourceFr   2m)scrollrj  Tr~  r   r'   
_scroll_id)	scroll_idr  )r  )from_rj  )stopstartrp  r   rr  rn  r   ra   r   fields_param_namer   rq  r*  r  r   r  clear_scroll)
r+   	PAGE_SIZElimit
use_scrollr   paramsskippager~  r  r,   r,   r-   
_do_search  sp   

!z&Elasticsearch7SearchResults._do_searchc                 C   s^   | j jj| j | jjjj| jdddd }|| j	8 }| j
d ur*t|| j
| j	 }t|dS )NT)ru  r   countr   )r   r   r  rr  rn  r   ra   r   rp  r  r  minmax)r+   	hit_countr,   r,   r-   	_do_count  s   



z%Elasticsearch7SearchResults._do_countN)F)r2   r3   r4   r  supports_facetrt  rp  r  r   rq  r  r  r,   r,   r,   r-   rg  .  s    
,
Srg  c                       s$   e Zd Z fddZdd Z  ZS )*ElasticsearchAutocompleteQueryCompilerImplc                    st   t  j|i | | jr5g }dd | jj D }| jD ]}||v r*| j|| }|| q|| _	d S d | _	d S )Nc                 S   r   r,   r   r   r,   r,   r-   r     s    zGElasticsearchAutocompleteQueryCompilerImpl.__init__.<locals>.<dictcomp>)
r   r.   r   r   ra   get_autocomplete_search_fieldsr   ry   r   r   )r+   r   r   r   autocomplete_fieldsr)   r   r,   r-   r.     s   



z3ElasticsearchAutocompleteQueryCompilerImpl.__init__c                 C   sz   | j p| jjg}dd |D }t|dkrdddi iiiS t| jtr*| | j|S t| jtr4di iS t	d| jj
j )Nc                 S      g | ]}t |qS r,   )r&   r5  r,   r,   r-   r     r  zNElasticsearchAutocompleteQueryCompilerImpl.get_inner_query.<locals>.<listcomp>r   r	  r
  r   z/`%s` is not supported for autocomplete queries.)r   r   r   r*  rs   r  r"   r;  r   rK  r   r2   rN  r,   r,   r-   rO  
  s   z:ElasticsearchAutocompleteQueryCompilerImpl.get_inner_query)r2   r3   r4   r.   rO  rf  r,   r,   r   r-   r    s    r  c                   @   s   e Zd ZdS )'Elasticsearch7AutocompleteQueryCompilerN)r2   r3   r4   r,   r,   r,   r-   r    s    r  c                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ElasticsearchIndexRebuilderc                 C   r`   r(   r   r+   r   r,   r,   r-   r.   "  rb   z$ElasticsearchIndexRebuilder.__init__c                 C      | j   d S r(   )r   r   r0   r,   r,   r-   reset_index%  rd  z'ElasticsearchIndexRebuilder.reset_indexc                 C   s   |    | jS r(   )r  r   r0   r,   r,   r-   r  (  s   z!ElasticsearchIndexRebuilder.startc                 C   r  r(   )r   r   r0   r,   r,   r-   finish.  rd  z"ElasticsearchIndexRebuilder.finishNr2   r3   r4   r.   r  r  r  r,   r,   r,   r-   r  !  s
    r  c                   @   r  )
!ElasticsearchAtomicIndexRebuilderc                 C   s0   || _ |j|j| j jd td  | _d S )Nrk   r$   )aliasr   r   r   r
   rr   r   r  r,   r,   r-   r.   3  s   
z*ElasticsearchAtomicIndexRebuilder.__init__c                 C   s(   | j   | j  | j| j j d S r(   )r  r   r   r   r   r   r0   r,   r,   r-   r  9  s   

z-ElasticsearchAtomicIndexRebuilder.reset_indexc                 C   s   | j   | j S r(   )r   r   r0   r,   r,   r-   r  D  s   
z'ElasticsearchAtomicIndexRebuilder.startc                 C   sp   | j   | j r)| j }| j | jj |D ]}|j| j jkr&|  qd S | j  | j | jj d S r(   )r   r   r  r   r   r   r   r   )r+   	old_indexr   r,   r,   r-   r  J  s   



z(ElasticsearchAtomicIndexRebuilder.finishNr  r,   r,   r,   r-   r  2  s
    r  c                       s   e Zd ZeZeZeZe	Z
eZeZeZdZdZdddddgddddd	gdd
ddddddddddddddddddddddidiZdd Zdd Z fddZdd  Zd!d" Zd#d$ Zd%d& Z  ZS )'Elasticsearch7SearchBackendTtimeoutr   custom	lowercaseasciifoldingngram)rf   	tokenizerrW  	edgengram)ngram_analyzerr\      r%   )rf   min_grammax_gram
edge_ngram   front)rf   r  r  side)ngram_tokenizeredgengram_tokenizerr'   )r  r  )r^   r  rW  max_ngram_diff   )analysisr   c                 C   sV   |j dk}|jp|rdnd}d}|jdur |jdur |j|jf}|j||j|||dS )zKGiven a parsed URL, return the host configuration to be added to self.hostshttpsi  P   N)hostport
url_prefixuse_sslverify_certs	http_auth)schemer  usernamepasswordhostnamepath)r+   urlr  r  r  r,   r,   r-   _get_host_config_from_url  s   
z5Elasticsearch7SearchBackend._get_host_config_from_urlc                 C   s   i S )zGiven a list of parsed URLs, return a dict of additional options to be passed into the
        Elasticsearch constructor; necessary for options that aren't valid as part of the 'hosts' config
        r,   )r+   urlsr,   r,   r-   _get_options_from_host_urls  s   z7Elasticsearch7SearchBackend._get_options_from_host_urlsc                    s  t  | |dd  _|dd _|dd _|ddr& j _n j _t	 j
 _
t j
|di  _
|d	i } jd u rn|d
dg}t|trU|g}dd |D } fdd|D  _| |  j| j< tdd ji| _d S )NHOSTSINDEXwagtailTIMEOUT
   ATOMIC_REBUILDFINDEX_SETTINGSOPTIONSURLSzhttp://localhost:9200c                 S   r  r,   r   r   r  r,   r,   r-   r     r  z8Elasticsearch7SearchBackend.__init__.<locals>.<listcomp>c                    s   g | ]}  |qS r,   )r  r  r0   r,   r-   r     rz  hostsr,   )r   r.   popr  r   r  atomic_rebuilder_classrebuilder_classbasic_rebuilder_classr   r   r#   rs   r|   r   r  timeout_kwarg_namer   r   )r+   r  optionses_urlsparsed_urlsr   r0   r-   r.     s*   


z$Elasticsearch7SearchBackend.__init__c                 C   s8   t |}d|jj  d |j  }| | | j| S )Nrl   rk   )r   rp   rq   rr   r2   r   r   )r+   ra   rv   index_suffixr,   r,   r-   rr    s   
z/Elasticsearch7SearchBackend.get_index_for_modelc                 C   s   |  | | jS r(   )r   r   r0   r,   r,   r-   	get_index  rd  z%Elasticsearch7SearchBackend.get_indexc                 C   s   |  |  S r(   )r  r  r0   r,   r,   r-   get_rebuilder  rd  z)Elasticsearch7SearchBackend.get_rebuilderc                 C   s   |     d S r(   )r  r  r0   r,   r,   r-   r    s   z'Elasticsearch7SearchBackend.reset_index)r2   r3   r4   r6   r   r   r   r   query_compiler_classr  !autocomplete_query_compiler_classrg  results_classr  r  r  r  catch_indexing_errorsr  r   r  r  r.   rr  r  r  r  rf  r,   r,   r   r-   r  j  sZ    

 '&r  )<rb  collectionsr   copyr   urllib.parser   	django.dbr   r   django.db.models.sqlr   django.db.models.sql.constantsr	   django.utils.cryptor
   elasticsearchr   ELASTICSEARCH_VERSIONr   r   elasticsearch.helpersr   wagtail.search.backends.baser   r   r   r   r   wagtail.search.indexr   r   r   r   r   r   r   wagtail.search.queryr   r   r   r   r   r    r!   r"   wagtail.utils.utilsr#   r   r&   r6   r   r   rg  r  r  r  r  r  SearchBackendr,   r,   r,   r-   <module>   sH    $(	     h E
)8 
