首页 > 解决方案 > MongoDB / $或在集合的多个字段中搜索的最佳索引结构是什么?

问题描述

使用如下结构的 MongoDB 集合:

{
    title: {
      lang1: "Title in lang1",
      lang2: "Title in lang2"
    },
    description: {
      lang1: "Description in lang1",
      lang2: "Description in lang2"
    },
    keywords: ['keyword1','keyword2']
}

此查询的最佳索引结构是什么:

{$or: [ 
    {"title.lang1": /searchTerm/i},
    {"title.lang2": /searchTerm/i},
    {"description.lang1": /searchTerm/i},
    {"description.lang2": /searchTerm/i},
    {"keywords": /searchTerm/i},
]}

到目前为止我测试过的内容(使用 MongoDB Compass):

此用例是否有有效的选择?

PS:使用文本索引不是一个选项,因为模糊搜索需要正则表达式搜索(搜索词被标记化)。

编辑:在这个来自 MongoDB Atlas 团队的视频中,您可以看到一些非常快速的模糊搜索在使用MongoDB Atlas(付费托管解决方案)上可用的名为Full Text Search的功能执行。我想用免费的Community Server实现类似的功能。演讲者 Eliot Horowitz 正在谈论在 MongoDB 中嵌入Lucene 。也许这是唯一的方法?

标签: mongodbmongodb-querymongodb-indexes

解决方案


推荐阅读