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

Time for action – creating a Dynamic Query

You will learn to create a dynamic query using the Lucene syntax and promote the related temporary index to permanent.

  1. In the Management Studio, select the Indexes tab to display the Indexes screen.
  2. Click on the Dynamic Query button to display the Query Index screen.
  3. Click on the Dynamic/ list and select the Orders collection.
  4. In the Query area, enter the following code snippet:
    UnitCost: [1 TO 20]
    Tip

    In the Query area, press Ctrl + Space to show the index fields list and select the UnitCost field.

    To do search on the data, instead of a lexical search you can use Unitcost: [Dx1 TO Dx20].

  5. Click on the Execute button to show the results of the query.
  6. Click on the Indexes link to show the Indexes screen.
  7. Right-click on the Auto/Orders/ByUnitCost index to show the contextual menu and click on the Promote to auto index button.

What just happened?

We created a dynamic query which will retrieve all the documents in the Orders collection that matches the range values we specified in the query expression for the UnitsCost field.

In this Lucene expression, we use a range query which allows us to match documents whose field(s) values are between the lower and upper bounds specified by the range query. Range queries can be inclusive or exclusive of the upper and lower bounds.

The query engine will search all the documents belonging to the Orders collection that the UnitCost field value will match with the value range we have specified 1 TO 20.

By executing the dynamic query, RavenDB will create a temporary index named Temp/Orders/ByUnitCost which will be found in the available indexes list.

Then we promoted the temporary index created by the dynamic query to permanent. You need to promote a temporary index only if you want to keep using that index. RavenDB will purge temporary indexes if it has not queried for 1200 seconds (which is the default value fixed by the Raven/TempIndexCleanupThreshold configuration key).

Note

Managing temporary indexes is discussed in detail in Chapter 4, RavenDB Indexes and Queries.