Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

How to Index String Fields For Faceted Search

On this page

  • Define the Index for the stringFacet Type
  • Configure Properties for the stringFacet Type
  • Try an Example for the stringFacet Type

You can use the Atlas Search stringFacet type to index string fields for faceting, which allows you to run a facet query on that field. Atlas Search doesn't apply the analyzer when indexing string fields for faceting.

Atlas Search only supports facet 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 type. When you facet on a string field inside embedded documents, Atlas Search returns facet count for only the number of matching parent documents.

Atlas Search doesn't dynamically index string values for faceting. You must use static 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 numberFacet 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.

  1. Click Refine Your Index to configure your index.

  2. In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.

  3. Click Customized Configuration.

  4. 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.

  5. Click the Data Type dropdown and select StringFacet. To learn more more about this type, see Field Properties.

  6. Click Add.

The following is the JSON syntax for the stringFacet type. Replace the default index definition with the following. To learn more about the fields, see Field Properties.

{
"mappings": {
"dynamic": true|false,
"fields": {
"<field-name>": {
"type": "stringFacet"
}
}
}
}

The Atlas 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. Atlas Search truncates BSON string values indexed as the stringFacet type to around 8000 UTF-16 code units.

The following index definition example uses the sample_mflix.movies collection. If you have the sample data already loaded on your cluster, you can use the Visual Editor or JSON 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.

The following index definition for the sample_mflix.movies collection in the sample dataset indexes the genres field as stringFacet for faceting.

  1. In the Add Field Mapping window, select genres from the Field Name dropdown.

  2. Click the Data Type dropdown and select StringFacet.

  3. Click Add.

{
"mappings": {
"dynamic": false,
"fields": {
"genres": {
"type": "stringFacet"
}
}
}
}

The following index definition for the sample_mflix.movies collection in the sample dataset indexes the genres field as stringFacet and string types to return the following types of results for your queries:

  • Metadata results for queries using Atlas Search facet.

  • Search results for queries using Atlas Search operators like text, phrase, and other operators that perform text search.

  1. In the Add Field Mapping window, select genres from the Field Name dropdown.

  2. Click the Data Type dropdown and select StringFacet.

  3. Click Add.

  4. Repeat step 1 and select String from the Data Type dropdown.

  5. Review the default setting for String Properties and click Add.

{
"mappings": {
"dynamic": false,
"fields": {
"genres": [
{
"type": "stringFacet"
},
{
"type": "string"
}
]
}
}
}

Tip

See also: Additional Index Definition Examples

Back

string