API

Parsing queries and matching dictionary data

This is the core of the library, which can be used to match dictionaries against a query.

querydict.parser

This module implements the core of querydict, in the QueryEngine class. Sample usage:

>>> from querydict.parser import QueryEngine
>>> data = { "name", "Bob" }
>>> query = QueryEngine("name:Bob")
>>> query.match("data")  # True
exception querydict.parser.MatchException

Exception raised when matching fails, for example if input dictionary contains keys with the wrong data type for the specified query.

class querydict.parser.QueryEngine(query: str, short_circuit: bool = True, ambiguous_action: str = 'AND', allow_bare_field: bool = False, max_depth: int = 10)

Match a Lucene style query against dict data, using an abstract tree parser.

Parameters:
  • query – A Lucene style query
  • short_circuit – Whether to terminate matching early inside AND or OR conditions (default: True)
  • ambiguous_action – The action to use for ambiguous queries, for example “field1:value1 field2:value2” (default: “AND”)
  • allow_bare_field – Whether to allow a search term without a specified field, for example “value1” (default: False).
  • max_depth – The maximum recursion depth when parsing a query (default: 10).
Raises:

QueryException – If the input query is too complex, or uses unsupported features.

match(data: dict, default_field: str = None) → bool

Match a dictionary against the configured query.

Parameters:
  • data – A dictionary containing fields and values to match against.
  • default_field – The name of a field to use for unqualified values.
Returns:

True if there is a match, False otherwise

Raises:

MatchException – If there is a problem with the input data dictionary.

exception querydict.parser.QueryException

Exception raised when parsing a query string fails, for example if the query is invalid or uses unsupported features.