Kru*_*ngi 5 search text mongodb
我正在 mongoDB 服务器版本 2.6.3 中尝试 mongoDB 文本搜索。当我使用以下查询执行文本搜索时
db.collection.find({"$text" : {"$search" : "a@b"}})
Run Code Online (Sandbox Code Playgroud)
我得到的结果文件包含 a 或 b,这意味着我得到的结果是
db.collection.find({"$text" : {"$search" : "a b"}})
Run Code Online (Sandbox Code Playgroud)
我猜 mongo 文本解析器正在考虑将'@'字符作为分隔符并将其替换为空格。有没有办法可以指定'@'不应将字符视为分隔符?
要匹配短语(而不是单个术语),请将短语括在转义双引号中(\"),如下所示:
"\"a@b\""
Run Code Online (Sandbox Code Playgroud)
所以你的查询看起来像:
db.collection.find({"$text" : {"$search" : "\"a@b\""}})
Run Code Online (Sandbox Code Playgroud)
例如:
db.collection.insert([
{"x": "a@b"},
{"x": "a b"},
{"x": "b"},
{"x": "a"}
]);
db.collection.createIndex({ x: "text"});
Run Code Online (Sandbox Code Playgroud)
查询db.collection.find({"$text" : {"$search" : "\"a@b\""}})返回
/* 0 */
{
"_id" : ObjectId("5549ddce180e849972939043"),
"x" : "a@b"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1385 次 |
| 最近记录: |