-
Notifications
You must be signed in to change notification settings - Fork 0
Atlas Search
{
"name": "sample_supplies-sales-dynamic",
"searchAnalyzer": "lucene.standard",
"analyzer": "lucene.standard",
"collectionName": "sales",
"database": "sample_supplies",
"mappings": {
"dynamic": true
}
}The /app/search_index.json file contains the Atlas search index definition. Here is an overview of the fields defined:
- The
namefield specifies the name of the index as it will appear in MongoDB Atlas. - The
searchAnalyzerspecifies the analyzer to apply to query text before searching with it. In this example, we use the default lucene.standard analyzer. - The
analyzerdefines how Atlas search will turn a string field's contents into searchable terms. It defines the tokenizer used to extract tokens from the > text and filters to use. In this example, we use the default lucene.standard analyzer. - The
collectionNameand database define the collection and database on which to create the index. - The
mappingsobject is used to define the mapping to use, dynamic or static. In this example, we are using dynamic which means that we want Atlas > Search to automatically index all supported field types.
To create the Atlas Search index:
atlas clusters search indexes create --clusterName myAtlasClusterEDU -f /app/search_index.json
To verify that the index creation has started, please run the following command in the terminal:
atlas clusters search indexes list --clusterName myAtlasClusterEDU --db sample_supplies --collection sales
Run the following findOne() command to obtain sample document.
db.sales.findOne()
var pipeline = [
{
$search: {
index: "sample_supplies-sales-dynamic",
text: {
query: "notepad",
path: { "wildcard": "*" }
} } },
{
$set: {
score: {
$meta: "searchScore" }
}
}
]
db.sales.aggregate(pipeline)
search_index.json
{
"name": "sample_supplies-sales-static",
"searchAnalyzer": "lucene.standard",
"analyzer": "lucene.standard",
"collectionName": "sales",
"database": "sample_supplies",
"mappings": {
"dynamic": false,
"fields": {
"storeLocation": {
"type": "string"
}
}
}
}atlas clusters search indexes create --clusterName myAtlasClusterEDU -f /app/search_index.json
The compound operator within the $search aggregation stage allows us to give weight to different field and also filter our results without having to create additional aggregation stages. The four options for the compound operator are "must", "mustNot, "should", and "filter".
"must" will exclude records that do not meet the criteria. "mustNot" will exclude results that do meet the criteria. "should" will allow you to give weight to results that do meet the criteria so that they appear first. "filter" will remove results that do not meet the criteria.
$search {
"compound": {
"must": [{
"text": {
"query": "field",
"path": "habitat"
}
}],
"should": [{
"range": {
"gte": 45,
"path": "wingspan_cm",
"score": {"constant": {"value": 5}}
}
}]
}
}$searchMeta is an aggregation stage for Atlas Search where the metadata related to the search is shown. This means that if our search results are broken into buckets, using facet, we can see that in the $searchMeta stage, because those buckets are information about how the search results are formatted.
$searchMeta: {
"facet": {
"operator": {
"text": {
"query": ["Northern Cardinal"],
"path": "common_name"
}
},
"facets": {
"sightingWeekFacet": {
"type": "date",
"path": "sighting",
"boundaries": [ISODate("2022-01-01"),
ISODate("2022-01-08"),
ISODate("2022-01-15"),
ISODate("2022-01-22")],
"default" : "other"
}
}
}
}"facet" is an operator within $searchMeta. "operator" refers to the search operator - the query itself. "facets" operator is where we put the definition of the buckets for the facets.