简体   繁体   English

Mongoose,无法使用 default_language 创建文本索引:'none'

[英]Mongoose, cannot create text index with default_language: 'none'

I try to create search feature using $text and $search in mongoose but it stuck when the query meet mongodb english stop words .我尝试在 mongoose 中使用$text$search创建搜索功能,但是当查询遇到 mongodb english stop words时它卡住了。 I try to change my default_language to none to ignore the stop words list but then I realized that I cannot change the default_language.我尝试将我的 default_language 更改为 none 以忽略stop words列表,但后来我意识到我无法更改 default_language。

I was wondering the way my mongoose always create index with default_language: english, even I directly use default_language: 'none' .我想知道我的猫鼬总是使用 default_language: english 创建索引的方式,即使我直接使用default_language: 'none'

Here is my code:这是我的代码:

kataSchema.index({ kata: "text" }, { default_language: 'none' });

and then when I open mongo shell and type db.katas.getIndexes(), it's always showing me this:然后当我打开 mongo shell 并输入 db.katas.getIndexes() 时,它总是向我显示:

[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "v" : 2,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "kata_text",
        "background" : true,
        "weights" : {
            "kata" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 3
    }
]

I also try to delete the index in mongo shell and run my code again but it still use english as its default_language.我还尝试删除 mongo shell 中的索引并再次运行我的代码,但它仍然使用英语作为其 default_language。

Do I miss at something or else?我想念什么吗? I was already searching for it everywhere but still cannot find the issue.我已经到处寻找它,但仍然找不到问题。 Thanks for advice.谢谢你的建议。

mongoose version: "^5.10.0"猫鼬版本:“^5.10.0”

I guess the problem comes from the .index itself .. Use createIndexes instead.我猜问题来自.index本身.. 使用createIndexes代替。

In your model's schema, try this:在您模型的架构中,试试这个:

.
.
.
const Kata = mongoose.model('Kata', kataSchema);
Kata.createIndexes({ kata: "text" }, { default_language: 'none' });

Had quite the same issue with Mongoose 5.10.0 and MongoDB 4.4.0 : Mongoose 5.10.0和 MongoDB 4.4.0有完全相同的问题:

contentSchema.index(
{
    title: "text",
    shortDescription: "text",
    description: "text",
},
{
    weights: {
        title: 10,
        shortDescription: 5,
        description: 1
    },
    default_language: "german",
    name: "SearchIndex"
});

weights and default_language were just ignored. weightsdefault_language被忽略了。 Updating Mongoose:更新猫鼬:

npm update mongoose

Mongoose is now version 5.10.16 and default_language as well as weights are working correctly without any code modifications. Mongoose 现在是5.10.16版, default_language权重都可以正常工作,无需任何代码修改。

You will have to drop the wrongly created index first before the new one is applied.在应用新索引之前,您必须先删除错误创建的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM