Indexes: Indexing LINQ Extensions
Various indexing LINQ extensions are available to enhance the usability and reduce the complexity of the indexing functions. The available extensions are:
Boost
You can read more about boosting here.
Reverse
Strings and enumerables can be reversed by using Reverse
extension.
class Employees_ByReversedFirstName extends AbstractIndexCreationTask {
constructor() {
super();
this.map = "docs.Employees.Select(employee => new { " +
" FirstName = employee.FirstName.Reverse() " +
"})";
}
}
const results = await session
.query({ indexName: "Employees/ByReversedFirstName" })
.whereEquals("firstName", "treboR")
.all();
WhereEntityIs
WhereEntityIs
can be used to check if given Raven-Entity-Name
value in metadata for the given document matches any of the given values. This can be useful when indexing polymorphic data. Please visit our dedicated article to get more information (or click here).
IfEntityIs
IfEntityIs
is similar to WhereEntityIs
, yet it checks only against one value.
Parsing numbers
String values can be safely parsed to int
, long
, decimal
and double
using the appropriate methods:
- ParseInt,
- ParseLong,
- ParseDecimal,
- ParseDouble
There are two overrides for each method: The first one returns the default value in case of parsing failure. The second one accepts the value that should be returned when failure occurs.
class Item_Parse extends AbstractIndexCreationTask {
constructor() {
super();
this.map = "docs.Items.Select(item => new {" +
" item = item, " +
" parts = item.version.Split('.', System.StringSplitOptions.None) " +
"}).Select(this0 => new { " +
" majorWithDefault = this0.parts[0].ParseInt(), " + // will return default(int) in case of parsing failure
" majorWithCustomDefault = this0.parts[0].ParseInt(-1) " + // will return -1 in case of parsing failure
"})";
this.storeAllFields("Yes");
}
}
class Item {
constructor(version) {
this.version = version;
}
}
const item1 = new Item("3.0.1");
const item2 = new Item("Unknown");
await session.store(item1);
await session.store(item2);
await session.saveChanges();
const results = await session
.query({ indexName: "Item/Parse" })
.all();
assert.strictEqual(2, results.length);
assert.ok(results.some(x => x.majorWithDefault === 3));
assert.ok(results.some(x => x.majorWithCustomDefault === 3));
assert.ok(results.some(x => x.majorWithDefault === 0));
assert.ok(results.some(x => x.majorWithCustomDefault === -1));