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.
public static class Employees_ByReversedFirstName extends AbstractIndexCreationTask {
public Employees_ByReversedFirstName() {
map = "docs.Employees.Select(employee => new { " +
" FirstName = employee.FirstName.Reverse() " +
"})";
}
}
List<Employee> results = session
.query(Employee.class, Employees_ByReversedFirstName.class)
.whereEquals("FirstName", "treboR")
.toList();
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.
public static class Item_Parse extends AbstractIndexCreationTask {
public static class Result {
private int majorWithDefault;
private int majorWithCustomDefault;
public int getMajorWithDefault() {
return majorWithDefault;
}
public void setMajorWithDefault(int majorWithDefault) {
this.majorWithDefault = majorWithDefault;
}
public int getMajorWithCustomDefault() {
return majorWithCustomDefault;
}
public void setMajorWithCustomDefault(int majorWithCustomDefault) {
this.majorWithCustomDefault = majorWithCustomDefault;
}
}
public Item_Parse() {
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
"})";
storeAllFields(FieldStorage.YES);
}
}
public static class Item {
private String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
Item item1 = new Item();
item1.setVersion("3.0.1");
Item item2 = new Item();
item2.setVersion("Unknown");
session.store(item1);
session.store(item2);
session.saveChanges();
List<Item_Parse.Result> results = session
.query(Item_Parse.Result.class, Item_Parse.class)
.toList();
Assert.assertEquals(2, results.size());
Assert.assertTrue(results.stream().anyMatch(x -> x.getMajorWithDefault() == 3));
Assert.assertTrue(results.stream().anyMatch(x -> x.getMajorWithCustomDefault() == 3));
Assert.assertTrue(results.stream().anyMatch(x -> x.getMajorWithDefault() == 0));
Assert.assertTrue(results.stream().anyMatch(x -> x.getMajorWithCustomDefault() == -1));
Remarks
Information
Default storage
value for the storeAllFields()
method is FieldStorage.NO
. Keep in mind that storing fields will increase disk space usage.