-
Notifications
You must be signed in to change notification settings - Fork 20
Query Rewrite
Query Rewriting is as important, if not more so, than the IR engine’s performance and functionality, for IR applications and their users very rarely search for something explicitly and exactly.
A query such as [pets] will match “pets” exactly, but not “pet”, or “cat”, or “cats”, or “dogs”, or “dog”, and so on. This is a matter of performance expansions and contractions. They can be applied during index time or during query time -- and often times you need, or should choose based on the application’s needs and users expectations.
For more information about expansion and contraction, and index vs query time application, see Elastic’s documentation, and Huge E. Williams’s excellent write up.
Trinity provides functionality for rewriting/expanding queries effortlessly, and it’s designed with performance and simplicity in mind.
You don’t need to use it, you can of course manipulate the AST directly, but using Trinity::rewrite_query<> is the recommended way to do it; it properly deals with expansions and contractions that spans multiple tokens and other edge conditions, and it’s trivial to use.
Trinity::query q("pet");
Trinity::rwrite_query(q, 512, 3, [](const auto &runCtx,
const auto tokens,
const auto cut,
auto &allocator,
auto out
{
if (cat == 1)
{
if (tokens[0].Eq(_S("pet")))
{
out->push_back("pets");
out->push_back("cats");
out->push_back("cat");
out->push_back("dogs");
out->push_back("dog");
}
}
});You should check out queries_rewrite.h -- the semantics and species are described in the comments.
Please read Query Understanding blog posts on query rewrites, expansion, segmentation, etc. You should also check the 'Inside the Algolia Engine' blog posts, especially part 2 and and part 6.