Include Query Explanations


  • When making a query, each document in the query results is assigned a score.
    This score determines the order by which the documents come back in the results when requesting
    to order by score.

  • Each document in the results includes this score under the @index-score property in its metadata.

  • Use includeExplanations to get the score details and see how it was calculated.

    • Including explanations is available only when using Lucene as the underlying search engine.
    • You can configure which search engine will be used. Learn how in Selecting the search engine.


Include explanations in a query

$explanations = new Explanations();

/** @var array<Product> $syrups */
$syrups = $session->advanced()->documentQuery(Product::class)
    ->includeExplanations(null, $explanations)
    ->search("Name", "Syrup")
    ->toList();

$scoreDetails = $explanations->getExplanations($syrups[0]->getId());
from "Products"
where search(Name, "Syrup") or search(Name, "Lager")
include explanations()

Please note that the First parameter is optional.
If you intend to use the default options, just paste null instead of the options object.

View explanations

  • The detailed explanations can be viewed from the Query view in Studio.

  • Running a query with includeExplanations will show an additional Explanations Tab.

Figure 1. Explanations in the Studio

Include explanations

  • Sample score details:
Figure 2. View explanations

View explanation

Syntax

public function includeExplanations(?ExplanationOptions $options, Explanations &$explanations): DocumentQueryInterface;
Parameter Type Description
$options ?ExplanationOptions This object is optional.
If you intend to use the default options, place null here.
&$explanations Explanations
  • A callback function (action) that takes Explanations as an argument. It will be called by the client with the resulting Explanations.
  • You can interact with resulting Explanations inside your callback.