Mongo Java Driver searching by text index

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Mongo Java Driver searching by text index



In mongo's java driver I am using $text then $search to find things in a document however, this seems to not always yield the right results such as sometimes it will return items that don't have the searched for string. Is there a reason for this?



ANSWER



You can just escape the string and mongo won't use the delimiters. Example:



{"$text" : {"$search" : ""<insert term here>""}}


{"$text" : {"$search" : ""<insert term here>""}}




1 Answer
1



As a quick preliminary note: You should generally give some more details and examples around your specific problem. However, this one can probably be answered without that. ;)



You are dealing with a text (!) index here. That means that there is quite some work done on the MongoDB server side when it creates the index; work that goes well beyond the sheer string.split() sort of thing that one would probably expect.


string.split()



The most important things to understand is that the index will not necessarily hold all values that your text contains (e.g. common words like "and" or "the" may simply be omitted) and neither will it necessarily contain the exact words that you can read in your source data but rather word stems. A very good explanation on some of the stuff that's going on here can be found here: https://blog.codecentric.de/en/2013/01/text-search-mongodb-stemming/



There's more inside the index engine that deals with languages and special characters. But that's all reasonably well documented here and here.



Lastly, when you search for a string with spaces inside a field that's covered by a text index, the following part of the documentation is also relevant to understand:



$text will tokenize the search string using whitespace and most
punctuation as delimiters, and perform a logical OR of all such tokens
in the search string.



For example, you could use the following query to find all stores
containing any terms from the list “coffee”, “shop”, and “java”:



db.stores.find( { $text: { $search: "java coffee shop" } } )



The bottom line is: There's quite a bunch of sources of potential confusion when it comes to text indexes so you want to make sure you've read up on them before you get going.





Sorry! Didn't really know how to word it. For when I created my index I used wildcard. Is it possible to prevent it from using punctuation and whitespace as delimiters?
– C McCoy
Dec 5 '17 at 11:54







I don't think so. What are you trying to achieve specifically?
– dnickless
Dec 5 '17 at 12:13





Searching through all fields for a text string.
– C McCoy
Dec 5 '17 at 12:38





Would you be able to give us some sample data and your index definition as well as a bunch of sample queries that you would want to support?
– dnickless
Dec 5 '17 at 13:46





Data looks like this: {("_id": "Stuff"), "fieldOne": 300, "fieldTwo" : "Big/dumb.potatosald", "field3" : "rawSauce", "field4" : "noKetchup"} For making the index I did db.collection.createIndex({"$**": "text"}) I would like to be able to run a query like db.collection.find({$text: {$search: "Big/dumb.potatosald"}}) and only return documents with Big/dumb.potatosald in a field
– C McCoy
Dec 5 '17 at 15:36




{("_id": "Stuff"), "fieldOne": 300, "fieldTwo" : "Big/dumb.potatosald", "field3" : "rawSauce", "field4" : "noKetchup"}


db.collection.createIndex({"$**": "text"})


db.collection.find({$text: {$search: "Big/dumb.potatosald"}})






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions