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
query.search(fieldName, searchTerms);
query.search(fieldName, searchTerms, operator);
query.boost(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 | number | Boost value. Default: 1 . |
operator | SearchOperator |
Explicitly set relation between each Search function. One of the following: "OR" , "AND" . |
Example I - Dynamic Query
const users = await session.query({ collection: "Users" })
.search("name", "a*")
.all();
from Users
where search(name, 'a*')
Example II - Query Using Static Index
const users = await session.query({ indexName: "Users/ByNameAndHobbies" })
.search("name", "Adam")
.search("hobbies", "sport")
.all();
from index 'Users/ByNameAndHobbies'
where search(name, 'Adam') or search(hobbies, 'sport')
Example III - Boosting Usage
const users = await session
.query({ indexName: "Users/ByHobbies" })
.search("hobbies", "I love sport")
.boost(10)
.search("hobbies", "but also like reading books")
.boost(5)
.all();
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.