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:
List<User> users = session
.query(User.class)
.search("Name", "John Adam")
.toList();
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:
List<User> users = session
.query(User.class)
.search("Hobbies", "looking for someone who likes sport books computers")
.toList();
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:
List<User> users = session
.query(User.class)
.search("Name", "Adam")
.search("Hobbies", "sport")
.toList();
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:
List<User> users = session
.query(User.class)
.search("Hobbies", "I love sport")
.boost(10)
.search("Hobbies", "but also like reading books")
.boost(5)
.toList();
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:
List<User> users = session
.query(User.class)
.search("Hobbies", "computers")
.search("Name", "James")
.whereEquals("Age", 20)
.toList();
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:
List<User> users = session
.query(User.class)
.search("Name", "Adam")
.andAlso()
.search("Hobbies", "sport")
.toList();
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
:
List<User> users = session
.query(User.class)
.not()
.search("Name", "James")
.toList();
According to RQL syntax it will be transformed into the query:
from Users
where exists(Name) and not search(James, 'Adam')
You can also combine search options:
List<User> users = session
.query(User.class)
.search("Name", "Adam")
.andAlso()
.not()
.search("Hobbies", "sport")
.toList();
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
List<User> users = session
.query(User.class)
.search("Name", "Jo* Ad*")
.toList();
from Users
where search(Name, 'Jo* Ad*')
Example II - Using Suffix and Postfix Wildcards
List<User> users = session
.query(User.class)
.search("Name", "*oh* *da*")
.toList();
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.
public static class Users_ByName extends AbstractIndexCreationTask {
public Users_ByName() {
map = "docs.Users.Select(user => new {" +
" Name = user.Name" +
"})";
index("Name", FieldIndexing.SEARCH);
}
}
List<User> users = session
.query(User.class, Users_ByName.class)
.search("Name", "John")
.toList();
from index 'Users/ByName'
where search(Name, 'John')
Example II - FullTextSearch
List<User> users = session
.query(User.class, Users_Search.class)
.search("Query", "John")
.toList();
from index 'Users/Search'
where search(Query, 'John')