
    =wgB                     l    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	  G d de
      ZddZd	 Zdd
Zy)    N)query)	text_type)syntax)print_debugQueryParserErrorc                       e Zd ZdZdej
                  ej                  ej                  fdZ	d Z
d Zd Zd Zd Zd	 Zd
 Zd Zd Z	 	 ddZd Zd ZddZddZddZddZddZy)QueryParsera  A hand-written query parser built on modular plug-ins. The default
    configuration implements a powerful fielded query language similar to
    Lucene's.

    You can use the ``plugins`` argument when creating the object to override
    the default list of plug-ins, and/or use ``add_plugin()`` and/or
    ``remove_plugin_class()`` to change the plug-ins included in the parser.

    >>> from whoosh import qparser
    >>> parser = qparser.QueryParser("content", schema)
    >>> parser.remove_plugin_class(qparser.WildcardPlugin)
    >>> parser.add_plugin(qparser.PrefixPlugin())
    >>> parser.parse(u"hello there")
    And([Term("content", u"hello"), Term("content", u"there")])
    Nc                     || _         || _        || _        || _        || _        g | _        || j                         }| j                          | j                  |       y)a  
        :param fieldname: the default field -- the parser uses this as the
            field for any terms without an explicit field.
        :param schema: a :class:`whoosh.fields.Schema` object to use when
            parsing. The appropriate fields in the schema will be used to
            tokenize terms/phrases before they are turned into query objects.
            You can specify None for the schema to create a parser that does
            not analyze the text of the query, usually for testing purposes.
        :param plugins: a list of plugins to use. WhitespacePlugin is
            automatically included, do not put it in this list. This overrides
            the default list of plugins. Classes in the list will be
            automatically instantiated.
        :param termclass: the query class to use for individual search terms.
            The default is :class:`whoosh.query.Term`.
        :param phraseclass: the query class to use for phrases. The default
            is :class:`whoosh.query.Phrase`.
        :param group: the default grouping. ``AndGroup`` makes terms required
            by default. ``OrGroup`` makes terms optional by default.
        N)		fieldnameschema	termclassphraseclassgrouppluginsdefault_set_add_ws_pluginadd_plugins)selfr   r   r   r   r   r   s          M/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/qparser/default.py__init__zQueryParser.__init__7   s\    , #"&
?&&(G!    c                 >   ddl m} |j                         |j                         |j	                         |j                         |j                         |j                         |j                         |j                         |j                         |j                         g
S )z4Returns the default list of plugins to use.
        r   )r   )whoosh.qparserr   WhitespacePluginSingleQuotePluginFieldsPluginWildcardPluginPhrasePluginRangePluginGroupPluginOperatorsPluginBoostPluginEveryPlugin)r   r   s     r   r   zQueryParser.default_setY   s     	+((*))+$$&&&($$&##%##%'')##%##%
 
	r   c                 4    |D ]  }| j                  |        y)zVAdds the given list of plugins to the list of plugins in this
        parser.
        N)
add_plugin)r   pinspins      r   r   zQueryParser.add_pluginsk   s    
  	!COOC 	!r   c                 h    t        |t              r |       }| j                  j                  |       y)zEAdds the given plugin to the list of plugins in this parser.
        N)
isinstancetyper   append)r   r'   s     r   r%   zQueryParser.add_plugins   s'     c4 %CC r   c                 <    ddl m} | j                   |              y )Nr   )r   )whoosh.qparser.pluginsr   r%   )r   r   s     r   r   zQueryParser._add_ws_plugin{   s    ;(*+r   c                 :    | j                   j                  |       y)zYRemoves the given plugin object from the list of plugins in this
        parser.
        N)r   remove)r   pis     r   remove_pluginzQueryParser.remove_plugin   s    
 	Br   c                 f    | j                   D cg c]  }t        ||      r| c}| _         yc c}w )zARemoves any plugins of the given class from this parser.
        N)r   r)   )r   clsr0   s      r   remove_plugin_classzQueryParser.remove_plugin_class   s&     &*\\MrB9LMMs   ..c                 \    | j                  |j                         | j                  |       y)a{  Removes any plugins of the class of the given plugin and then adds
        it. This is a convenience method to keep from having to call
        ``remove_plugin_class`` followed by ``add_plugin`` each time you want
        to reconfigure a default plugin.

        >>> qp = qparser.QueryParser("content", schema)
        >>> qp.replace_plugin(qparser.NotPlugin("(^| )-"))
        N)r4   	__class__r%   )r   plugins     r   replace_pluginzQueryParser.replace_plugin   s$     	  !1!12r   c                     g }| j                   D ],  }t        ||      } ||       D ]  }|j                  |        . |j                  d        |D cg c]  \  }}|	 c}}S c c}}w )Nc                     | d   S )N    )xs    r   <lambda>z(QueryParser._priorized.<locals>.<lambda>   s
    ! r   )key)r   getattrr+   sort)r   
methodnameitems_and_prioritiesr7   methoditem_s          r   
_priorizedzQueryParser._priorized   su      "ll 	2FVZ0Ft 2$++D12	2 	!!n!5$89q999s   A&c           
      n   |j                         }|dk(  r |||d   |      S |dk(  r| j                  |||      S |dk(  r| j                  j                  }n:|dk(  rt        j
                  }n$|dk(  rt        j                  }nt        d|z         ||D cg c]  } ||||       c}      S c c}w )	a  Returns a query for multiple texts. This method implements the
        intention specified in the field's ``multitoken_query`` attribute,
        which specifies what to do when strings that look like single terms
        to the parser turn out to yield multiple tokens when analyzed.

        :param spec: a string describing how to join the text strings into a
            query. This is usually the value of the field's
            ``multitoken_query`` attribute.
        :param texts: a list of token strings.
        :param fieldname: the name of the field.
        :param termclass: the query class to use for single terms.
        :param boost: the original term's boost in the query string, should be
            applied to the returned query object.
        firstr   boostphrasedefaultandorz!Unknown multitoken_query value %r)lowerr   r   qclassr   AndOrr   )r   spectextsr   r   rK   rQ   ts           r   multitoken_queryzQueryParser.multitoken_query   s      zz|7?Ya>>X##IuE#BBy **&'J)-(. / /$)+  %Y? + , , +s   B2c                    | j                   r|| j                   v r| j                   |   }|j                         r	 |j                  |||      }|S t        |j                  |d||            }
t        |
      dkD  r| j                  |j                  |
|||      S |
sy|
d   } ||||      S #  t        j                         d   }	t        j                  |	      cY S xY w)z\Returns the appropriate query object for a single term in the query
        string.
        rJ   r;   r   )modetokenizeremovestopsNr   )r   self_parsingparse_querysysexc_infor   error_querylistprocess_textlenrW   )r   r   textr   rK   rZ   r[   fieldqerU   s              r   
term_queryzQueryParser.term_query   s     ;;93KK	*E !!#0)))T)GAH ++Dw5=8C , E FE 5zA~,,U-C-CU-6	5J J
 8DD66/0q)A ,,Q//s   B/ /.Cc                 $    | j                  d      S )zrReturns a priorized list of tagger objects provided by the parser's
        currently configured plugins.
        taggersrG   r   s    r   rj   zQueryParser.taggers       
 y))r   c                 $    | j                  d      S )ztReturns a priorized list of filter functions provided by the
        parser's currently configured plugins.
        filtersrk   rl   s    r   ro   zQueryParser.filters   rm   r   c           
         g }|}| j                         }|rt        |d|z         fd}|t              k  rd}|D ]  }	|	j                  | |      }||j                  |k  rt        d|	dd|d      ||k  r+ |||      }
|rt        |d|
z         |j                  |
       |rt        |d	|	d
|d|       |j                  |       |j                  x}} n |s|dz  }|t              k  r|t              k  r!|j                   ||t                           | j                  |      }|rt        |d|z         |S )zReturns a group of syntax nodes corresponding to the given text,
        created by matching the Taggers provided by the parser's plugins.

        :param text: the text to tag.
        :param pos: the position in the text to start tagging at.
        zTaggers: %rc                 T    t        j                  | |       }| |_        ||_        |S N)r   WordNode	startcharendchar)rt   ru   nrd   s      r   interzQueryParser.tag.<locals>.inter  s+    Yw 78A#AKAIHr   NzToken z did not move cursor forward. (z, )z	Tween: %rzTagger: z at z: r;   zTagged group: %r)rj   r   rc   matchru   	Exceptionr+   r   )r   rd   posdebugstackprevrj   rw   nodetaggertweenr   s    `          r   tagzQueryParser.tag  sa    ,,.}w67	 CIoD! ||D$4#||s*'7=tS)J K Kcz %dC 0 '{U/BCU+#E'-sD,: ;LL&!%-D3#& q1 CIo6 #d)LLtSY/0 

5!1E9:r   c                     |rt        |d|z         | j                         D ]=  }|rt        |d|z          || |      }|rt        |d|z         |1t        d|z         |S )z^Takes a group of nodes and runs the filters provided by the parser's
        plugins.
        zPre-filtered group: %rz..Applying: %rz..Result: %rz!Filter %r did not return anything)r   ro   rz   )r   nodesr|   fs       r   	filterizezQueryParser.filterize>  s{     7%?@ 	IAE#3a#78dENEE>E#9:} Ca GHH	I r   c                 T    | j                  |||      }| j                  ||      }|S )zReturns a group of syntax nodes corresponding to the given text,
        tagged by the plugin Taggers and filtered by the plugin filters.

        :param text: the text to tag.
        :param pos: the position in the text to start tagging at.
        )r{   r|   r|   )r   r   )r   rd   r{   r|   r   s        r   processzQueryParser.processP  s/     3e4uE2r   c                 >   t        |t              s|j                  d      }| j                  ||      }|rt	        |d|z         |j                  |       }|st
        j                  }|rt	        |d|z         |r!|j                         }|rt	        |d|z         |S )a  Parses the input string and returns a :class:`whoosh.query.Query`
        object/tree.

        :param text: the unicode string to parse.
        :param normalize: whether to call normalize() on the query object/tree
            before returning it. This should be left on unless you're trying to
            debug the parser output.
        :rtype: :class:`whoosh.query.Query`
        latin1r   zSyntax tree: %rzPre-normalized query: %rzNormalized query: %r)r)   r   decoder   r   r   	NullQuery	normalize)r   rd   r   r|   r   rf   s         r   parsezQueryParser.parse\  s     $	*;;x(DT/0589KKA9A=>AE#9A#=>r   c                      y rr   r<   )r   rd   r   s      r   parse_zQueryParser.parse_z  s    r   )g      ?TT)r   F)F)TF)T)__name__
__module____qualname____doc__r   TermPhraser   AndGroupr   r   r   r%   r   r1   r4   r8   rG   rW   rh   rj   ro   r   r   r   r   r   r<   r   r   r	   r	   &   s      37%**"\\ "D$!!, N :",H JN#&7P**;z$
<r   r	   c                 b    ddl m} t        d|fi |} || |      }|j                  |       |S )a  Returns a QueryParser configured to search in multiple fields.

    Instead of assigning unfielded clauses to a default field, this parser
    transforms them into an OR clause that searches a list of fields. For
    example, if the list of multi-fields is "f1", "f2" and the query string is
    "hello there", the class will parse "(f1:hello OR f2:hello) (f1:there OR
    f2:there)". This is very useful when you have two textual fields (e.g.
    "title" and "content") you want to search by default.

    :param fieldnames: a list of field names to search.
    :param fieldboosts: an optional dictionary mapping field names to boosts.
    r   )MultifieldPluginN)fieldboosts)r-   r   r	   r%   )
fieldnamesr   r   kwargsr   pmfps          r   MultifieldParserr     s5     8D&+F+A
:;
?CLLHr   c                     ddl m}m} |j                  |j                  |j
                  g}|j                  }t        | |f||d|S )zRReturns a QueryParser configured to support only +, -, and phrase
    syntax.
    r   r   r   r   r   )r   r   r   r   PlusMinusPluginr   OrGroupr	   )r   r   r   r   r   r&   orgroups          r   SimpleParserr     sV    
 /$$##  "D nnGy& !$g !! !r   c                     ddl m}m} |j                  t	        | j                               | |j                        }|j                  |j                  |j                  |g}|j                  }t        d|f||d|S )zReturns a QueryParser configured to support only +, -, and phrase
    syntax, and which converts individual terms into DisjunctionMax queries
    across a set of fields.

    :param fieldboosts: a dictionary mapping field names to boosts.
    r   r   )r   r   Nr   )r   r   r   r   ra   keysDisMaxGroupr   r   r   r   r	   )	r   r   tiebreakr   r   r   r   r&   r   s	            r   DisMaxParserr     s     /

"
"4(8(8(:#;/:)/);); # =C $$##  D nnGtVKTKFKKr   rr   )g        )r^   whooshr   whoosh.compatr   r   r   whoosh.qparser.commonr   r   objectr	   r   r   r   r<   r   r   <module>r      s5   8   # ! ?
U& Ut
,!Lr   