MongoDB 自动完成的 Atlas 文本索引无法匹配
MongoDB Atlas text index with autocomplete failing to match
我在我的一个数据库集合上创建了一个文本索引,每个集合都有自动完成集。我索引的集合字段是名字、姓氏和电子邮件。以下是我用来创建文本索引的内容。成功:
{
"mappings": {
"dynamic": false,
"fields": {
"email": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
],
"firstname": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
],
"surname": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
]
}
}
}
当我 运行 mongoDB shell 中的以下查询时,我没有收到任何错误,但当姓氏字段与查询完全匹配时也没有结果:
db.users.aggregate([ { "$search": { "autocomplete":
{ "query": "janson","path": "surname", "fuzzy":
{ "maxEdits": 2, "prefixLength": 3 } } } }])
我也尝试了索引中的所有三个集合字段:
db.users.aggregate([ { "$search": { "autocomplete":
{ "query": "janson","path": ["firstname","surname", "email"], "fuzzy":
{ "maxEdits": 2, "prefixLength": 3 } } } }])
这给了我以下错误:
"Remote error from mongot :: caused by :: "path" must be a string (from "autocomplete")",
我看不出我的索引定义有什么问题,因为它已通过 atlas 验证并存在于服务器上。可能是什么问题?
谢谢
编辑
一个简化的查询:
db.groups.aggregate([ { "$search": { "autocomplete": { "query": "test", "path": "title" } } }])
Atlas索引规范:
{
"mappings": {
"dynamic": false,
"fields": {
"title": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
]
}
}
}
Atlas 声称 100% 索引的简单文档:
{
"_id": {
"$oid": "5f773d551dd78dfa97219ae2"
},
"title": "test"
}
如果您使用non-default索引,您需要指定索引名称:
db.users.aggregate([ {
"$search": {
"index": "<REPLACE_WITH_INDEX_NAME>",
"autocomplete": {
"query": "janson",
"path": "surname",
"fuzzy": {
"maxEdits": 2,
"prefixLength": 3
}
}
}
}])
下图中的 <REPLACE_WITH_INDEX_NAME>
是 SearchIndex1
:
我在我的一个数据库集合上创建了一个文本索引,每个集合都有自动完成集。我索引的集合字段是名字、姓氏和电子邮件。以下是我用来创建文本索引的内容。成功:
{
"mappings": {
"dynamic": false,
"fields": {
"email": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
],
"firstname": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
],
"surname": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
]
}
}
}
当我 运行 mongoDB shell 中的以下查询时,我没有收到任何错误,但当姓氏字段与查询完全匹配时也没有结果:
db.users.aggregate([ { "$search": { "autocomplete":
{ "query": "janson","path": "surname", "fuzzy":
{ "maxEdits": 2, "prefixLength": 3 } } } }])
我也尝试了索引中的所有三个集合字段:
db.users.aggregate([ { "$search": { "autocomplete":
{ "query": "janson","path": ["firstname","surname", "email"], "fuzzy":
{ "maxEdits": 2, "prefixLength": 3 } } } }])
这给了我以下错误:
"Remote error from mongot :: caused by :: "path" must be a string (from "autocomplete")",
我看不出我的索引定义有什么问题,因为它已通过 atlas 验证并存在于服务器上。可能是什么问题?
谢谢
编辑
一个简化的查询:
db.groups.aggregate([ { "$search": { "autocomplete": { "query": "test", "path": "title" } } }])
Atlas索引规范:
{
"mappings": {
"dynamic": false,
"fields": {
"title": [
{
"foldDiacritics": false,
"maxGrams": 7,
"minGrams": 3,
"tokenization": "edgeGram",
"type": "autocomplete"
}
]
}
}
}
Atlas 声称 100% 索引的简单文档:
{
"_id": {
"$oid": "5f773d551dd78dfa97219ae2"
},
"title": "test"
}
如果您使用non-default索引,您需要指定索引名称:
db.users.aggregate([ {
"$search": {
"index": "<REPLACE_WITH_INDEX_NAME>",
"autocomplete": {
"query": "janson",
"path": "surname",
"fuzzy": {
"maxEdits": 2,
"prefixLength": 3
}
}
}
}])
下图中的 <REPLACE_WITH_INDEX_NAME>
是 SearchIndex1
: