mongodb - 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):
- 选项 1:无索引。我收到了警告
No index available for this query.
。 - 选项 2:在所有这些字段上创建复合索引。我收到了警告
No index available for this query.
。 - 选项 3:为每个字段创建一个索引,我收到警告
Shard results differ
。查询比选项 1 或 2慢。
此用例是否有有效的选择?
PS:使用文本索引不是一个选项,因为模糊搜索需要正则表达式搜索(搜索词被标记化)。
编辑:在这个来自 MongoDB Atlas 团队的视频中,您可以看到一些非常快速的模糊搜索在使用MongoDB Atlas(付费托管解决方案)上可用的名为Full Text Search的功能执行。我想用免费的Community Server实现类似的功能。演讲者 Eliot Horowitz 正在谈论在 MongoDB 中嵌入Lucene 。也许这是唯一的方法?
解决方案
推荐阅读
- windows - 擦除和取消主分支会影响我的其他分支吗
- reactjs - fsevents@2.3.2 安装问题的不支持平台
- ruby-on-rails - 使用 react_component() 在 haml 文件中添加反应组件
- reactjs - 尝试使用 react-papaparse 逐行流式传输本地文件,但它不起作用 - 我是正确编码还是不可能?
- npm - Node 包管理器如何决定安装哪个版本?
- javascript - Office 脚本循环 Excel 自动化
- angular - Spring Boot中基于产品ID的多个产品详细信息
- security - 如何防止 PR 更改我的 CI 配置?
- spring - Spring 5 异常处理——ResponseStatusException 模型
- javascript - sqlMessage: '字段\'winner_statustext\'没有默认值',