qparser module

Parser classes

class whoosh.qparser.QueryParser(default_field, schema=None, conjunction=<class 'whoosh.query.And'>, termclass=<class 'whoosh.query.Term'>)

The default parser for Whoosh, implementing a powerful fielded query language similar to Lucene’s.

Parameters:
  • default_field – Use this as the field for any terms without an explicit field. For example, if the query string is “hello f1:there” and the default field is “f2”, the parsed query will be as if the user had entered “f2:hello f1:there”. This argument is required.
  • conjuction – Use this query.Query class to join together clauses where the user has not explictly specified a join. For example, if this is query.And, the query string “a b c” will be parsed as “a AND b AND c”. If this is query.Or, the string will be parsed as “a OR b OR c”.
  • termclass – Use this query.Query class for bare terms. For example, query.Term or query.Variations.
  • schema – An optional fields.Schema object. If this argument is present, the appropriate field will be used to tokenize terms/phrases before they are turned into query objects.
parse(input, normalize=True)

Parses the input string and returns a Query object/tree.

This method may return None if the input string does not result in any valid queries. It may also raise a variety of exceptions if the input string is malformed.

Parameters:
  • input – the unicode string to parse.
  • 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.
Return type:

whoosh.query.Query

class whoosh.qparser.MultifieldParser(fieldnames, schema=None, conjunction=<class 'whoosh.query.And'>, termclass=<class 'whoosh.query.Term'>)
A subclass of QueryParser. Instead of assigning unfielded clauses to a default field, this class 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.
class whoosh.qparser.SimpleParser(fieldname, schema=None, termclass=<class 'whoosh.query.Term'>, phraseclass=<class 'whoosh.query.Phrase'>, minmatch=0, minpercent=0.75, phrasefields=None)
class whoosh.qparser.SimpleNgramParser(fieldname, minchars, maxchars, discardspaces=False, analyzerclass=<function NgramAnalyzer at 0x1928668>)

A simple parser that only allows searching a single Ngram field. Breaks the input text into grams. It can either discard grams containing spaces, or compose them as optional clauses to the query.

Parameters:
  • fieldname – The field to search.
  • minchars – The minimum gram size the text was indexed with.
  • maxchars – The maximum gram size the text was indexed with.
  • discardspaces – If False, grams containing spaces are made into optional clauses of the query. If True, grams containing spaces are ignored.
  • analyzerclass – An analyzer class. The default is the standard NgramAnalyzer. The parser will instantiate this analyzer with the gram size set to the maximum usable size based on the input string.

Table Of Contents

Previous topic

postings module

Next topic

query module

This Page