Querying an Index



  • This article is a basic overview of how to query a static index.
    Indexing the content of your documents allows for fast document retrieval when querying the index.

  • Examples in this article show querying an index.
    For dynamic query examples see Query Overview.

  • You can query an index with either of the following:


session.query

  • The following examples query an index using the session's query method.

  • Customize your query with these API methods.

Query index - no filtering

// Query the 'Employees' collection using the index - without filtering
// (Open the 'Index' tab to view the index class definition)

const employees = await session
     // Pass the index name as a parameter
     // Use slash `/` in the index name, replacing the underscore `_` from the index class definition
    .query({ indexName: "Employees/ByName" })
     // Execute the query
    .all();

// All 'Employee' documents that contain DOCUMENT-fields 'firstName' and\or 'lastName' will be returned
// Query the 'Employees' collection using the index - without filtering

const employees = await session
     // Pass the queried collection as the first param
     // Pass the index class as the second param
    .query(Employee, Employees_ByName)
     // Execute the query
    .all();

// All 'Employee' documents that contain DOCUMENT-fields 'firstName' and\or 'lastName' will be returned
// The index definition:

class Employees_ByName extends AbstractJavaScriptIndexCreationTask {

    constructor() {
        super();

        // Define the INDEX-fields 
        this.map("Employees", e => ({
            
            // Content of INDEX-fields 'firstName' & 'lastName' 
            // is composed of the relevant DOCUMENT-fields
            firstName: e.firstName,
            lastName: e.lastName
        }));

        // * The index-fields can be queried on to fetch matching documents. 
        //   You can query and filter Employee documents based on their first or last names.

        // * Employee documents that do Not contain both 'firstName' and 'lastName' fields
        //   will Not be indexed.

        // * Note: the INDEX-field name does Not have to be exactly the same
        //   as the DOCUMENT-field name. 
    }
}
// Note:
// Use slash `/` in the index name, replacing the underscore `_` from the index class definition

from index "Employees/ByName"

// All 'Employee' documents that contain DOCUMENT-fields 'FirstName' and\or 'LastName' will be returned

Query index - with filtering

// Query the 'Employees' collection using the index - filter by INDEX-field

const employees = await session
     // Pass the index name as a parameter
     // Use slash `/` in the index name, replacing the underscore `_` from the index class definition
    .query({ indexName: "Employees/ByName" })
     // Filter the retrieved documents by some predicate on an INDEX-field
    .whereEquals("lastName", "King")
     // Execute the query
    .all();

// Results will include all documents from 'Employees' collection whose 'lastName' equals to 'King'
// Query the 'Employees' collection using the index - filter by INDEX-field

const employees = await session
     // Pass the queried collection as the first param
     // Pass the index class as the second param
    .query(Employee, Employees_ByName)
     // Filter the retrieved documents by some predicate on an INDEX-field
    .whereEquals("lastName", "King")
     // Execute the query
    .all();

// Results will include all documents from 'Employees' collection whose 'lastName' equals to 'King'
// The index definition:

class Employees_ByName extends AbstractJavaScriptIndexCreationTask {

    constructor() {
        super();

        // Define the INDEX-fields 
        this.map("Employees", e => ({
            
            // Content of INDEX-fields 'firstName' & 'lastName' 
            // is composed of the relevant DOCUMENT-fields
            firstName: e.firstName,
            lastName: e.lastName
        }));

        // * The index-fields can be queried on to fetch matching documents. 
        //   You can query and filter Employee documents based on their first or last names.

        // * Employee documents that do Not contain both 'firstName' and 'lastName' fields
        //   will Not be indexed.

        // * Note: the INDEX-field name does Not have to be exactly the same
        //   as the DOCUMENT-field name. 
    }
}
// Note:
// Use slash `/` in the index name, replacing the underscore `_` from the index class definition

from index "Employees/ByName"
where lastName == "King"

// Results will include all documents from 'Employees' collection whose 'lastName' equals to 'King'.
  • An exception will be thrown when filtering by fields that are Not defined in the index.

  • Read more about filtering here.

Query index - with paging

// Query the 'Employees' collection using the index - page results

// This example is based on the previous filtering example
const employees = await session
    .query({ indexName: "Employees/ByName" })
    .whereEquals("lastName", "King")
    .skip(5)  // Skip first 5 results
    .take(10) // Retrieve up to 10 documents
    .all();

// Results will include up to 10 matching documents
// Query the 'Employees' collection using the index - page results

// This example is based on the previous filtering example
const employees = await session
    .query(Employee, Employees_ByName)
    .whereEquals("lastName", "King")
    .skip(5)  // Skip first 5 results
    .take(10) // Retrieve up to 10 documents
    .all();

// Results will include up to 10 matching documents
// The index definition:

class Employees_ByName extends AbstractJavaScriptIndexCreationTask {

    constructor() {
        super();

        // Define the INDEX-fields 
        this.map("Employees", e => ({
            
            // Content of INDEX-fields 'firstName' & 'lastName' 
            // is composed of the relevant DOCUMENT-fields
            firstName: e.firstName,
            lastName: e.lastName
        }));

        // * The index-fields can be queried on to fetch matching documents. 
        //   You can query and filter Employee documents based on their first or last names.

        // * Employee documents that do Not contain both 'firstName' and 'lastName' fields
        //   will Not be indexed.

        // * Note: the INDEX-field name does Not have to be exactly the same
        //   as the DOCUMENT-field name. 
    }
}
// Note:
// Use slash `/` in the index name, replacing the underscore `_` from the index class definition

from index "Employees/ByName"
where lastName == "King"
limit 5, 10 // skip 5, take 10
  • Read more about paging here.

session.advanced.rawQuery

  • Queries defined with query are translated by the RavenDB client to RQL when sent to the server.

  • The session also gives you a way to express the query directly in RQL using the rawQuery method.

Example:

// Query with RawQuery - filter by INDEX-field

const results = await session
     // Provide RQL to rawQuery
    .advanced.rawQuery("from index 'Employees/ByName' where lastName == 'King'")
     // Execute the query
    .all();

// Results will include all documents from 'Employees' collection whose 'lastName' equals to 'King'.
// The index definition:

class Employees_ByName extends AbstractJavaScriptIndexCreationTask {

    constructor() {
        super();

        // Define the INDEX-fields 
        this.map("Employees", e => ({
            
            // Content of INDEX-fields 'firstName' & 'lastName' 
            // is composed of the relevant DOCUMENT-fields
            firstName: e.firstName,
            lastName: e.lastName
        }));

        // * The index-fields can be queried on to fetch matching documents. 
        //   You can query and filter Employee documents based on their first or last names.

        // * Employee documents that do Not contain both 'firstName' and 'lastName' fields
        //   will Not be indexed.

        // * Note: the INDEX-field name does Not have to be exactly the same
        //   as the DOCUMENT-field name. 
    }
}
// Note:
// Use slash `/` in the index name, replacing the underscore `_` from the index class definition

from index "Employees/ByName"
where LastName == "King"