Skip to main content

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:

StripHtml

This extension can come in handy when you want to index a HTML without any HTML tags e.g. for full text search.

public class Article_Search : AbstractIndexCreationTask<Article>
{
public class Result
{
public string Content { get; set; }
}

public Article_Search()
{
Map = articles => from article in articles
select new
{
Content = article.ContentHtml.StripHtml()
};

Index(x => x.ContentHtml, FieldIndexing.Analyzed);
}
}

Boost

You can read more about boosting here.

Reverse

Strings and enumerables can be reversed by using Reverse extension.

public class Employees_ByReversedFirstName : AbstractIndexCreationTask<Employee>
{
public Employees_ByReversedFirstName()
{
Map = employees => from employee in employees
select new
{
FirstName = employee.FirstName.Reverse()
};
}
}

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 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, double and single using appropriate methods:

  • ParseInt,
  • ParseLong,
  • ParseDecimal,
  • ParseDouble,
  • ParseSingle

There are two overrides for each method, first one returning default value in case of parsing failure, second one accepting value that should be returned when failure occurs.

public class Item_Parse : AbstractIndexCreationTask<Item>
{
public class Result
{
public int MajorWithDefault { get; set; }

public int MajorWithCustomDefault { get; set; }
}

public Item_Parse()
{
Map = items => from item in items
let parts = item.Version.Split('.')
select new
{
MajorWithDefault = parts[0].ParseInt(), // will return default(int) in case of parsing failure
MajorWithCustomDefault = parts[0].ParseInt(-1) // will return -1 in case of parsing failure
};

StoreAllFields(FieldStorage.Yes);
}
}