Mongo DB Atlas搜索:在嵌入式文档上创建Atlas搜索索引

时间:2020-10-08 09:55:44

标签: mongodb mongodb-atlas mongodb-atlas-search

对于存储类别,我具有以下架构-

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

要进行搜索,需要在类别名称和子类别名称上都应用图集搜索索引。我尝试使用以下映射,但不适用于子类别的名称和描述。

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

字段映射中是否缺少某些内容?

2 个答案:

答案 0 :(得分:0)

我在本文档的图集搜索中找到了我的这个问题的答案:

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

因此,我确实参考了本文档,只是略微修改了映射以支持我的期望。我以前做错了。

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

答案 1 :(得分:0)

这是对文档或对象进行 Atlas 搜索的工作代码。

ma​​inSubscriber -> 对象(数据类型)

主订阅者的类型必须是“文档”。然后你可以指定内部字段如下。

更多细节参考文档中的静态地图示例 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"
      }
    }
  }
}