RavenDB uses indexes to facilitate fast queries powered by Lucene, the full-text search engine.
The indexing of a single document starts from creating Lucene's Document according to an index definition. Lucene processes it by breaking it into fields and splitting all the text from each field into tokens (or terms) in a process called tokenization. Those tokens will be stored in the index, and later will be searched upon.
The tokenization process uses an object called an Analyzer.
The indexing process and its results can be controlled by various field options and by the Analyzers.
Lucene offers several Analyzers out of the box, and new ones can be created easily. Various Analyzers differ in the way they split the text stream ("tokenize"), and in the way they process those tokens in post-tokenization.
For example, given this sample text:
The quick brown fox jumped over the lazy dogs, Bob@hotmail.com 123432.
StandardAnalyzer, which is Lucene's default, will produce the following tokens:
You can override NGram analyzer default token lengths by configuring Indexing.Analyzers.NGram.MinGram and Indexing.Analyzers.NGram.MaxGram per index e.g. setting them to 3 and 4 accordingly will generate:
For regular text fields, RavenDB uses a custom analyzer called LowerCaseKeywordAnalyzer. Its implementation
behaves like Lucene's KeywordAnalyzer, but it also performs case normalization by converting all characters
to lower case. That is - RavenDB stores the entire text field as a single token, in a lower cased form. Given
the same sample text above, LowerCaseKeywordAnalyzer will produce a single token:
[the quick brown fox jumped over the lazy dogs, firstname.lastname@example.org 123432.]
Default Exact Analyzer
For 'exact case' text fields, RavenDB uses Lucene's KeywordAnalyzer, which treats the entire text field as one
token and does not change the case of the original text. To make an index store text with the exact case, see the
section on changing field indexing behavior below.
Default Search Analyzer
For full-text search text fields, RavenDB uses RavenStandardAnalyzer, which is just an optimized version of
Lucene's StandardAnalyzer. To make an index that allows full-text search, see the section on changing field
indexing behavior below.
To allow full-text search on the text fields, you can use the analyzers provided out of the box with Lucene. These are available as part of the Lucene library which ships with RavenDB.
For most cases, Lucene's StandardAnalyzer would be your analyzer of choice. As shown above, this analyzer is aware of e-mail and network addresses when tokenizing. It normalizes cases, filters out common English words, and does some basic English stemming as well.
For languages other than English, or if you need a custom analysis process, you can roll your own Analyzer. It is quite simple and may already be available as a contrib package for Lucene.
There are also Collation analyzers available (you can read more about them here).
Selecting an Analyzer for a Field
To index a document field using a specific analyzer, all you need to do is to match it with the field's
The analyzer you are referencing must be available to the RavenDB server instance. See the different
methods of creating custom analyzers below.
Creating Custom Analyzers
You can write your own custom analyzers as a file. Custom analyzers can be defined as:
Database Custom Analyzers - can only be used by the indexes of the database where they are defined.
Server-Wide Custom Analyzers - can be used by indexes on all databases on all servers in the cluster.
A database analyzer can have the same name as a server-wide analyzer. In this situation, the indexes of that
database will use the database version of the analyzer. So you can think of database analyzers as overriding
the server-wide analyzers with the same names.
There are a few ways to create a custom analyzer and add it to your server:
1. Using the Studio
2. Using the Client API
3. Adding it directly to RavenDB's binaries, see below
Using the Client API
First, create a class that inherits from abstract Lucene.Net.Analysis.Analyzer (you need to reference
Lucene.Net.dll, which is supplied with RavenDB Server package). For example:
public class MyAnalyzer : Lucene.Net.Analysis.Analyzer
public override TokenStream TokenStream(string fieldName, TextReader reader)
throw new CodeOmitted();
Next, define the analyzer for a specific database using the operation PutAnalyzersOperation. Or, to make it
a server-wide analyzer, use PutServerWideOperation. These operations are very similar in how they work.
Both of them take one parameter: either an AnalyzerDefinition, or an array of AnalyzerDefinition's.
Another way of adding custom analyzers to RavenDB is to place them next to RavenDB's binaries. Note that it needs to be
compatible with .NET Core 2.0 (e.g. .NET Standard 2.0 assembly). The fully qualified name needs to be specified for an
indexing field that is going to be tokenized by the analyzer. This is the only way to add custom analyzers in RavenDB
versions older than 5.2.
Manipulating Field Indexing Behavior
By default, each indexed field is analyzed using the 'LowerCaseKeywordAnalyzer' which indexes a field as a single, lower cased term.
This behavior can be changed by setting the FieldIndexing option for a particular field. The possible values are:
Setting the FieldIndexing option for this field to Exact turns off the field analysis. This causes all the
properties to be treated as a single token and the matches must be exact (case sensitive), using
the KeywordAnalyzer behind the scenes.
If you want to disable indexing on a particular field, use the FieldIndexing.No option. This can be useful when you want to store field data in the index, but don't want to make it available for querying. However, it will still be available
for extraction by projections: