Having some doubts about the created index and internal behaviour
See original GitHub issueHi, I just installed your plugin and making some unit testing with my use case I have some troubles. Let’s say I have the next basic schema:
const articleSchema = new mongoose.Schema({
title: {
en: String,
es: String,
}
// ...
otherProp: Boolean
}):
I want the fuzzy search to be in the two nested keys of the title, so I use the plugin according to your api:
articleSchema.plugin(fuzzySearch, {
fields: [
{
name: 'title',
keys: ['en', 'es'],
minSize: 3
}
]
});
Let’s say i create the next document with title:
await new Article ({
title: {
en: 'This is an article title in english',
es: 'A title that is supposed to be in spanish'
}
},
// ...
).save();
After I check the database the n-grams are generated correctly and exist in the database.
title_fuzzy: [
{
en_fuzzy: [
'his',
'thi',
'this',
'is',
'an',
'cle',
'icl',
'tic',
'rti',
// ...
]
All cool, BUT when I did my testing, for example with the string ‘cle’ which is part of the n-grams array I have no results, not even when using more words, included in the n-gram array like: ‘arti’ or ‘artic’. The only way I get results is by writing the whole word ‘article’ which afaik is the default behaviour of the native mongodb text search.
While doing more debugging I printed the generated indexes and with surprise I saw that the text index is in the original field, this is the index object I get back:
{
v: 2,
key: { _fts: 'text', _ftsx: 1 },
name: 'title.es_text_title.en_text',
ns: 'LocalTest.articles',
background: true,
weights: { 'title.en': 1, 'title.es': 1 },
default_language: 'english',
language_override: 'language',
textIndexVersion: 3
}
Am I doing something wrong? Or why the index is not pointed to the generated n-gram arrays? If not, what’s happening?
Also, is there any way to override the default language? In the docs, mongo can work with different languages.
Thank you in advance.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (6 by maintainers)
@VassilisPallas thank you very much.
I guess I was blindfolded by going back and forth on that error. This was actually coming from a “language” field of type Array that I had on my model, which is a reserved word for mongoDB. The error wasn’t very explicit but this was the cause.
I’m sorry that I reported it here and thank you again for the great work 💪🏽 All the best.
First of all, you don’t need to create the indices by yourself, which mean you can remove these lines:
and
Now the problem can be anything. Did you try to delete the indices and let the plugin to create them?