【问题标题】:Atlas search array of objectsAtlas搜索对象数组
【发布时间】:2021-01-01 18:55:14
【问题描述】:

我有以下架构:

{
   name: String,
   phones: [
        {
            number: String,
            type: String
        }
   ]
}

我如何索引phones.number 以便我可以写如下内容:

collection.aggregate([{
       "$search":{ 
            "compound":{
                  "should":[
                      {"autocomplete":{"query":"012345","path":"name"}},
                      {"autocomplete":{"query":"012345","path":"phones.number"}}
                  ]
             }
         }
}])

文档here 给出了字符串数组的示例,但没有给出对象数组的示例。

【问题讨论】:

  • 自动完成也不是数组的有效索引定义,afaik,

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


【解决方案1】:

根据this answer,支持按数组中子文档的属性进行索引。只需通过phones.number 创建一个索引。

更多信息请参见the documentation


编辑

我将标准索引与 Atlas Search 的索引混淆了。从文档中,您应该能够以这种方式索引文档数组:


{
  "analyzer":"lucene.standard",
  "searchAnalyzer":"lucene.standard",
  "mappings":{
    "dynamic":false,
    "fields":{
      "name":{
        "type":"string",
        "analyzer":"lucene.standard"
      },
      "phones":{
        "type":"document",
        "fields":{
          "number":{
            "type":"string",
            "analyzer":"lucene.standard"
          },
          "type":{
            "type":"string",
            "analyzer":"lucene.standard"
          }
        }
      }
    }
  }
}

【讨论】:

  • 您的思维方式实际上不支持点符号。用户需要将电话字段指定为类型document 及其对象成员。请参阅此处有关索引定义的文档:docs.atlas.mongodb.com/reference/atlas-search/…
  • @Nice-Guy 此链接上的示例是否不适用于此上下文中的多键索引? docs.mongodb.com/manual/core/index-multikey
  • @Nice-Guy 抱歉,我发现我将右舷索引与 Atlas Search 的索引混淆了。
  • 感谢它的帮助。 @Chris,atlas 搜索的索引字段的顺序是否对我们的查询(高度选择性/最不选择性)表现良好很重要?
  • yayyyÿÿyyyyyyyy
猜你喜欢
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2013-12-05
相关资源
最近更新 更多