How to Index Numeric Values
On this page
You can use the Atlas Search number
type to index fields with numeric
values of int32
, int64
, and double
data types.
You can use the equals, range, and near
operators to query indexed fields of type number
.
Note
To query numeric values in arrays, you can use only the range operator.
Atlas Search doesn't automatically index numeric values for faceting. Instead, you must index the numeric values using numberFacet to run a facet query on number fields.
Atlas Search automatically indexes all numeric fields in indexes created after July 2023 for sorting the Atlas Search results. For preexisting indexes, you must rebuild the index to use number fields in the index for sorting. To learn more, see Update Your Existing Index and Sort Atlas Search Results.
If you enable dynamic mappings, Atlas Search
automatically indexes fields of type number
. You can
use the Visual Editor or the JSON Editor in the Atlas UI to
index fields as the number
type.
Define the Index for the number
Type
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 Number.
Configure the field properties for the
number
type. To learn more, see Field Properties.Click Add.
The following is the JSON syntax for the number
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": "number", "representation": "int64|double", "indexIntegers": true|false, "indexDoubles": true|false } } } }
Configure number
Field Properties
The Atlas Search number
type has the following parameters:
UI Field Name | JSON Option | Type | Necessity | Description | Default |
---|---|---|---|---|---|
Data Type | type | string | Required | Human-readable label that identifies this field type.
Value must be number . | |
Representation | representation | string | Optional | Data type of the field to index. Values are:
To learn more, see example below. | double |
Index Integers | indexIntegers | boolean | Optional | Flag that 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 .
To learn more, see example below. | true |
Index Doubles | indexDoubles | boolean | Optional | Flag that indicates whether to index or omit indexing double
type values. Value can be true or false .
Either this or indexIntegers must be true .
To learn more, see example below. | true |
Try an Example for the number
Type
The following index definition examples use multiple collections in the sample data. If you have the sample data already loaded on your cluster, you can use the Visual Editor and JSON Editor to configure these indexes. 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_analytics.accounts
collection in the sample
dataset indexes the
account_id
field with 64-bit integer values. The
following example also:
Indexes all other integer values in the
account_id
field.Rounds any decimal values and indexes small double type values in the
account_id
field.
In the Add Field Mapping window, select account_id from the Field Name dropdown.
Click the Data Type dropdown and select Number.
Modify the Number Properties to set the following:
Representation to
int64
.Index Integers to
true
.Index Doubles to
true
.
Click Add.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "account_id": { "type": "number", "representation": "int64" } } } }
The following index definition for the
sample_analytics.accounts
collection in the
sample dataset indexes integer
values and omits doubles values in the the account_id
.
In the Add Field Mapping window, select account_id from the Field Name dropdown.
Click the Data Type dropdown and select Number.
Modify the Number Properties to set the following:
Representation to
int64
.Index Integers to
true
.Index Doubles to
false
.
Click Add.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "account_id": { "type": "number", "representation": "int64", "indexDoubles": false } } } }
The following index definition for the
sample_airbnb.listingsAndReviews
collection in the
sample dataset indexes
double
type values and omits the 32-bit and 64-bit integer
values in the bathrooms
field.
In the Add Field Mapping window, select bathrooms from the Field Name dropdown.
Click the Data Type dropdown and select Number.
Modify the Number Properties to set the following:
Representation to
doubles
.Index Integers to
false
.Index Doubles to
true
.
Click Add.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "bathrooms": { "type": "number", "indexIntegers": false } } } }