---
title: "Full-Text Search with Index"
url: "https://docs.ravendb.net/6.2/indexes/querying/searching"
version: "6.2"
language: "nodejs"
---

# Full-Text Search with Index

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/indexes/querying/searching.md), [Java](https://docs.ravendb.net/6.2/indexes/querying/searching.java.md), [Python](https://docs.ravendb.net/6.2/indexes/querying/searching.python.md), [PHP](https://docs.ravendb.net/6.2/indexes/querying/searching.php.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Prior to reading this article, please refer to [Full-Text search with dynamic queries](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md)
>   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.  
>     Learn more in [Setting analyzer for index-field](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#setting-analyzer-for-index-field).
>
> - In this article:
>   - [Indexing single field for FTS](#indexing-single-field-for-fts)
>   - [Indexing multiple fields for FTS](#indexing-multiple-fields-for-fts)
>   - [Indexing all fields for FTS (using AsJson)](#indexing-all-fields-for-fts-using-asjson)
>   - [Indexing dynamic fields for FTS](#indexing-dynamic-fields-for-fts)
>   - [Boosting search results](#boosting-search-results)
>   - [Analyzer-generated phrases in static-index searches](#analyzer-generated-phrases-in-static-index-searches)
>   - [Searching with wildcards](#searching-with-wildcards)
>     - [When using RavenStandardAnalyzer or StandardAnalyzer or NGramAnalyzer](#when-usingoror)
>     - [When using a custom analyzer](#when-using-a-custom-analyzer)
>     - [When using the Exact analyzer](#when-using-the-exact-analyzer)

## Indexing single field for FTS

#### The index:

```javascript
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](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md) for all available **Search options**.

##### Query

```javascript
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.
```

##### RQL

```sql
from index "Employees/ByNotes"
where search(employeeNotes, "French")
```

## Indexing multiple fields for FTS

#### The index:

```javascript
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("employeeData", "Search");

      // Note:
      // Since no analyzer is set then the default 'RavenStandardAnalyzer' is used.
  }
}
```

#### Sample query:

##### Query

```javascript
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
```

##### RQL

```sql
from index "Employees/ByEmployeeData"
where (search(employeeData, "Manager") or search(employeeData, "French Spanish", and))
```

## Indexing all fields for FTS (using AsJson)

- To search across ALL fields in a document without defining each one explicitly, use the `AsJson` method,  
  which is available when using **a C# LINQ string** that is assigned to the `map` property in the Node.js index class,
  as shown in the example below.

- This approach makes the index robust to changes in the document schema.  
  By calling `.Select(x => x.Value)` on the result of `AsJson(...)`,
  the index automatically includes values from ALL existing and newly added properties
  and there is no need to update the index when the document structure changes.

- > **Info**
  >
  > This indexing method is supported only when using **Lucene** as the indexing engine.

#### The index:

```javascript
// Extend the index class from 'AbstractCsharpIndexCreationTask':
class Products_ByAllValues extends AbstractCsharpIndexCreationTask {
  constructor () {
      super();

      // Using a C# LINQ string:  
      this.map = `docs.Products.Select(product => new {
                      AllValues = this.AsJson(product).Select(x => x.Value)
                  })`;

      // Configure the index-field for FTS:
      // Set 'Search' on index-field 'AllValues'
      this.index("AllValues", "Search");

      // Note:
      // Since no analyzer is set, the default 'RavenStandardAnalyzer' is used.

      // Set the search engine type to Lucene:
      this.searchEngineType = "Lucene";
  }
}
```

#### Sample query:

##### Query

```javascript
const products = await session
  .query({ indexName: "Products/ByAllValues" })
  .search("AllValues", "tofu")
  .all();

// * Results will contain all Product documents that have 'tofu'
//   in ANY of their fields.
//
// * Search is case-insensitive since the default analyzer is used.
```

##### RQL

```sql
from index "Products/ByAllValues"
where search(AllValues, "tofu")
```

## Indexing dynamic fields for FTS

- [Dynamic index fields](https://docs.ravendb.net/6.2/indexes/using-dynamic-fields.nodejs.md) are created at indexing time, with names and values derived from document content,  
  rather than being predefined as fixed index fields in the index definition.  
  This allows you to query the index by fields that are not known at index definition or deployment time.

- Dynamic index fields can be configured for full-text search just like regular index fields.  
  Learn more in [Indexing dynamic fields for full-text search](https://docs.ravendb.net/6.2/indexes/using-dynamic-fields.nodejs.md#indexing-dynamic-fields-for-full-text-search).

## 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](https://docs.ravendb.net/6.2/indexes/boosting.nodejs.md).

  - Add a boost value to the queried terms **at query time**.  
    Refer to article [Boost search results](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/boost-search-results.nodejs.md).

## Analyzer-generated phrases in static-index searches

- The rules for parsing `search()` input and matching multiple tokens produced from one search term are described in
  [Search with analyzer-generated phrases](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md#search-with-analyzer-generated-phrases).

- The phrase-matching behavior is the same for dynamic and static-index queries and applies to both Corax and Lucene.

- The difference for a static index is that you can select the analyzer used for each searchable index-field.
  The selected analyzer determines whether one search term remains one token or produces multiple tokens that RavenDB searches as a phrase.

---

### How the index-field analyzer affects the query

The analyzer configured for an index-field normally processes both the field content at indexing time and the search terms at query time.

For example, selected built-in analyzers process the same search term as follows:

| Analyzer configured for the index-field | Query-time tokens from `sales%representative` | Query behavior |
| - | - | - |
| [RavenStandardAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#using-the-default-search-analyzer) | `sales`, `representative` | Searches the two tokens as a **phrase** |
| [StandardAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#analyzers-that-remove-common-stop-words) | `sales`, `representative` | Searches the two tokens as a **phrase** |
| [WhitespaceAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#analyzers-that-do-not-remove-common-stop-words) | `sales%representative` | Searches for one case-sensitive token;<br />no analyzer-generated phrase is created |

> **Info**
>
> #### Query-time analyzer exceptions:
>
> - When an index-field uses [NGramAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#analyzers-that-tokenize-according-to-the-defined-number-of-characters), RavenDB uses the default search analyzer instead of `NGramAnalyzer`
>   to process full-text search terms at query time.
>
> - Search terms that use wildcards follow the analyzer rules described in
>   [Searching with wildcards](#searching-with-wildcards).
>
> Learn more in [Analyzers at query time](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#analyzers-at-query-time).

---

> **Note**
>
> ### Example
>
> The `Employees_ByEmployeeData` index defined in
> [Indexing multiple fields for FTS](#indexing-multiple-fields-for-fts)
> does not assign an analyzer to `employeeData`, so the field uses the default `RavenStandardAnalyzer`.
>
> For the following query, `RavenStandardAnalyzer` produces the tokens `sales` and `representative`.  
> RavenDB applies the phrase-matching rules described in the linked [full-text search section](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md#how-the-input-form-affects-matching).
>
> #### Query
>
> ```javascript
> const employees = await session
>    // Query the static-index
>   .query({ indexName: "Employees/ByEmployeeData" })
>   .search("employeeData", "sales%representative")
>   .all();
> ```
>
> #### RQL
>
> ```sql
> from index "Employees/ByEmployeeData"
> where search(employeeData, "sales%representative")
> ```
>
> For the behavior of existing Corax indexes that predate phrase-query support, see
> [Search-engine compatibility](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md#search-engine-compatibility).

## Searching with wildcards

- When making a full-text search with wildcards in the search terms,
  the presence of wildcards (`*`) in the terms sent to the search engine is determined by the transformations applied by the
  [analyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md) used in the index.

- Note the different behavior in the following cases, as described below:
  - [When using RavenStandardAnalyzer or StandardAnalyzer or NGramAnalyzer](#when-usingoror)
  - [When using a custom analyzer](#when-using-a-custom-analyzer)
  - [When using the Exact analyzer](#when-using-the-exact-analyzer)

- When using [Corax](https://docs.ravendb.net/6.2/indexes/search-engine/corax.md) as the search engine,  
  this behavior will only apply to indexes that are newly created or have been reset.

> **Note**
>
> ##### When using `RavenStandardAnalyzer` or`StandardAnalyzer` or `NGramAnalyzer`:
>
> Usually, the same analyzer used to tokenize field content at **indexing time** is also used to process the terms provided in the **full-text search query**
> before they are sent to the search engine to retrieve matching documents.
>
> **However, in the following cases**:
>
> - When making a [dynamic search query](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md)
> - or when querying a static index that uses the default [RavenStandardAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#using-the-default-search-analyzer)
> - or when querying a static index that uses the [StandardAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#analyzers-that-remove-common-stop-words)
> - or when querying a static index that uses the [NGramAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#analyzers-that-tokenize-according-to-the-defined-number-of-characters)
>
> the queried terms in the *search* method are processed with the [LowerCaseKeywordAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#using-the-default-analyzer)  
> before being sent to the search engine.
>
> This analyzer does Not remove the `*`, so the terms are sent with `*`, as provided in the search terms.  
> For example:
>
> ###### Index
>
> ```javascript
> class Employees_ByNotes_usingDefaultAnalyzer extends AbstractJavaScriptIndexCreationTask {
>
>   constructor() {
>       super();
>
>       // Define the index-fields 
>       this.map("Employees", e => ({
>           employeeNotes: e.Notes
>       }));
>
>       // Configure the index-field for FTS:
>       this.index("employeeNotes", "Search");
>
>       // Since no analyzer is explicitly set
>       // then the default 'RavenStandardAnalyzer' will be used at indexing time.
>
>       // However, when making a search query with wildcards,
>       // the 'LowerCaseKeywordAnalyzer' will be used to process the search terms
>       // prior to sending them to the search engine.
>   }
> }
> ```
>
> ###### Query
>
> ```javascript
> let explanations;
>
> const employees = await session
>   .query({ indexName: "Employees/ByNotes/usingDefaultAnalyzer" })
>    // If you request to include explanations,
>    // you can see the exact term that was sent to the search engine.
>   .includeExplanations(e => explanations = e)
>    // Provide a term with a wildcard to the search method:
>   .search("employeeNotes", "*rench")
>   .all();
>
> // Results will contain all Employee documents that have terms that end with 'rench'
> // (e.g. French). 
>
> // Checking the explanations, you can see that the search term 'rench'
> // was sent to the search engine WITH the leading wildcard, i.e. '*rench'
> // since the 'LowerCaseKeywordAnalyzer' is used in this case. 
> const explanation = explanations.explanations[employees[0].id][0];
> const expectedVal = "employeeNotes:*rench";
>
> assert.ok(explanation.includes(expectedVal), 
>   `'${explanation}' does not contain '${expectedVal}.'`);
> ```
>
> ###### RQL
>
> ```sql
> from index "Employees/ByNotes/usingDefaultAnalyzer"
> where search(EmployeeNotes, "*rench")
> include explanations()
> ```

> **Note**
>
> ##### When using a custom analyzer:
>
> - When setting a [custom analyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#creating-custom-analyzers) in your index to tokenize field content,
>   then when querying the index, the search terms in the query will be processed according to the custom analyzer's logic.
>
> - The `*` will remain in the terms if the custom analyzer allows it.
>   It is the user’s responsibility to ensure that wildcards are not removed by the custom analyzer if they should be included in the query.
>
> - Note:  
>   An exception to the above is when the wildcard is used as a suffix in the search term (e.g. `Fren*`).  
>   In this case the wildcard will be included in the query regardless of the analyzer's logic.
>
> For example:
>
> ###### Index
>
> ```javascript
> class Employees_ByNotes_usingCustomAnalyzer extends AbstractJavaScriptIndexCreationTask {
>
>   constructor() {
>       super();
>
>       this.map("Employees", e => ({
>           employeeNotes: e.Notes
>       }));
>
>       // Configure the index-field for FTS:
>       this.index("employeeNotes", "Search");
>
>       // Set a custom analyzer for the index-field:
>       this.analyze("employeeNotes", "RemoveWildcardsAnalyzer");
>   }
> }
> ```
>
> ###### Custom\_analyzer
>
> ```javascript
> // The custom analyzer:
> // ====================
>
> const removeWildcardsAnalyzer = `
>   using System.IO;
>   using Lucene.Net.Analysis; 
>   using Lucene.Net.Analysis.Standard;
>   namespace CustomAnalyzers
>   {
>       public class RemoveWildcardsAnalyzer : StandardAnalyzer
>       {
>           public RemoveWildcardsAnalyzer() : base(Lucene.Net.Util.Version.LUCENE_30)
>           {
>           }
>   
>           public override TokenStream TokenStream(string fieldName, System.IO.TextReader reader)
>           {
>               // Read input stream and remove wildcards (*)
>               string text = reader.ReadToEnd();
>               string processedText = RemoveWildcards(text);
>               StringReader newReader = new StringReader(processedText);
>               
>               return base.TokenStream(fieldName, newReader);
>           }
>   
>           private string RemoveWildcards(string input)
>           {
>               // Replace wildcard characters with an empty string
>               return input.Replace("*", "");
>           }
>       }
>   }`;
>
> // Deploying the custom analyzer:
> // ==============================
>
> const analyzerDefinition = {
>   name: "RemoveWildcardsAnalyzer",
>   code: removeWildcardsAnalyzer
> };
>
> await documentStore.maintenance.send(new PutAnalyzersOperation(analyzerDefinition));
> ```
>
> ###### Query
>
> ```javascript
> let explanations;
>
> const employees = await session
>   .query({ indexName: "Employees/ByNotes/usingCustomAnalyzer" })
>   .includeExplanations(e => explanations = e)
>    // Provide a term with wildcards to the Search method:
>   .search("employeeNotes", "*French*")
>   .all();
>
> // Even though a wildcard was provided,
> // the results will contain only Employee documents that contain the exact term 'French'.
>
> // The search term was sent to the search engine WITHOUT the wildcard,
> // as the custom analyzer's logic strips them out.
>
> // This can be verified by checking the explanations:
> const explanation = explanations.explanations[employees[0].id][0];
>
> const expectedVal = "employeeNotes:french";
> assert.ok(explanation.includes(expectedVal),
>   `'${explanation}' does not contain '${expectedVal}.'`);
>
> const notExpectedVal = "employeeNotes:*french";
> assert.ok(!explanation.includes(notExpectedVal),
>   `'${explanation}' does not contain '${notExpectedVal}.'`);
> ```
>
> ###### RQL
>
> ```sql
> from index "Employees/ByNotes/UsingCustomAnalyzer"
> where search(EmployeeNotes, "*French*")
> include explanations()
> ```

> **Note**
>
> ##### When using the Exact analyzer:
>
> When using the default Exact analyzer in your index (which is [KeywordAnalyzer](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md#using-the-default-exact-analyzer)),  
> then when querying the index, the wildcards in your search terms remain untouched.  
> The terms are sent to the search engine exactly as produced by the analyzer.
>
> For example:
>
> ###### Index
>
> ```javascript
> class Employees_ByFirstName_usingExactAnalyzer extends AbstractJavaScriptIndexCreationTask {
>
>   constructor() {
>       super();
>
>       this.map("Employees", e => ({
>           firstName: e.FirstName
>       }));
>
>       // Set the Exact analyzer for the index-field:
>       // (The field will not be tokenized)
>       this.index("firstName", "Exact");
>   }
> }
> ```
>
> ###### Query
>
> ```javascript
> let explanations;
>
> const employees = await session
>   .query({ indexName: "Employees/ByFirstName/usingExactAnalyzer" })
>   .includeExplanations(e => explanations = e)
>    // Provide a term with a wildcard to the Search method:
>   .search("firstName", "Mich*")
>   .all();
>
> // Results will contain all Employee documents with FirstName that starts with 'Mich'
> // (e.g. Michael).
>
> // The search term, 'Mich*', is sent to the search engine
> // exactly as was provided to the Search method, WITH the wildcard.
>
> const explanation = explanations.explanations[employees[0].id][0];
> const expectedVal = "firstName:Mich*";
>
> assert.ok(explanation.includes(expectedVal),
>   `'${explanation}' does not contain '${expectedVal}.'`);
> ```
>
> ###### RQL
>
> ```sql
> from index "Employees/ByFirstName/usingExactAnalyzer"
> where search(FirstName, "Mich*")
> include explanations()
> ```

## See also

- [Full-Text search](https://docs.ravendb.net/6.2/client-api/session/querying/text-search/full-text-search.nodejs.md)
- [Analyzers](https://docs.ravendb.net/6.2/indexes/using-analyzers.nodejs.md)
- [Boosting](https://docs.ravendb.net/6.2/indexes/boosting.nodejs.md)
