liveBook · Manning

Query expansion

As explained in AI-Powered Search

Query expansion is a retrieval technique that augments a query with additional terms or representations to improve matching with relevant content.

A search for “advil” can miss a relevant document that says “ibuprofen” but never uses the query word. Query expansion adds related terms or weighted tokens to the search request so documents can match, or rank more highly, without containing the exact query. The original term can retain a stronger boost than its expansions. section 5.4.5 section 5.4.1

One way to generate expansions is to traverse a semantic knowledge graph (SKG) built from indexed content. Start at the query term, retrieve related terms from a document field, and use their relatedness scores as boosts. Because these are terms found in the index, they can be searched directly in document fields without fine-tuning a model to the dataset. section 5.4.5 section 7.4.3

Expanding “vibranium”

In a science-fiction index, a traversal from “vibranium” returns terms including “wakandan,” “adamantium,” and “wakanda.” Python can format the results as a search query such as `vibranium^5 wakandan^0.8197 adamantium^0.80724 wakanda^0.79122`. The stronger boost on the original word keeps an exact match prominent while the related terms provide other paths to relevant documents. section 5.4.5

How the added terms enter the request determines the trade-off:

  • Put them alongside the original query to allow more ways to match; adjust `min_match` to require more matching terms when greater conceptual precision is wanted. section 5.4.5
  • Require the original query with `AND` while using related terms as additional matching conditions, or place the terms in `query_boosts` to change ranking without changing the requested query. section 5.4.5
  • For an ambiguous word, select expansions in context first. “Chef” has different related terms in cooking and DevOps categories; using an unqualified list risks mixing senses. section 6.2

An alternative, SPLADE-style expansion uses a pretrained language model to produce weighted sparse tokens rather than SKG terms taken directly from the index. Some outputs are partial tokens, so using them for search requires compatible indexed tokens. Inspect the generated terms and tune matching and boosts: relatedness alone does not guarantee that every expansion fits the user’s intent. section 7.4.3 section 6.2

Listing 5.7 Different query augmentation strategies
def generate_request(query, min_match=None, boost=None):
  request = {"query": query,
             "query_fields": ["title", "body"]}
  if min_match:
    request["min_match"] = min_match
  if boost:
    request["query_boosts"] = boost
  return request

simple_expansion = generate_request(f"{query} {expansion}", "1")
increased_conceptual_precision = \
  generate_request(f"{query} {expansion}", "30%")
increased_precision_same_recall = \
  generate_request(f"{query} AND ({expansion})", "2")
slightly_increased_recall = generate_request(f"{query} {expansion}", "2")
same_results_better_ranking = generate_request(query, "2", expansion)

The Python request examples show where expanded terms, `min_match`, `AND`, and `query_boosts` enter a search request. section 5.4.5

How AI-Powered Search covers it

How deep it goes
Works through SKG traversal, weighted query construction, and alternative matching requests; introduces SPLADE-style output without building its model.
Examples
Uses boosted “advil” alternatives, a “vibranium” SKG traversal, category-specific senses of “chef,” and SPLADE++ token outputs.
What it assumes
Starts from indexed document fields and demonstrates Python-based SKG traversal; assumes a configured search engine for running requests.
What you can do after
Apply it via SKG traversal and search requests to generate weighted terms and choose matching or ranking strategies.
How it connects
Connects semantic knowledge graphs, sparse lexical search, query-sense disambiguation, and SPLADE-style expansion.

Also explained in

Compare how 2 books explain query expansion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.