Full-Text Search with Index
-
Prior to reading this article, it is recommended to take a look at the Full-Text search with dynamic queries article to learn about the
search
method. -
All capabilities provided by
search
with a dynamic query can also be used when querying a static-index. -
However, as opposed to making a dynamic search query where an auto-index is created for you,
when using a static-index:-
You must configure the index-field in which you want to search.
See examples below. -
You can configure which analyzer will be used to tokenize this field.
See selecting an analyzer.
-
Indexing single field for FTS
The index:
class Employees_ByNotes extends AbstractJavaScriptIndexCreationTask {
constructor() {
super();
// Define the index-fields
this.map("Employees", e => ({
employeeNotes : e.Notes
}));
// Configure the index-field for FTS:
// Set 'Search' on index-field 'employeeNotes'
this.index("employeeNotes", "Search");
// Optionally: Set your choice of analyzer for the index-field.
// Here the text from index-field 'employeeNotes' will be tokenized by 'WhitespaceAnalyzer'.
this.analyze("employeeNotes", "WhitespaceAnalyzer");
// Note:
// If no analyzer is set then the default 'RavenStandardAnalyzer' is used.
}
}
Query with Search:
-
Using the
search
method has the advantage of using any of its functionalities,
such as using wildcards, searching for multiple terms, etc. -
Refer to Full-Text search with dynamic queries for all available Search options.
const employees = await session
// Query the index
.query({ indexName: "Employees/ByNotes" })
// Call 'Search':
// pass the index field name that was configured for FTS and the term to search for.
.search("employeeNotes", "French")
.all();
// * Results will contain all Employee documents that have 'French' in their 'Notes' field.
//
// * Search is case-sensitive since field was indexed using the 'WhitespaceAnalyzer'
// which preserves casing.
from index "Employees/ByNotes"
where search(employeeNotes, "French")
Indexing multiple fields for FTS
The index:
class Employees_ByEmployeeData extends AbstractJavaScriptIndexCreationTask {
constructor() {
super();
// Define the index-fields
this.map("Employees", e => ({
// Multiple document-fields can be indexed
// into the single index-field 'employeeData'
employeeData : [e.FirstName, e.LastName, e.Title, e.Notes]
}));
// Configure the index-field for FTS:
// Set 'Search' on index-field 'employeeNotes'
this.index("employeeNotes", "Search");
// Note:
// Since no analyzer is set then the default 'RavenStandardAnalyzer' is used.
}
}
Sample query:
const employees = await session
// Query the static-index
.query({ indexName: "Employees/ByEmployeeData" })
.openSubclause()
// A logical OR is applied between the following two Search calls:
.search("employeeData", "Manager")
// A logical AND is applied between the following two terms:
.search("employeeData", "French Spanish", "AND")
.closeSubclause()
.all();
// * Results will contain all Employee documents that have:
// ('Manager' in any of the 4 document-fields that were indexed)
// OR
// ('French' AND 'Spanish' in any of the 4 document-fields that were indexed)
//
// * Search is case-insensitive since the default analyzer is used
from index "Employees/ByEmployeeData"
where (search(employeeData, "Manager") or search(employeeData, "French Spanish", and))
Boosting search results
-
In order to prioritize results, you can provide a boost value to the searched terms.
This can be applied by either of the following:-
Add a boost value to the relevant index-field inside the index definition.
Refer to article indexes - boosting. -
Add a boost value to the queried terms at query time.
Refer to article Boost search results.
-