Skip to content

Latest commit

 

History

History
255 lines (213 loc) · 12.2 KB

README.md

File metadata and controls

255 lines (213 loc) · 12.2 KB

What's Cooking with Atlas Search

A Restaurant Finder Application Demo MongoDB Atlas Search

Hello! 👋 This application allows you to search lightning fast through restaurants in English, Thai, Indonesian and Japanese based on a variety of search parameters and data types:

  • restaurant name
  • food name
  • geolocation coordinates
  • cuisine type
  • average star rating
  • borough


Note1: This dataset is mocked. Please do not use to make actual dining decisions.

Note2: This demo is multiligual version of the What's Cooking repos.

What's Cooking implements many Atlas Search features from autocomplete to custom function scoring. Using the $search operator in a MongoDB aggregation pipeline, we can build fine-grained searches across text, numerics, and geospatial data. By building out What's Cooking, you'll learn all sorts of ways MongoDB allows you to build complex, fine-grained full-text searches on your Atlas data.

No additional servers or software needed. No need to keep data in sync. Everything is done in MongoDB Atlas.

  • fuzzy matching
  • highlighting
  • autocomplete
  • range queries
  • geoqueries
  • facets
  • relevance-based scoring
  • custom function scoring
  • synonyms

Check out the demo video to see a demonstration of all the features or visit the link below to play around with the finished application, hosted entirely in MongoDB Atlas:

This application is hosted entirely by MongoDB Atlas was created using:

  • React
  • Tailwind CSS
  • MongoDB Realm for backend HTTPs endpoints and functions
  • A modified sample dataset based on MongoDB's Atlas sample_restaurants dataset

Currently this app is not suitable for mobile, but feel free to send a PR. 😊

Prerequisites

  • A MongoDB Atlas account. Get one for free here.
  • Node.js version 16 and npm.
  • Restaurant sample dataset.
  • Synonyms dataset.
  • (Recommended) MongoDB Compass - GUI

You can read and download the dataset using the MongoDB Shell, any MongoDB driver, or my favorite MongoDB Compass using the following URI:

  
mongodb+srv://mongodb:atlassearch@whatscooking.8u6sklg.mongodb.net/whatscooking

It is also included in this repo's data and indexes directory.


To Build This Application...

Prepare Data

  1. Load data to Atlas cluster:
    • database: whatscooking
    • collection: restaurants_[locale] (e.g. restaurants_en), menu_synonyms_[locale](e.g. menu_synonyms_en)
  2. Create Search indexes. (Index definitions includes in indexes directory.)
    • default
    • autocomplete
    • facetIndex

Deploy HTTPS Endpoints at your own env

  1. Create Application
  2. At your Atlas UI, click Create a New App
    Name your application whatever you wish.
    After ensuring it is linked to the correct cluster, click the CREATE APP SERVICE button.
  3. Create HTTPS Endpoints
  4. Go to HTTPS Endpoints at navigation bar at left and click Add An Endpoint
    Create following 3 HTTP endpoints
    Route Source code of function HTTP methods Respond With Result Return Type
    /restaurants/getRestaurantsAutocomplete appservice/functions/getRestaurantsAutocomplete.js GET Enable JSON
    /getRestaurants appservice/functions/getRestaurants.js POST Enable JSON
    /restaurants/getFacets appservice/functions/getFacets.js POST Enable JSON
    /synonyms/getFoodSynonyms appservice/functions/getFoodSynonyms.js POST Enable JSON

    Please keep default value for other options and click Save Draft

  5. Function Setting
  6. Go to Function at navigation bar at left
    At each function which you created, go to Settings tab, choose System at Authentigation and click Save Draft
  7. Deploy app
  8. At the top of UI, click REVIEW DRAFT & DEPLOY

Testing HTTPS endpoints

You can test each HTTPS endpoints with the following commands

/restaurants/getRestaurantsAutocomplete

command
$curl https://ap-southeast-1.aws.data.mongodb-api.com/app/application-1-rgjfz/endpoint/restaurants/getRestaurantsAutocomplete?restname="burger"&locale=en
response
[{"_id":"6095a34a7c34416a90d3212d","name":"Burger King","restaurant_id":"40370238"},{"_id":"6095a34a7c34416a90d3212e","name":"Burger King","restaurant_id":"40370167"},{"_id":"6095a34a7c34416a90d32135","name":"Burger King","restaurant_id":"40370239"},{"_id":"6095a34a7c34416a90d3214b","name":"Burger King","restaurant_id":"40370916"},{"_id":"6095a34a7c34416a90d32164","name":"Burger King","restaurant_id":"40370917"},{"_id":"6095a34a7c34416a90d32166","name":"Burger King","restaurant_id":"40372422"},{"_id":"6095a34a7c34416a90d3216b","name":"Burger King","restaurant_id":"40372618"},{"_id":"6095a34a7c34416a90d321b4","name":"Cozy Soup \u0026 Burger","restaurant_id":"40375839"},{"_id":"6095a34a7c34416a90d3228a","name":"Burger Barn Restaurant","restaurant_id":"40384486"}]

/getRestaurants

command
curl \
-H "Content-Type: application/json" \
-d '{"searchTerm": "burger ", "food": "", "operator": "text",    "dist": 1, "stars": 1, "cuisine": [], "locale": "en"}' \
https://ap-southeast-1.aws.data.mongodb-api.com/app/application-1-rgjfz/endpoint/getRestaurants

response

{"aggString":"[{\"$search\":{\"text\":{\"query\":\"burger \",\"path\":\"name\",\"fuzzy\":{\"maxEdits\":2}}}},{\"$limit\":21},{\"$project\":{\"name\":1,\"cuisine\":1,\"borough\":1,\"location\":1,\"menu\":1,\"restaurant_id\":1,\"address.street\":1,\"stars\":1,\"review_count\":1,\"PriceRange\":1,\"sponsored\":1,\"score\":{\"$meta\":\"searchScore\"},\"highlights\":{\"$meta\":\"searchHighlights\"}}}]","restaurants":[{"_id":"6095a4864ba3a04a69a79eba","address":{"street":"Pearl Street"},"borough":"Manhattan","cuisine":"Hamburgers","name":"Burger Burger","restaurant_id":"41316784","location":{"type":"Point","coordinates":[-74.0105051,40.7040805]},"stars":3.5,"review_count":159,"menu":["Bacon burger","Santa Fe burger","Ahi Tuna burger","Cheeseburger","Loaded Fries","Mushroom swiss burger","Hickory burger","Classic burger","Fajita burger","Oldtimer with cheese","French Fries","Vegetarian burger"],"PriceRange":2,"score":3.3074374198913574},{"_id":"6095a34a7c34416a90d3212d","address":{"street":"Northern Boulevard"},"borough":"Queens","cuisine":"Hamburgers","name":"Burger King","restaurant_id":"40370238","location":{"type":"Point","coordinates":[-73.89707140000002,40.7543896]},"stars":3,"review_count":38,"menu":["Cheeseburger","Ahi Tuna burger","Chili Cheeseburger","Buffalo Fries","Vegetarian burger","Loaded Fries","French Fries","Classic burger","Triple layer burger","Oldtimer with cheese","Hickory burger","Oldtimer"],"PriceRange":2,"score":2.49027419090271}, ...}

/restaurants/getFacets

command
curl \
-H "Content-Type: application/json" \
-d '{"searchTerm": "burger", "food": "", "operator": "text", "dist": 1, "stars": 1, "cuisine": [], "locale": "en"}' \
https://ap-southeast-1.aws.data.mongodb-api.com/app/application-1-rgjfz/endpoint/restaurants/getFacets

response

{"results":[{"count":{"lowerBound":183},"facet":{"cuisineFacet":{"buckets":[{"_id":"Hamburgers","count":105},{"_id":"American","count":69},{"_id":"Other","count":3},{"_id":"Jewish/Kosher","count":2},{"_id":"Pizza/Italian","count":2},{"_id":"Latin (Cuban, Dominican, Puerto Rican, South \u0026 Central American)","count":1},{"_id":"Mexican","count":1}]},"boroughFacet":{"buckets":[{"_id":"Manhattan","count":69},{"_id":"Brooklyn","count":47},{"_id":"Queens","count":36},{"_id":"Bronx","count":23},{"_id":"Staten Island","count":8}]}}}],"searchMetaStageString":"{\"$searchMeta\":{\"index\":\"facetIndex\",\"facet\":{\"operator\":{\"text\":{\"query\":\"burger\",\"path\":[\"name\",\"cuisine\"]}},\"facets\":{\"cuisineFacet\":{\"type\":\"string\",\"path\":\"cuisine\"},\"boroughFacet\":{\"type\":\"string\",\"path\":\"borough\"}}}}}","searchMetaStage":{"$searchMeta":{"index":"facetIndex","facet":{"operator":{"text":{"query":"burger","path":["name","cuisine"]}},"facets":{"cuisineFacet":{"type":"string","path":"cuisine"},"boroughFacet":{"type":"string","path":"borough"}}}}},"ok":true}

/synonyms/getFoodSynonyms

command
$curl https://ap-southeast-1.aws.data.mongodb-api.com/app/application-1-rgjfz/endpoint/synonyms/getFoodSynonyms?locale=en

response

{"foodSynonyms":[{"_id":"6268a01b5899f60be615cb66","input":["noodles"],"mappingType":"explicit","synonyms":["lo mein","chow mein","pasta","udon","ramen","spaghetti","alphabetti","macaroni","pasta"],"date_inserted":"2022-04-27T01:44:59.057Z","editable":false}],"ok":true}

To Run This Application....

  1. Clone the repo.
  2. Navigate inside whatsCooking-multilingual directory.
  3. Run npm install .
  4. Change HTTPS endpoint urls at src/hooks/useHomeFetch.js to your own ones.
  5. Change HTTPS endpoint urls at src/compenents/SearchBar.js to your own one.
  6. Change MongoDB Connectionstring at src/pages/IndexPage.js to your own one.
  7. Run npm start .

To Deploy Frontend to AWS S3...

  1. Make sure that you're using NodeJS v16 to successfully build the artifacts. If you're using nvm to manage your Node version, just type nvm use to swich to the correct NodeJS version
  2. Set AWS temporary credentials in your environment variable and ensure that you're pointing to the correct AWS account
  3. Run ./deploy_to_s3.sh
  4. The S3 bucket is fronted by CloudFront, so it might take a while for the cache to be refreshed. If you want invalidate the cache manually, following the instructions when you run the script

React Components....

Using Realm as Your Serverless Backend....

What's Cooking uses HTTP services in Realm to create 5 APIs to allow you to query for your restaurant data over HTTP:

  • GetRestaurantsEndPoint called from the useHomeFetch.js hook.
  • GetFacetsEndpoint called from the useHomeFetch.js hook.
  • Suggestions_AC_Endpoint called from the SearchBar.js component.
  • getSynonyms called in the SynonymsPage.js.





If you have any questions or feedback about this repo, feel free to create an Issue or PR in this repo.

Also please join our online MongoDB Community to interact with our product and engineering teams along with thousands of other MongoDB and Realm users.

Have fun and happy coding!