see on GitHub

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);
	}
}
IList<Article> results = session
	.Query<Article_Search.Result, Article_Search>()
	.Search(x => x.Content, "Raven*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard)
	.OfType<Article>()
	.ToList();
public class Article
{
	public string Id { get; set; }

	public string Title { get; set; }

	public string ContentHtml { get; set; }
}

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()
					};
	}
}
IList<Employee> results = session
	.Query<Employee, Employees_ByReversedFirstName>()
	.Where(x => x.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 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);
	}
}
public class Item
{
	public string Version { get; set; }
}
session.Store(new Item { Version = "3.0.1" });
session.Store(new Item { Version = "Unknown" });

session.SaveChanges();

var results = session
	.Query<Item_Parse.Result, Item_Parse>()
	.ToList();

Assert.Equal(2, results.Count);
Assert.True(results.Any(x => x.MajorWithDefault == 3));
Assert.True(results.Any(x => x.MajorWithCustomDefault == 3));
Assert.True(results.Any(x => x.MajorWithDefault == 0));
Assert.True(results.Any(x => x.MajorWithCustomDefault == -1));