MongoDB diacriticInSensitive 搜索未按预期显示所有重音(带有变音符号的单词)行,反之亦然

标签 mongodb search diacritics text-search accent-insensitive

我有一个具有以下结构的文档集合

uid, name

有索引

db.Collection.createIndex({name: "text"})

它包含以下数据

1, iphone
2, iphóne
3, iphonë
4, iphónë

当我对 iphone 进行文本搜索时 我只得到两条记录,这是出乎意料的

actual output
--------------
1, iphone
2, iphóne

如果我搜索 iphonë

db.Collection.find( { $text: { $search: "iphonë"} } );

I am getting
---------------------
3, iphonë
4, iphónë

但实际上我期待以下输出

db.Collection.find( { $text: { $search: "iphone"} } );
db.Collection.find( { $text: { $search: "iphónë"} } );

    Expected output
    ------------------
    1, iphone
    2, iphóne
    3, iphonë
    4, iphónë

我在这里遗漏了什么吗? 如何通过搜索 iphoneiphónë 获得超出预期的输出?

最佳答案

mongodb 3.2 以来,text indexes变音符号不敏感:

With version 3, text index is diacritic insensitive. That is, the index does not distinguish between characters that contain diacritical marks and their non-marked counterpart, such as é, ê, and e. More specifically, the text index strips the characters categorized as diacritics in Unicode 8.0 Character Database Prop List.

所以下面的查询应该有效:

db.Collection.find( { $text: { $search: "iphone"} } );
db.Collection.find( { name: { $regex: "iphone"} } );

但看起来分音符 ( ¨ ) 存在错误,即使它在 unicode 8.0 列表中被分类为变音符号(JIRA 上的问题:SERVER-29918)

解决方案

mongodb 3.4 开始,您可以使用 collation它允许您执行这种查询:

例如,要获得预期的输出,请运行以下查询:

db.Collection.find({name: "iphone"}).collation({locale: "en", strength: 1})

这将输出:

{ "_id" : 1, "name" : "iphone" }
{ "_id" : 2, "name" : "iphône" }
{ "_id" : 3, "name" : "iphonë" }
{ "_id" : 4, "name" : "iphônë" }

在排序规则中,strength 是要执行的比较级别

  • 1 : 仅基本字符
  • 2 : 变音符号敏感
  • 3 :区分大小写 + 区分变音符号

关于MongoDB diacriticInSensitive 搜索未按预期显示所有重音(带有变音符号的单词)行,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138189/

相关文章:

node.js - 使用 Mongoose 自定义连接

MongoDB 自定义排序顺序,用于带分页的查询

python 搜索列表中的元素是否是另一个列表的子集,无需内置函数

excel - Microsoft Excel 会破坏 .csv 文件中的变音符号?

postgresql - 用多字符字符串替换字符

c# - 有什么办法可以跳过 GeoNear() MongoDB 方法中的某些文档吗?

regex - MongoDB正则表达式匹配问题

ruby-on-rails - 在Rails中使用ruby中的哈希表查询/过滤器

vue.js - 惯性清除我的搜索输入类型

sql - mysqldump 重音符号