I am currently working on a project where a large database (+25.000.000 records) needs to be searched (on a local machine) in under the 1000ms. That made me think of MongoDB and the desired performance can be reached. I can do this by several different kinds of indexes, however I need to be able to match partially.
The end user is allowed to search on a couple of fields. (Not all of the fields; Need to be able to search 5/9 of the fields the rest is meta data not searchable for the end user)
I first tried creating a compound index (on all 5 of the fields) for full text search
db.tracks.find({$text:{$search:"Greatest Hits"}})
This seems to be working rather well and giving me all sorts of results with Great, Greatest and Hits on any of the compound fields.
However a user should be able to select the fields that he wants to search on. (For example: AlbumTitle, TrackTitle etc). This means that When track title is toggled off I don't want results from that column.
db.tracks.find({AlbumTitle: "Greatest Hits"})
The above query would be able to find exact matches and thus not finding the same result when searching on Greatest. This made me think of something like the following
db.tracks.find({AlbumTitle: {$text: {$search: "Greatest Hits"}}})
but that doesn't work because then $text would be an unknown operator.
If something like the above would be possible I would be able to build my query dynamically based on the toggled fields like
db.tracks.find({$or: [ {ToggledField1: ...}, {ToggeldField4: ...} ]})
and thus ensuring only the needed fields are queried. Would this at all be possible inside a MongoDB database, and if so what is the best way to achieve the desired functionality
Thanks in Advance
Copyright Notice:Content Author:「Max Hamulyák」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34794774/mongodb-full-text-search-logical-or-query