RavenDB 2.x  Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – editing an Index

We want to enhance the TotalOrdersPerCustomer index by adding a Reduce function. For that, we will open the index in the Edit Index screen and modify its related LINQ functions.

  1. In the Management Studio, display the Indexes screen.
  2. Click on the pencil at the right of the name of the TotalOrdersPerCustomer index.
  3. In the Edit Index screen, click on the Add Reduce button to add a new Reduce function
  4. In the Reduce section, enter the following code snippet:
    from result in results
    group result by result.CustomerId into g
    select new {CustomerId = g.Key, TotalCost = g.Sum(x=>x.TotalCost)}
  5. Click on the Save Index button to save changes.
  6. Click on the Indexes links to show the Indexes list.
  7. Click on the TotalOrdersPerCustomer index to query it and display results in the Query Index Screen.

What just happened?

We enhanced the TotalOrdersPerCustomer index and added a Reduce function.

This Reduce LINQ function aggregates the result of the Map function (written in the previous section) and returns a two-column dataset: CustomerId and TotalCost for each CustomerId.

The Map function result is grouped on the CustomerId key and the TotalCost field is aggregated in each group.

The Query Index screen

The Management Studio's Query Index screen allows you to query an index result dynamically. For that, you can use the query designer area to write a query expression.

The Query Index screen has a toolbar with an Execute button which lets you run the query and will show the results. We have the Add Sort By button which you can use to display the query result in a specific order. Also, you can display the index statistics by clicking on the Index Statistics button or by deleting all the documents resulting from the query.

To write a query expression, you will use the Lucene syntax which is a custom query syntax for querying the indexes. In the query area, you can use Ctrl + Space for hints on the field names.

Note

Technically, Raven makes use of the Lucene.NET library, which was ported from the Java based Lucene library. Lucene is a fully-featured text search engine library written entirely in Java.

To get more details about Lucene.NET, you may browse this link: http://lucenenet.apache.org/.