How to Index Date Fields For Faceted Search
On this page
You can use the Atlas Search dateFacet
type for indexing date values for
faceting.
Atlas Search only supports facet queries against fields
indexed as the dateFacet
type. To perform a normal search also
on the same field, you must index the field as type date 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
date values for faceting. You must use static
mappings to index date values for
faceting. You can use the Visual Editor or the JSON Editor in
the Atlas UI to index date fields as the numberFacet
type.
Review dateFacet
Limitations
The following limitations apply:
You can't index a date field for faceting if it's inside an array or if it's inside a document in an array.
Atlas Search doesn't support date faceting over fields indexed as part of an
embeddedDocuments
field.Note
To vote for this feature and provide your feedback, use this MongoDB feedback article.
Define the Index for dateFacet
Type
To define the index for the dateFacet
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 DateFacet. To learn more more about this type, see Field Properties.
Click Add.
The following is the JSON syntax for the dateFacet
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": "dateFacet" } } } }
Configure dateFacet
Field Properties
The Atlas Search dateFacet
type takes the following parameter:
UI Field Name | JSON Option | Type | Necessity | Description |
---|---|---|---|---|
Data Type | type | string | required | Human-readable label that identifies this field type. Value must
be dateFacet . |
Try an Example for dateFacet
Type
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 released
field as the Atlas Search dateFacet
type to support queries against
that field using Atlas Search facet.
In the Add Field Mapping window, select released from the Field Name dropdown.
Click the Data Type dropdown and select DateFacet.
Click Add.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "released": { "type": "dateFacet" } } } }
The following example index definition indexes the released
field as the dateFacet
and date
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.
In the Add Field Mapping window, select released from the Field Name dropdown.
Click the Data Type dropdown and select DateFacet.
Click Add.
Repeat step 1 and select Date from the Data Type dropdown.
Click Add.
Replace the default index definition with the following index definition.
{ "mappings": { "dynamic": false, "fields": { "released": [ { "type": "dateFacet" }, { "type": "date" } ] } } }