Text search occurrences number

Hello,

Is there a way to get the number of occurrences total and per document using text search? Without map-reduce.

Thank you.

Hi @Roman_Right

You can use a text search using Atlas search as a recommended type (as opposed to old text indexes) and than use $META_SEARCH to get counts per document.

If you wish to get occurrences within a document you can use $split and $size - 1 of the array created in the aggregation pipeline.

I would try to do everything in an aggregation and not in map reduce which is a legacy approach

Thanks
Pavel

Hey @Pavel_Duchovny ,
Thank you it helps much.

The only problem is “$split” is case sensitive, while text search is case insensitive. Split can skip smth, which was found by text search. Or am I wrong?

Hi @Roman_Right

From what I remember case sensitive can be tuned in search indexes.

Are you using Atlas search?

Thanks
Pavel

No. I’m using common MongoDB 4.4.
I don’t see in the docs that I can turn off case insensitivity: https://docs.mongodb.com/manual/core/index-text/#case-insensitivity .

Maybe I can do smth like split but with regex to count the number of the occurrences? But I don’t see this option in the documentation

It looks like I found the solution.
I can do a case-insensitive text search and later in the next aggregation step I can do $regexFindAll to find all the occurrences for each found document and manipulate it as I want.

Thank you very much for your help.

1 Like