Indexes: Map Indexes
Map
indexes, sometimes referred to as simple indexes, contain one (or more) mapping functions that indicate which fields from the documents should be indexed.- After indexing, documents can be searched by the indexed fields.
-
The mapping functions are LINQ-based functions; they can be considered the core of indexes.
-
In This Page:
Also see:
- Indexing fields from related documents
- Aggregating data with Map-Reduce indexes
- Indexing multiple collections with Multi-Map indexes
- Running calculations and storing the results in the index to reduce query time
Indexing Single Fields
Let's create an index that will help us search for Employees
by their FirstName
, LastName
, or both.
- First, let's create an index called
Employees/ByFirstAndLastName
class Employees_ByFirstAndLastName extends AbstractCsharpIndexCreationTask {
// ...
}
- The next step is to create the indexing function itself. This is done by setting the
map
field with mapping function in the constructor.
constructor() {
super();
this.map = `from employee in docs.Employees
select new {
FirstName = employee.FirstName,
LastName = employee.LastName
}`;
}
const employees1 = await session.query({ indexName: "Employees/ByFirstAndLastName" })
.whereEquals("FirstName", "Robert")
.all();
const employees2 = await session.query({ indexName: "Employees/ByFirstAndLastName" })
.whereEquals("FirstName", "Robert")
.all();
from index 'Employees/ByFirstAndLastName'
where FirstName = 'Robert'
Field Types
Please note that indexing capabilities are detected automatically from the returned field type from the indexing function.
For example, if our Employee
will have a property called Age
that is an number
then the following indexing function...
`from employee in docs.Employees
select new
{
Age = employee.Age
}`
...grant us the capability to issue numeric queries (return all the Employees that Age
is more than 30).
Changing the Age
type to a string
will take that capability away from you. The easiest example would be to issue .ToString()
on the Age
field...
`from employee in docs.Employees
select new
{
Age = employee.Age.ToString()
}`
Convention
You will probably notice that in the Studio
, this function is a bit different from the one defined in the Employees_ByFirstAndLastName
class:
`from employee in docs.Employees
select new
{
FirstName = employee.FirstName,
LastName = employee.LastName
}`
The part you should pay attention to is docs.Employees
. This syntax indicates from which collection a server should take the documents for indexing. In our case, documents will be taken from the Employees
collection. To change the collection, you need to change Employees
to the desired collection name or remove it and leave only docs
to index all documents.
Combining Multiple Fields
Since each index contains a LINQ function, you can combine multiple fields into one.
Example I
class Employees_ByFullName extends AbstractCsharpIndexCreationTask {
constructor() {
super();
this.map = `from employee in docs.Employees
select new {
Name = employee.FirstName + ' ' + employee.LastName
}`;
}
}
const employees = await session
.query({ indexName: "Employees/ByFullName" })
.whereEquals("FullName", "Robert King")
.ofType(Employee)
.all();
from index 'Employees/ByFullName'
where FullName = 'Robert King'
Example II
Information
In this example, the index field Query
combines all values from various Employee fields into one. The default Analyzer on field is changed to enable Full Text Search
operations. The matches no longer need to be exact.
You can read more about analyzers and Full Text Search
here.
class Employees_Query extends AbstractCsharpIndexCreationTask {
constructor() {
super();
this.map = `from employee in docs.Employees
select new {
Query = new [] {
employee.FirstName,
employee.LastName,
employee.Title,
employee.Address.City
}
}`;
this.index("Query", "Search");
}
}
const employees = await session
.query({ indexName: "Employees/Query" })
.search("Query", "John Doe")
.ofType(Employee)
.all();
from index 'Employees/Query'
where search(Query, 'John Doe')
Indexing Partial Field Data
Imagine that you would like to return all employees that were born in a specific year. You can do it by indexing Birthday
from Employee
in the following way:
class Employees_ByBirthday extends AbstractCsharpIndexCreationTask {
constructor() {
super();
this.map = `from employee in docs.Employees
select new {
Birthday = employee.Birthday,
}`;
}
}
const startDate = new Date(1963, 1, 1);
const endDate = new Date(1963, 12, 31, 23, 59, 59, 999);
const employees = await session
.query({ indexName: "Employees/ByBirthday" })
.whereBetween("Birthday", startDate, endDate)
.ofType(Employee)
.all();
from index 'Employees/ByBirthday '
where Birthday between '1963-01-01' and '1963-12-31T23:59:59.9990000'
RavenDB gives you the ability to extract field data and to index by it. A different way to achieve our goal will look as follows:
class Employees_ByYearOfBirth extends AbstractCsharpIndexCreationTask {
constructor() {
super();
this.map = `from employee in docs.Employees
select new {
YearOfBirth = employee.Birthday.Year,
}`;
}
}
const employees = await session
.query({ indexName: "Employees/ByYearOfBirth" })
.whereEquals("YearOfBirth", 1963)
.ofType(Employee)
.all();
from index 'Employees/ByYearOfBirth'
where YearOfBirth = 1963
Indexing Nested Data
If your document contains nested data, e.g. Employee
contains Address
, you can index on its fields by accessing them directly in the index. Let's say that we would like to create an index that returns all employees that were born in a specific Country
:
class Employees_ByCountry extends AbstractCsharpIndexCreationTask {
constructor() {
super();
this.map = `from employee in docs.Employees
select new {
Country = employee.Address.Country
}`;
}
}
const employees = await session
.query({ indexName: "Employees/ByCountry" })
.whereEquals("Country", "USA")
.ofType(Employee)
.all();
from index 'Employees/ByCountry'
where Country = 'USA'
If a document relationship is represented by the document's ID, you can use the LoadDocument
method to retrieve such a document. More about it can be found here.
Indexing Missing Fields
By default, indexes will not index a document that contains none of the specified fields. This behavior can be changed using the Indexing.IndexEmptyEntries configuration option.
The option Indexing.IndexMissingFieldsAsNull
determines whether missing fields in documents are indexed with the value null
, or not indexed at all.