Storing data in index
After the tokenization and analysis process is complete, the resulting tokens are stored in an index, which is now ready to be search with. Only fields in the final index projection could be used for searches, and the actual tokens stored for each depend on how the selected Analyzer processed the original text.
Lucene allows storing the original token text for fields, and RavenDB exposes this feature in the index definition object via Stores
.
By default, tokens are saved to the index as Indexed and Analyzed but not Stored - that is: they can be searched on using a specific Analyzer (or the default one), but their original value is unavailable after indexing. Enabling field storage causes values to be available for retrieval via the ProjectFromIndexFieldsInto, transformers, or other projection functions. It is done like this:
public class Employees_ByFirstAndLastName : AbstractIndexCreationTask<Employee>
{
public Employees_ByFirstAndLastName()
{
Map = employees => from employee in employees
select new
{
employee.FirstName,
employee.LastName
};
Stores.Add(x => x.FirstName, FieldStorage.Yes);
Stores.Add(x => x.LastName, FieldStorage.Yes);
}
}
store
.DatabaseCommands
.PutIndex(
"Employees_ByFirstAndLastName",
new IndexDefinition
{
Map = @"from employee in docs.Employees
select new
{
employee.FirstName,
employee.LastName
}",
Stores = new Dictionary<string, FieldStorage>
{
{ "FirstName", FieldStorage.Yes },
{ "LastName", FieldStorage.Yes }
}
});
Remarks
Information
Default value in Stores
for each field is FieldStorage.No
. Keep in mind that storing fields will increase disk space required by an index.
Info
If the projection function requires only the fields that are stored, then the document will not be loaded from storage and all data will come directly from the index. This can increase query performance (at the cost of disk space used) in the frequent situations when the whole document is not needed.
Raven/ImplicitFetchFieldsFromDocumentMode
setting can be altered to change the behavior of field fetching. By default it allows fetching fields from document if index is missing them (they are not stored), but this can be changed to skipping those fields or even throwing an exception. Read more about this configuration option here.