Querying: Searching

When you need to do a more complex text searching, use the search method. This method allows you to pass a few search terms that will be used in the searching process for a particular field. Here is a sample code that uses the search method to get users with the name John or Adam:

const users = await session
    .query(User)
    .search("name", "John Adam")
    .all();
from Users
where search(name, 'John Adam')

Each of the search terms (separated by space character) will be checked independently. The result documents must match exactly one of the passed terms.

In the same way, you can also look for users that have some hobby:

const users = await session
    .query(User)
    .search("hobbies", "looking for someone who likes sport books computers")
    .all();
from Users
where search(name, 'looking for someone who likes sport books computers')

The results will return users that are interested in sport, books or computers.

Multiple Fields

By using the search() method, you are also able to look for multiple indexed fields. In order to search using both name and hobbies properties, you need to issue the following query:

const users = await session
    .query(User)
    .search("name", "Adam")
    .search("hobbies", "sport")
    .all();
from Users
where search(name, 'Adam') or search(hobbies, 'sport')

Boosting

Indexing in RavenDB is built upon the Lucene engine that provides a boosting term mechanism. This feature introduces the relevance level of matching documents based on the terms found. Each search term can be associated with a boost factor that influences the final search results. The higher the boost factor, the more relevant the term will be. RavenDB also supports that, in order to improve your searching mechanism and provide the users with much more accurate results you can specify the boost argument.

For example:

const users = await session
    .query(User)
    .search("hobbies", "I love sport")
    .boost(10)
    .search("hobbies", "but also like reading books")
    .boost(5)
    .all();
from Users
where boost(search(hobbies, 'I love sport'), 10) or boost(search(hobbies, 'but also like reading books'), 5)

This search will promote users who do sports before book readers and they will be placed at the top of the results list.

Search Options

You can specify the logic of a search expression. It can be either:

  • or,
  • andAlso,
  • not.

The following query:

const users = await session
    .query(User)
    .search("hobbies", "computers")
    .search("name", "James")
    .whereEquals("age", 20)
    .all();

will be translated into

from Users
where search(hobbies, 'computers') or search(name, 'James') and Age = 20

You can also specify what exactly the query logic should be. The applied option will influence a query term where it was used. The query as follows:

const users = await session
    .query(User)
    .search("name", "Adam")
    .andAlso()
    .search("hobbies", "sport")
    .all();

will result in the following RQL query:

from Users
where search(name, 'Adam') and search(hobbies, 'sport')

If you want to negate the term use not:

const users = session
    .query(User)
    .not()
    .search("name", "James")
    .all();

According to RQL syntax it will be transformed into the query:

from Users
where exists(name) and not search(name, 'Adam')

You can also combine search options:

const users = await session
    .query(User)
    .search("name", "Adam")
    .andAlso()
    .not()
    .search("hobbies", "sport")
    .all();

It will produce the following RQL query:

from Users
where search(name, 'Adam') and (exists(hobbies) and not search(hobbies, 'sport'))

Using Wildcards

When the beginning or ending of a search term is unknown, wildcards can be used to add additional power to the searching feature. RavenDB supports both suffix and postfix wildcards.

Example I - Using Postfix Wildcards

const users = await session
    .query(User)
    .search("name", "Jo* Ad*")
    .all();
from Users
where search(name, 'Jo* Ad*')

Example II - Using Suffix and Postfix Wildcards

const users = await session
    .query(User)
    .search("name", "*oh* *da*")
    .all();
from Users
where search(name, '*oh* *da*')

Warning

RavenDB allows you to search by using such queries, but you have to be aware that leading wildcards drastically slow down searches.

Consider if you really need to find substrings. In most cases, looking for whole words is enough. There are also other alternatives for searching without expensive wildcard matches, e.g. indexing a reversed version of text field or creating a custom analyzer.

Static Indexes

All of the previous examples demonstrated searching capabilities by executing dynamic queries and were using auto indexes underneath. The same set of queries can be done when static indexes are used, and also those capabilities can be customized by changing the analyzer or setting up full text search on multiple fields.

Example I - Basics

To be able to search you need to set Indexing to Search on a desired field.

class Users_ByName extends AbstractIndexCreationTask {
    constructor() {
        super();

        this.map = `docs.Users.Select(user => new {    
            name = user.name
        })`;

        this.index("name", "Search");
    }
}

const users = await session
    .query({ indexName: "Users/ByName" })
    .search("name", "John")
    .all();
from index 'Users/ByName'
where search(name, 'John')

Example II - FullTextSearch

const users = session
    .query({ indexName: "Users/Search" })
    .search("query", "John")
    .all();
from index 'Users/Search'
where search(query, 'John')