Skip to main content

Include Explanations in Index Query

Include explanations when querying Map index

public class Products_BySearchName : AbstractIndexCreationTask<Product>
{
public class IndexEntry
{
public string Name { get; set; }
}

public Products_BySearchName()
{
Map = products => from product in products
select new IndexEntry()
{
Name = product.Name
};

// Configure the index-field 'Name' for FTS
Indexes.Add(x => x.Name, FieldIndexing.Search);
}
}

Include explanations when querying Map-Reduce index

// This index counts the number of units ordered per category in all Product documents
// ===================================================================================

public class NumberOfUnitsOrdered_PerCategory :
AbstractIndexCreationTask<Product, NumberOfUnitsOrdered_PerCategory.IndexEntry>
{
public class IndexEntry
{
public string Category { get; set; }
public int NumberOfUnitsOrdered { get; set; }
}

public NumberOfUnitsOrdered_PerCategory()
{
Map = products => from product in products
// Load the products' category
let categoryName = LoadDocument<Category>(product.Category).Name

select new IndexEntry()
{
Category = categoryName,
NumberOfUnitsOrdered = product.UnitsOnOrder
};

Reduce = results => from result in results
group result by result.Category
into g
let unitsOrdered = g.Sum(x => x.NumberOfUnitsOrdered)

select new IndexEntry()
{
Category = g.Key,
NumberOfUnitsOrdered = unitsOrdered
};
}
}

Syntax

// Use this overload when querying a Map index
IDocumentQuery<T> IncludeExplanations(out Explanations explanations);

// Use this overload when querying a Map-Reduce index
IDocumentQuery<T> IncludeExplanations(ExplanationOptions options, out Explanations explanations);
ParameterTypeDescription
explanationsExplanationsAn out param that will be filled with the explanations results.
optionsExplanationOptionsAn object that specifies the GroupKey when querying a Map-Reduce index.
public class Explanations
{
// Returns a list with all explanations.
// Pass the document ID of a document from the results to get its score details (Map index)
// Pass the GroupKey of an item from the results to get its score details (Map-Reduce index)
public string[] GetExplanations(string key);
}
public sealed class ExplanationOptions
{
// The group key that was used to group by the items in the Map-Reduce index
public string GroupKey { get; set; }
}