9

Is there any way to query results on Loopback+MongoDB with Diacritic-Case-Insensitive options?

For example, If I want to search for the query olimpic, and the database contains words like:

Olímpic
olimpic
Olimpic

Then, all of the above should be returned as results. I have tried with multiple queries listed below and other combinations, but nothing has worked so far.

{"where":{"name.text":{"like":"olimpic","options":"i"}}}
{"where":{"name.text":{"like":"/^olimpic$/i","options":"i"}}}
{"where":{"name.text":{"like":"/.*olimpic.*/i"}}}
{"where":{"name.text":{"regexp":"/.*olimpic.*/i"}}}

Any idea?

Thanks in advance!

7
  • Have you tried anything so far? If so, you should share it.
    – lintmouse
    Nov 2, 2015 at 15:37
  • @dustmouse all tests have been unsuccessful. when I found the solution, I will publish it
    – mhergon
    Nov 2, 2015 at 15:39
  • mhergon - That's fine. But it still doesn't hurt to show your attempt to demonstrate that you've made the effort to solve the problem.
    – lintmouse
    Nov 2, 2015 at 15:41
  • dustmouse - I added more examples...
    – mhergon
    Nov 2, 2015 at 16:01
  • @mhergon for insensitive have you tried: {"where":{"name.text":{"regexp":"/(?i).*olimpic.*/i"}}} ? Nov 6, 2015 at 15:30

1 Answer 1

8
+50

What you want ought to be possible with text indices as of version 3.1.7 of MongoDB. Please see SERVER-19557 for details. Earlier versions can not deal with diacritics.

Setting up a text index is rather easy: simply create an index on all fields you want to be searched – there can be only one text index per collection:

db.yourCollection.createIndex(
  {"name.text":"text","foo":"text"},
  {"default_language":"french"}
)

Now, to search your index, you simply do the following:

db.yourCollection.find(
  { $text: {$search:"Olimpic"} }
)

which should give you the expected results.

hth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.