Text Search in Mongo: Look up item name that contains spaces


Text Search in Mongo: Look up item name that contains spaces



I have a collection in MongoDB that looks something like this (all other fields omitted for clarity)


{ "name" : "Toyota Camry" },
{ "name" : "Fort Taurus" },
{ "name" : "Ford Fiesta" }



What I want to do is to identify sentences where these items appear 'all inclusive'. For example, the texts:



I need some way of querying MongoDB, so it will give me a match on Ford Fiesta but no match on Fiesta



I have tried to use text index, but this seams to return everything. Ideally, the concept is



"Take this line of text, and see if it contains any of the stored items in the database"



EVEN BETTER:



If I can get the LONGEST match (i.e. if there's a "ford fiesta" and a "ford fiesta turbo", then I'd like the latter ONLY if it's within the text.



Any good ideas? Is this doable?





For exact match - wrap the search term in double quotes like {$search: '"Ford Fiesta"'}. For the "even better" part, how you distinguish which one is longer "ford fiesta turbo" or "ford fiesta parts", when you search for "ford fiesta" ? As a side note, the code snippet is not a valid JSON document.
– Alex Blex
2 days ago



{$search: '"Ford Fiesta"'}





I think you may have understood me wrong - I have a long sentence, and I need to see if all that text contains an item or not. If I knew the sentence contained 'Ford Fiesta' already, then I wouldn't have a problem. To add to it, lets say in my database there is also an item named "Turbo Ford Fiesta", I'd ideally NOT get a match for that if the the sentence is "I drove my Ford Fiesta Turbo"
– Pedro G. Dias
2 days ago






Ah, you mean you need something like percolate query, where search terms are stored in the db, and the sentences are your query. There is no such thing in mongo I'm afraid.
– Alex Blex
2 days ago





Ok, after heavy surfing, I am inclined to agree with you. Would lucene help me out on top of this?
– Pedro G. Dias
2 days ago





I believe so. Evidently ES guys managed to implement it using lucene: github.com/elastic/elasticsearch/tree/master/modules/percolator/…
– Alex Blex
2 days ago




1 Answer
1



You can search for specifics by making a wildcard text index


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



and then doing the following


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



This will return only strings that EXACTLY match your text.






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.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV