RVN007 - Query field not defined in the index
-
RVN007 reports fields used for filtering, sorting, or searching in a static-index query when the index's map function does not define those fields.
-
Default severity: Info
Automated code fix: No -
In this article:
Description
The map function defines the index-fields in each index entry.
A query on a static index uses these fields for filtering, sorting, and searching.
If the query references a field the index does not contain, the server rejects the query with an error such as
"The field '…' is not indexed in '…', cannot query/sort on fields that are not indexed".
RVN007 reports these fields at compile time by comparing the fields referenced in the query with the index-fields defined by the map function.
Trigger condition
When is RVN007 reported?
Supported queries:
RVN007 checks queries that target a static index, in sync and async sessions:
- By index class:
session.Query<T, TIndex>()
The index class can be defined in the current project or a referenced project. - By index name:
session.Query<T>("IndexName")orsession.Query<T>(indexName: "IndexName"),
where the name is a string literal. The name is matched to an index class in the same project, using the class'sIndexNameoverride when it returns a string literal, or the class name with_replaced by/.
Checked operators:
RVN007 reports references to fields not defined by the index's map function in the following parts of the query:
- Method syntax:
the first lambda argument ofWhere,OrderBy,OrderByDescending,ThenBy,ThenByDescending, andSearch. - Query syntax:
thewhereandorderbyclauses of a query whose firstfromclause is theQuerycall.
How fields are checked:
RVN007 identifies index-fields from the members of the anonymous object or object initializer in the final projection of each Map, AddMap,
or AddMapForAll expression. The projection can use query syntax (select) or method syntax (Select or SelectMany).
RVN007 reads these expressions from constructor bodies of the index class and its base classes, including classes from referenced projects.
A field used as the receiver of a method call without a lambda argument, such as x.Tags.Contains("a") or x.Name.StartsWith("A"), is checked too.
The field in an indexer access, such as x.Tags[0], is also checked.
Each missing field is reported once per lambda, where condition, or individual expression in an orderby clause.
When is RVN007 not reported?
RVN007 skips some field references and query operators, and is also not reported when the target index or its fields cannot be determined. RVN007 is not reported for the following references and operators: RVN007 is also not reported when the target index or its fields cannot be determined:Full list of cases not checked
Id, which a static-index query can always filter and sort by.x.Address.City or x.Name.Length.x.Lines.Any(l => l.Product == "products/1"),
as in a fanout index.Select, ProjectInto, SelectMany, or GroupBy in method syntax,
or that follow select … into or group … into in query syntax.
or matches more than one index class.IndexName with a value other than a string literal (string form only).CreateField, CreateSpatialField, or AsJson, which create field names at runtime.
or its final select, Select, or SelectMany projection is not an anonymous object or object initializer.CreateIndexDefinition().private, protected, or private protected, or nested in a class with one of these access levels.Store(nameof(Order.Company), FieldStorage.Yes).
RVN007 recognizes stored-field names supplied as string literals or supported field lambdas, such as x => x.Company.
How to resolve
Define the missing index-field in the map function, or query an index that already defines it.
Before (triggers RVN007)
public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
public class IndexEntry
{
public string Company { get; set; }
public string Employee { get; set; }
}
public Orders_ByCompany()
{
// Employee is declared in IndexEntry but not set by the map,
// so it is not an index-field.
Map = orders => from o in orders
select new IndexEntry
{
Company = o.Company
};
}
}
var results = session.Query<Orders_ByCompany.IndexEntry, Orders_ByCompany>()
// Triggers RVN007: Employee is not defined as an index-field.
.Where(x => x.Employee == "employees/1-A")
// Return the matching Order documents.
.OfType<Order>()
.Take(50)
.ToList();
After (correct)
public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
public class IndexEntry
{
// The index-fields:
public string Company { get; set; }
public string Employee { get; set; }
}
public Orders_ByCompany()
{
Map = orders => from o in orders
select new IndexEntry
{
Company = o.Company,
Employee = o.Employee
};
}
}
var results = session.Query<Orders_ByCompany.IndexEntry, Orders_ByCompany>()
.Where(x => x.Employee == "employees/1-A")
// Return the matching Order documents.
.OfType<Order>()
.Take(50)
.ToList();
Limitations
-
RVN007 reads map fields only from constructor bodies; it does not follow helper methods called by constructors.
If the map is assigned entirely in a helper, RVN007 finds no index-fields and reports every otherwise checked field exceptId, even when the map defines those fields. RVN004 does not report a missing map in this case, while RVN001 reports the assignment outside the constructor. -
Only operators in the same method chain starting with the
Querycall are checked.
A query stored in a variable and filtered in a later statement is not checked. -
RVN007 does not stop at
ToList()orAsEnumerable()when following a method chain.
Supported operators after these calls are still checked even though they run in memory.
The exclusions for operators after a projection or grouping still apply. -
In query syntax, the first
fromclause must be theQuerycall itself. A source with other operators applied, such assession.Query<Order, Orders_ByCompany>().Customize(…), is not checked.