question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having some doubts about the created index and internal behaviour

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
adrien-prototypecommented, Nov 2, 2020

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

0reactions
VassilisPallascommented, Nov 1, 2020

First of all, you don’t need to create the indices by yourself, which mean you can remove these lines:

CoachingSchema.index({
	"coachUserName_fuzzy": "text",
	"sport_fuzzy": "text",
	"title_fuzzy": "text"
})

and

UserSchema.index({
	"userName_fuzzy": "text",
	"firstName_fuzzy": "text",
	"lastName_fuzzy": "text"
})

Now the problem can be anything. Did you try to delete the indices and let the plugin to create them?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server index behaviour when doing bulk insert
My table has several indexes and a clustered one. The question is: how are those indexes updated? For each row I insert? For...
Read more >
The behaviour change wheel: A new method for ...
Thus, the BCW approach is based on a comprehensive causal analysis of behaviour and starts with the question: 'What conditions internal to ...
Read more >
Self Determination Theory and How It Explains Motivation
Deci and Ryan's Self-Determination Theory (SDT) identifies autonomy, relatedness, and competence as crucial elements of human motivation.
Read more >
Frequently Asked Questions About Query Performance
Answer: The fastest type of query is one where there is only a single CONTAINS clause, and no other conditions in the WHERE...
Read more >
SQL Server Indexed Views: The Basics - Redgate Software
Views are a valuable tool for the SQL Server Developer, because they hide complexity and allow for a readable style of SQL expression....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found