How to Index String Fields For Faceted Search
- How to Index String Fields For Faceted Search
- How to Index String Fields For Faceted Search
- Define the Index for the [stringFacet](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-bson-data-types-string-facet Type
- Configure Properties for the [stringFacet](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-bson-data-types-string-facet Type
- Try an Example for the [stringFacet](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-bson-data-types-string-facet Type
- Learn More
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
How to Index String Fields For Faceted Search
You can use the MongoDB Search stringFacet type to index [string](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-string fields for faceting, which allows you to run a [facet](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref operator query on that field. MongoDB Search doesn't apply the analyzer when indexing string fields for faceting.
MongoDB Search only supports [facet](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref operator queries against fields indexed as the stringFacet type. To perform a normal search also on the same field, you must index the field as type string also.
To facet on string fields in embedded documents, you must index the parent fields as the [document](https://www.mongodb.com/docs/search/indexes/field-types/document-type.md#std-label-bson-data-types-document type. When you facet on a string field inside embedded documents, MongoDB Search returns facet count for only the number of matching parent documents.
MongoDB Search doesn't [dynamically](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings index string values for faceting. You must use [static mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings to index string values for faceting. You can use the Visual Editor or the JSON Editor in the Atlas UI to index string fields as the number type.
Define the Index for the [stringFacet](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-bson-data-types-string-facet Type
To define the index for the stringFacet type, choose your preferred configuration method in the Atlas UI and then select the database and collection.
-
Click Refine Your Index to configure your index.
-
In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.
-
Click Customized Configuration.
-
Select the field to index from the Field Name dropdown.
Note:
You can't index fields that contain the dollar ($) sign at the start of the field name.
-
Click the Data Type dropdown and select StringFacet. To learn more more about this type, see [Field Properties](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-fts-field-types-string-facet-options.
-
Click Add.
The following is the JSON (Javascript Object Notation) syntax for the stringFacet type. Replace the default index definition with the following. To learn more about the fields, see [Field Properties](https://www.mongodb.com/docs/search/indexes/field-types/vector-type.md#std-label-fts-vector-field-type-properties.
{
"mappings": {
"dynamic": true|false,
"fields": {
"<field-name>": {
"type": "stringFacet"
}
}
}
}
Configure Properties for the [stringFacet](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-bson-data-types-string-facet Type
The MongoDB Search stringFacet type has the following parameters:
| UI Field Name | JSON Option | Type | Necessity | Description |
|---|---|---|---|---|
| Data Type | type |
string | Required | Human-readable label that identifies this field type. Value must be stringFacet. MongoDB Search truncates BSON (Binary Javascript Object Notation) string values indexed as the stringFacet type to around 8000 UTF-16 (16-bit Unicode Transformation Format) code units. |
Try an Example for the [stringFacet](https://www.mongodb.com/docs/search/indexes/field-types/string-facet-type.md#std-label-bson-data-types-string-facet Type
The following index definition example uses the [sample_mflix.movies](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix collection. If you have the [sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-sample-data already loaded on your cluster, you can use the Visual Editor or JSON (Javascript Object Notation) Editor in the Atlas UI to configure the index. After you select your preferred configuration method, select the database and collection, and refine your index to add field mappings.
stringFacet Type Example
The following index definition for the sample_mflix.movies collection in the [sample dataset](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets indexes the genres field as stringFacet for faceting.
In the Add Field Mapping window, select genres from the Field Name dropdown.
Click the Data Type dropdown and select StringFacet.
Click Add.
{
"mappings": {
"dynamic": false,
"fields": {
"genres": {
"type": "stringFacet"
}
}
}
}
Multiple Types Example
The following index definition for the sample_mflix.movies collection in the [sample dataset](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets indexes the genres field as stringFacet and string types to return the following types of results for your queries:
-
Metadata results for queries using MongoDB Search [
facet(MongoDB Search Operator).](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref -
Search results for queries using MongoDB Search operators like [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, and other operators that perform text search.
-
In the Add Field Mapping window, select genres from the Field Name dropdown.
-
Click the Data Type dropdown and select StringFacet.
-
Click Add.
-
Repeat step 1 and select String from the Data Type dropdown.
-
Review the default setting for String Properties and click Add.
{
"mappings": {
"dynamic": false,
"fields": {
"genres": [
{
"type": "stringFacet"
},
{
"type": "string"
}
]
}
}
}
Learn More
To learn more about the facet collector and see example queries, see [Examples.](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-egs
To learn how to create an index with a facet definition and query that index using the facet collector, see the [How to Use Facets with MongoDB Search](https://www.mongodb.com/docs/search/tutorial/facet-tutorial.md#std-label-facet-tutorial tutorial.
To see an example index and query that uses stringFacet to run a faceted search with paginated results, see [Use Facet with the Paginated Results.](https://www.mongodb.com/docs/search/query/paginate-results.md#std-label-sequential-pagination-tutorial-facet-query
