Mongo db atlas search: create atlas search index on embedded document

1.9k views Asked by At

For storing categories, I have below schema -

{
    name: String,
    description : String,
    subCategories:[
      { 
          name:String,
          description : String
      }
    ]
}

For searching, need to apply atlas search index on both category name and subcategory name. I tried with the below mappings, it didn't work for subcategory's name and description.

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
          "analyzer": "lucene.standard",
          "type": "string"
        },
      "description": {
          "analyzer": "lucene.standard",
          "type": "string"
        },
      "subCategory.name": {
          "analyzer": "lucene.standard",
          "type": "string"
      },
      "subCategory.description": {
          "analyzer": "lucene.standard",
          "type": "string"
      }
    }
  }
}

Is there something I am missing in the field mappings?

2

There are 2 answers

0
Avani Khabiya On BEST ANSWER

I found answer to this question of mine in this documentation for atlas search :

https://docs.atlas.mongodb.com/reference/atlas-search/index-definitions/

So, what I did referencing this document is, modified my mappings slightly to support what I expect. I was doing it in wrong manner earlier.

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
          "analyzer": "lucene.standard",
          "type": "string"
        },
      "description": {
          "analyzer": "lucene.standard",
          "type": "string"
        },
      "subCategory": {
        "fields": {
          "name": {
            "analyzer": "lucene.standard",
            "type": "string"
          },
          "description": {
            "analyzer": "lucene.standard",
            "type": "string"
          }
        }
      }
    }
  }
}
0
Bathri Nathan On

Here is the working code to do Atlas search on document or Object.

mainSubscriber - > object (dataType)

The type of the main subscriber must be a 'document'. Then you can specify the inner field as below.

Refer to the static map example in the documentation for more details https://docs.atlas.mongodb.com/reference/atlas-search/index-definitions#std-label-index-config-example

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "identifier": {
        "analyzer": "lucene.standard",
        "type": "string"
      },
      "mainSubscriber": {
        "fields": {
          "identifier": {
            "analyzer": "lucene.standard",
            "type": "string"
          }
        },
        "type": "document"
      },
      "profileId": {
        "analyzer": "lucene.standard",
        "type": "string"
      }
    }
  }
}