Session: Querying: How to Use Search

More complex text searching can be achieved by using the search method. This method allows you to pass one or more search terms that will be used in the searching process for a particular field (or fields).

Syntax

IDocumentQuery<T> search(String fieldName, String searchTerms);
IDocumentQuery<T> search(String fieldName, String searchTerms, SearchOperator operator);
IDocumentQuery<T> boost(double boost);
Parameters
fieldName String Points index field that should be used for querying.
searchTerms String Space separated terms e.g. 'John Adam' means that we will look in selected field for 'John' or 'Adam'. Wildcards can be specified.
boost double Boost value. Default: 1.
operator SearchOperator Explicitly set relation between each Search function. One of the following: OR, AND.

Example I - Dynamic Query

List<User> users = session
    .query(User.class)
    .search("Name", "a*")
    .toList();
from Users 
where search(Name, 'a*')

Example II - Query Using Static Index

List<User> users = session.query(User.class, Users_ByNameAndHobbies.class)
    .search("Name", "Adam")
    .search("Hobbies", "sport")
    .toList();
from index 'Users/ByNameAndHobbies' 
where search(Name, 'Adam') or search(Hobbies, 'sport')

Example III - Boosting Usage

List<User> users = session
    .query(User.class, index("Users/ByHobbies"))
    .search("Hobbies", "I love sport")
    .boost(10)
    .search("Hobbies", "but also like reading books")
    .boost(5)
    .toList();
from index 'Users/ByHobbies' 
where boost(search(Hobbies, 'I love sport'), 10) or boost(search(Hobbies, 'but also like reading books'), 5)

Note

To leverage the searching capabilities with the usage of static indexes, please remember to enable full-text search in field settings of the index definition.