Unable to search text with Text Search Index

Hi Team,

I have created “text” search index for one of the collection. However, following script is not giving result.

db.emp.find({
“EmpId”: UUID(“b7eda8fd-41b8-4ecf-8c24-9dc06eee40a3”),
$text : {$search:"“child”", $caseSensitive:false}
})

Created Index as below:
db.emp.createIndex({“EmpId”:1,“RecordName”:“text”})

Document as below:

{
    "_id" : ObjectId("62ba4bc29aad3560c12161e2"),
    "EmpId" : UUID("b7eda8fd-41b8-4ecf-8c24-9dc06eee40a3"),
    "RecordName" : "{\"childid\":\"62ba4bc29aad3560c12161e2\",\"childname\":\"Mike Tyson\",\"Roll\":\"J001\"}"
}

I would like to return documents that contains word “child” in RecordName field.

updated Find script as below:

db.emp.find({
“EmpId”: UUID(“b7eda8fd-41b8-4ecf-8c24-9dc06eee40a3”),
 $text : {$search:"\"child\"", $caseSensitive:false}
})

You DO NOT have "child" in your document. You do have child but without the double quotes.

With double quotes your have "childid" and "childname".

I hope you have a really Really REALLY good reason to have a JSON string as a field value rather than a real JSON object. Just the fact that you are messed up with the quotes is a con point for doing that.

“RecordName” field contains child word. I would like to search the word child from the RecordName field. This is my requirement to store the JSON as String value in the document.

That was the whole point of my answer.

You query is not looking for the word child without quotes. It is looking for “child” within quotes. If you want to search for child without quotes, you have to replace

with quotes to

$search:"child"

without quotes.

This is not

Try to convince the requirement owner to change. Because

I can appreciate your reply, If you can please read very carefully about my question. I need to search word = child if it is there anywhere in the string. $text:{$search:"child"} will return docs if there is any docs match with child word. As per my given example, I have a doc with the value "RecordName" : "{\"childid\":\"62ba4bc29aad3560c12161e2\",\"childname\":\"Mike Tyson\",\"Roll\":\"J001\"}". This doc is not returning through Mongodb text search. This doc is returned when I hit following script: db.emp.find({"RecordName" : /child/}). While I need to utilise the Text Index therefore I have raised this issue after considering all other aspects.

Please read:

So basically, you DO NOT have the word child in your RecordName field but you have the substring child. You might have the words childid and childname.

I do not think you can do what you want with text search and the data you have. Search for child will not work according to the documentation I shared but childid might work.

Hi @Yatinkumar_Patel,

Text search is designed to search text based on language heuristics for word patterns. As @steevej suggested, that isn’t suitable for your pattern matching examples.

Text search uses the Snowball stemming library to find the root form of words using a set of rules or heuristics. For example, in English words ending with a single s are generally the plural form of a noun.

There’s a Snowball online demo you can use to get a sense of word stemming, or you can see this in the explain() output of a MongoDB text search query (look for the parsedTextQuery section in queryPlanner.winningPlan).

The examples you have of childid and childname are string patterns (not word patterns) so they both do not match any stemming rules and can only be matched exactly or from a plural form like childids or childnames.

If you want to find partial matches or autocomplete,I recommend considering Atlas Search, which integrates the Apache Lucene search library. Atlas Search has configurable text analyzers, operators like autocomplete, and options like highlight to show matches in results.

For a comparison of text matching approaches, please review A Decisioning Framework for MongoDB $regex and $text vs Atlas Search.

Regards,
Stennie

3 Likes

Thanks @Stennie appreciated for your reply. I understood that Text Search will not work for my given example.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.