Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

Env:

MongoDB (3.2.0) with MongoS

Collection:

users

Text Index creation:

  BasicDBObject keys = new BasicDBObject();

  keys.put("name","text");

  BasicDBObject options = new BasicDBObject();

  options.put("name", "userTextSearch");

  options.put("unique", Boolean.FALSE);

  options.put("background", Boolean.TRUE);

  userCollection.createIndex(keys, options); // using MongoTemplate

Document:

{"name":"LEONEL"}

Queries:

db.users.find( { "$text" : { "$search" : "LEONEL" } } ) => FOUND

db.users.find( { "$text" : { "$search" : "leonel" } } ) => FOUND (search caseSensitive is false)

db.users.find( { "$text" : { "$search" : "LEONÉL" } } ) => FOUND (search with diacriticSensitive is false)

db.users.find( { "$text" : { "$search" : "LEONE" } } ) => FOUND (Partial search)

db.users.find( { "$text" : { "$search" : "LEO" } } ) => NOT FOUND (Partial search)

db.users.find( { "$text" : { "$search" : "L" } } ) => NOT FOUND (Partial search)

Any idea why I get 0 results using a query "LEO" or "L"?

Regex with Text Index Search is not allowed.

db.getCollection('users')

     .find( { "$text" : { "$search" : "/LEO/i", 

                          "$caseSensitive": false, 

                          "$diacriticSensitive": false }} )

     .count() // 0 results

db.getCollection('users')

     .find( { "$text" : { "$search" : "LEO", 

                          "$caseSensitive": false, 

                          "$diacriticSensitive": false }} )

.count() // 0 results

Mongo Documentation:

https://docs.mongodb.com/v3.2/text-search/

https://docs.mongodb.com/manual/reference/operator/query/text/

https://docs.mongodb.com/manual/core/index-text/

https://jira.mongodb.org/browse/SERVER-15090

1 Answer

0 votes
by (108k points)
edited by

As at MongoDB 3.4, the text search characteristic is planned to support case-insensitive searches on text content with language-specific rules for stopwords and stemming. Stemming rules for supported languages are based on approved algorithms which generally handle common verbs and nouns but are unaware of proper nouns.

There is no explicit support for partial or fuzzy matches, but terms that stem from a similar result may appear to be working as such. For instance: "taste", "tastes", and tasteful" all stem to "tast". Try the Snowball Stemming Demo page to sample with more words and stemming algorithms.

Your results that match are all changes in the same word "LEONEL", and vary only by case and diacritic. Unless "LEONEL" can be stemmed from something shorter by the rules of your selected language, these are the only type of variations that will match.

Want to learn MongoDB from the experts, Check out this MongoDB Training in United States to enhance your Knowledge!

Related questions

0 votes
1 answer
asked Feb 19, 2021 in Web Technology by adhiraj (4k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 6, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 13, 2020 in Web Technology by ashely (50.2k points)

Browse Categories

...