Search code examples
mongodbmongodb-atlasmongodb-atlas-search

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


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?


Solution

  • 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"
              }
            }
          }
        }
      }
    }