
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.
- In the Management Studio, display the Indexes screen.
- Click on the pencil at the right of the name of the
TotalOrdersPerCustomer
index. - In the Edit Index screen, click on the Add Reduce button to add a new
Reduce
function - 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)}
- Click on the Save Index button to save changes.
- Click on the Indexes links to show the
Indexes
list. - 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/.