pandagg.tree.query module

class pandagg.tree.query.Query(q=None, mappings=None, nested_autocorrect=False)[source]

Bases: pandagg.tree._tree.Tree

applied_nested_path_at_node(nid)[source]

Return nested path applied at a clause.

Parameters:nid – clause identifier
Returns:None if no nested is applied, else applied path (str)
bool(must=None, should=None, must_not=None, filter=None, insert_below=None, on=None, mode='add', **body)[source]
>>> Query().bool(must={"term": {"some_field": "yolo"}})
boosting(positive=None, negative=None, insert_below=None, on=None, mode='add', **body)[source]
constant_score(filter=None, boost=None, insert_below=None, on=None, mode='add', **body)[source]
dis_max(queries, insert_below=None, on=None, mode='add', **body)[source]
filter(type_or_query, insert_below=None, on=None, mode='add', bool_body=None, **body)[source]
function_score(query, insert_below=None, on=None, mode='add', **body)[source]
has_child(query, insert_below=None, on=None, mode='add', **body)[source]
has_parent(query, insert_below=None, on=None, mode='add', **body)[source]
must(type_or_query, insert_below=None, on=None, mode='add', bool_body=None, **body)[source]

Create copy of initial Query and insert provided clause under “bool” query “must”.

>>> Query().must('term', some_field=1)
>>> Query().must({'term': {'some_field': 1}})
>>> from pandagg.query import Term
>>> Query().must(Term(some_field=1))
Keyword Arguments:
 
  • insert_below (str) – named query clause under which the inserted clauses should be placed.
  • compound_param (str) – param under which inserted clause will be placed in compound query
  • on (str) – named compound query clause on which the inserted compound clause should be merged.
  • mode (str one of ‘add’, ‘replace’, ‘replace_all’) – merging strategy when inserting clauses on a existing compound clause.
    • ‘add’ (default) : adds new clauses keeping initial ones
    • ‘replace’ : for each parameter (for instance in ‘bool’ case : ‘filter’, ‘must’, ‘must_not’, ‘should’), replace existing clauses under this parameter, by new ones only if declared in inserted compound query
    • ‘replace_all’ : existing compound clause is completely replaced by the new one
must_not(type_or_query, insert_below=None, on=None, mode='add', bool_body=None, **body)[source]
nested(path, query=None, insert_below=None, on=None, mode='add', **body)[source]
node_class

alias of pandagg.node.query.abstract.QueryClause

pinned_query(organic, insert_below=None, on=None, mode='add', **body)[source]
query(type_or_query, insert_below=None, on=None, mode='add', compound_param=None, **body)[source]

Insert provided clause in copy of initial Query.

>>> from pandagg.query import Query
>>> Query().query('term', some_field=23)
{'term': {'some_field': 23}}
>>> from pandagg.query import Term
>>> Query()\
>>> .query({'term': {'some_field': 23})\
>>> .query(Term(other_field=24))\
{'bool': {'must': [{'term': {'some_field': 23}}, {'term': {'other_field': 24}}]}}
Keyword Arguments:
 
  • insert_below (str) – named query clause under which the inserted clauses should be placed.
  • compound_param (str) – param under which inserted clause will be placed in compound query
  • on (str) – named compound query clause on which the inserted compound clause should be merged.
  • mode (str one of ‘add’, ‘replace’, ‘replace_all’) – merging strategy when inserting clauses on a existing compound clause.
    • ‘add’ (default) : adds new clauses keeping initial ones
    • ‘replace’ : for each parameter (for instance in ‘bool’ case : ‘filter’, ‘must’, ‘must_not’, ‘should’), replace existing clauses under this parameter, by new ones only if declared in inserted compound query
    • ‘replace_all’ : existing compound clause is completely replaced by the new one
script_score(query, insert_below=None, on=None, mode='add', **body)[source]
should(type_or_query, insert_below=None, on=None, mode='add', bool_body=None, **body)[source]
show(*args, line_max_length=80, **kwargs)[source]

Return compact representation of Query.

>>> Query()        >>> .must({"exists": {"field": "some_field"}})        >>> .must({"term": {"other_field": {"value": 5}}})        >>> .show()
<Query>
bool
└── must
    ├── exists                                                  field=some_field
    └── term                                          field=other_field, value=5

All *args and **kwargs are propagated to lighttree.Tree.show method. :return: str

to_dict(from_=None)[source]

Serialize Query as dict.