Skip to content

Queries

Mark Papadakis edited this page May 27, 2017 · 2 revisions

Queries are predicates, expressions the can be made up of tokens, phrases, logical operators and specialised operators specific to the Trinity’s query language (although its fairly easy to extend Query to support other application or domain specific expression extensions), and parenthesis. Here are a few simple examples:

  • world of warcraft
  • (world OR worlds) of “war craft” OR warcraft OR warcraft (book NOT (low fantasy)) AND “mists of pandaria” NOT “frozen king”
  • sf OR “san francisco” pizza OR pizzas cheese pepperoni NOT (pickles OR pickle)

You can use AND, NOT, OR and parenthesis to construct queries. You can prefix a token or phrase with either + or - to specify that you explicitly want it to be matched or you want to explicitly not match that term or phrase. You probably don’t need to know more about queries, after all, we all use Google, Bing and other search engines.

In Trinity, queries are ASTs (abstract syntax trees). When you parse an expression, this is done using Trinity::ast_parser. A Trinity::query is really a container of an AST as long as the allocator used to build it.

Here is how you can build a query:

Query q(expression);
Query q;

q.parse(expression);

You can specify a function in Query’s constructor or in the parse() method that is used to parse tokens from the expressions. Please refer to queries.h where Trinity::Query is defined for more information. An expression is a Trinity::str32_t. Read more about core constructs and limits.

Optimiser

When a query is executed by the execution engine (see Trinity::exec_query() implementation ), a 4-pass compiler/optimiser is responsible for building an execution plan, an execution tree from the query AST. That execution tree is optimised in order to prune it, re-arrange its nodes and otherwise translate the execution path resulted from a direct compilation of the query AST, to a highly compact and optimised representation that requires as few operations and checks as possible during document evaluation.

If you are so inclined, you can check the implementation in exec.cpp. If you come up with new optimizations (see optimize_node() in exec.cpp ), please send a PR so that others can take advantage of it.

Clone this wiki locally