简体   繁体   中英

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 . 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.

I was wondering the way my mongoose always create index with default_language: english, even I directly use 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:

[
    {
        "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.

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"

I guess the problem comes from the .index itself .. Use createIndexes instead.

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 :

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. 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.

You will have to drop the wrongly created index first before the new one is applied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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