Skip to main content

RVN007 - Query field not defined in the index

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") or session.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's IndexName override 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 of Where, OrderBy, OrderByDescending, ThenBy, ThenByDescending, and Search.
  • Query syntax:
    the where and orderby clauses of a query whose first from clause is the Query call.

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.

Full list of cases not checked

RVN007 is not reported for the following references and operators:

  • Id, which a static-index query can always filter and sort by.
  • Nested property paths, such as x.Address.City or x.Name.Length.
  • A collection field used with a lambda operator, such as x.Lines.Any(l => l.Product == "products/1"),
    as in a fanout index.
  • Operators that follow Select, ProjectInto, SelectMany, or GroupBy in method syntax,
    or that follow select … into or group … into in query syntax.

RVN007 is also not reported when the target index or its fields cannot be determined:

  • The index name is not a string literal, does not match an index class in the current project,
    or matches more than one index class.
  • The index class overrides IndexName with a value other than a string literal (string form only).
  • The index map uses CreateField, CreateSpatialField, or AsJson, which create field names at runtime.
  • A map is not a lambda with a readable body (for example, a block body or a method call),
    or its final select, Select, or SelectMany projection is not an anonymous object or object initializer.
  • JavaScript indexes, and indexes that override CreateIndexDefinition().
  • Single-map time-series and counters indexes. Their multi-map variants are checked.
  • Index classes declared private, protected, or private protected, or nested in a class with one of these access levels.
  • The index class or a base class cannot be analyzed, for example because it comes from a compiled assembly without information recorded by the RavenDB source generator.
  • Stored-field configuration cannot be analyzed, for example 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 except Id, 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 Query call are checked.
    A query stored in a variable and filtered in a later statement is not checked.

  • RVN007 does not stop at ToList() or AsEnumerable() 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 from clause must be the Query call itself. A source with other operators applied, such as session.Query<Order, Orders_ByCompany>().Customize(…), is not checked.

  • RVN002
    Reports query operators placed after a ProjectInto or Select projection.

  • RVN008
    Fires when a projected field is not retrievable from the index or the source document.

In this article