Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

How to Index Numeric Values for Faceted Search

On this page

  • Review numberFacet Limitations
  • Define the Index for the numberFacet Type
  • Configure numberFacet Field Properties
  • Try an Example for the numberFacet Type

You can use the Atlas Search numberFacet type to index numeric values using the specified representation for faceting. You can index numbers of BSON types int32, int64, and double.

Atlas Search only supports facet queries against fields indexed as the numberFacet type. To perform a normal search also on the same field, you must index the field as type number 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 number values for faceting. You must use static mappings to index number values for faceting. You can use the Visual Editor or the JSON Editor in the Atlas UI to index number fields as the numberFacet type.

The following limitations apply:

  • You can't index decimal128 for faceting.

  • You can't index numeric values in arrays or in a document contained in an array for faceting.

  • You can't facet over numeric fields indexed as part of an embeddedDocuments field.

To define the index for the numberFacet 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 NumberFacet.

  6. Configure the field properties for the numberFacet type. To learn more, see Field Properties.

  7. Click Add.

The following is the JSON syntax for the numberFacet 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": "numberFacet",
"representation": "int64|double",
"indexIntegers": true|false,
"indexDoubles": true|false
}
}
}
}

The Atlas Search numberFacet type takes the following parameters:

Option
Type
Necessity
Description
Default

type

string

Required

The type of field. Value must be numberFacet.

representation

string

Optional

The data type of the field to index. Values can be one of the following BSON types:

  • int64 - for indexing large integers without loss of precision and for rounding double values to integers. You can't use this type to index large double values.

  • double - for indexing large double values without rounding.

To learn more, see example below.

double

indexIntegers

boolean

Optional

Indicates whether to index or omit indexing int32 and int64 type values. Value can be true or false. Either this or indexDoubles must be true.

true

indexDoubles

boolean

Optional

Indicates whether to index or omit indexing double type values. Value can be true or false. Either this or indexIntegers must be true.

true

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 example index definition indexes the year field as the Atlas Search numberFacet type to supports queries against that field using Atlas Search facet.

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

  2. Click the Data Type dropdown and select NumberFacet.

  3. Accept the default values for the NumberFacet Properties.

  4. Click Add.

Replace the default index definition with the following index definition.

{
"mappings": {
"dynamic": false,
"fields": {
"year": {
"type": "numberFacet"
}
}
}
}

The following example index definition indexes the year field as the numberFacet and number 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 near, equals, and range.

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

  2. Click the Data Type dropdown and select NumberFacet.

  3. Accept the default values for the NumberFacet Properties.

  4. Click Add.

  5. Repeat step 1 and select Number from the Data Type dropdown.

  6. Accept the default values for the Number Properties.

  7. Click Add.

Replace the default index definition with the following index definition.

{
"mappings": {
"dynamic": false,
"fields": {
"year": [
{
"type": "numberFacet"
},
{
"type": "number"
}
]
}
}
}

Tip

See also: Additional Index Definition Examples

See the following pages for additional index definition examples:

Back

number