0

Basically I'm going through a large yelp database in my mongodb, and I want to find all reviews with the phrase hot wings and keyword discount. But I can't create the right query that will look for the review that has the phrase hot wings AND the keyword discount in the review.

To give you some background, I imported several large json files of yelp review into my mongodb database on ubuntu. I want to look for keyword "UFC" under "text" and "Alcohol: full_bar" under attributes and return a count of them, at bare minimum. Because I want to see if bars that mentioned UFC and MMA get more reviews and checkins and tips than other bars, that do not mention those things.

I've built this index successfully in my mongodb database:

> db.collection.createIndex({"text":"text", "attributes": "text"})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

This is what I have:

db.collection.find( { $text: { $search: "\"hot wings\" 'discount'" } } )

So far it only gave me reviews with hot wings in there, and discount in there. Is there way to show only reviews contains phrase hot wing AND keyword discount?

And is there way to search for multiple phrases and a key word at the same time?

Community
  • 1
  • 1
NewSQLguy
  • 53
  • 8

1 Answers1

0

this should work:

db.collection.find({
$and: [
    {'text': {'$regex': 'hot wings'} },
    {'text': {'$regex': 'discount'} }
]})

You can add more phrases to the $and array.

Joseph Milane
  • 184
  • 1
  • 9
  • in this example though, you're searching on the actual fields, and not the fields added to the collection's text index. – Joseph Milane Apr 29 '17 at 23:14
  • what i am saying is you don't need to add an index to run the exclusive search you are looking for, where the text contains both 'hot wings' and 'discount'. You can run the regex search that I showed on specific fields in your collection. In your case, you can run it on the text or attributes fields. count would work on the query as well, just add '.count()' to the end of the query I provided. – Joseph Milane Apr 30 '17 at 00:13
  • You didn't do it wrong. It all depends on what you want. The first query searches for 'hot wings' within all the fields in the $text index, the second one searches within the text field only. It looks like you added both the text and attributes fields to the $text index, so it will give you more results. – Joseph Milane Apr 30 '17 at 00:20
  • hey joe are you there for later? – NewSQLguy Apr 30 '17 at 01:23