Giter VIP home page Giter VIP logo

creating-a-search-microservice's Introduction

Creating a Search Microservice

Overview

This simple tutorial shows how to expose Atlas Search functionality via an Atlas HTTPS Endpoint.

For this tutorial I'll leverage the sample_training.inspections sample dataset.

architecture

Step 1: Create the Search Index

Create a default Search index on the sample_training.inspections collection.

Step 2: Create the HTTPS Endpoint

HTTPS endpoints allow you to define custom API endpoints for your appliciations. You access them from the Atlas Realm UI:

HTTPS Endpoint

Set the Route to /inspections/search

Add Endpoint

Set the HTTP Method to GET and Response With Result to ON.

HTTP Method

Create a New Function and set the Function Name to searchInspections:

Note, the HTTPS Endpoint requires a backing function, which when created from the HTTPS Endpoint, provides example code on how to access the endpoint's arguments and payload. You are welcome to create the function first, but you will see different example code if you do.

New Function

Replace the provided sample function code with the following:

// This function is the endpoint's request handler.
exports = function({ query, headers, body}, response) {
  
    // A default value for testing from this console if searchTerm is not defined.
    var searchTerm = "Fail"

    if (query != undefined)
      if (query.searchTerm != undefined)
        searchTerm = query.searchTerm;

    console.log("searchTerm: ", searchTerm);

    var inspectinsCollection = context.services.get("mongodb-atlas").db("sample_training").collection("inspections");

    const pipeline = [{$search: {
       index: 'default',
       text: {
        query: searchTerm,
        path: {
         wildcard: '*'
        }
       }
      }}, {$addFields: {
       score: {
        $meta: 'searchScore'
       }
      }}, {
        $limit: 10
      }]

    return inspectinsCollection.aggregate(pipeline).toArray()
      .then(inspections => {
        console.log(`Successfully searched inspections for ${searchTerm}.`)

        for(const inspection of inspections) {
          console.log(`business_name: ${inspection.business_name}`)
        }
        return inspections
      })
      .catch(err => console.error(`Failed to search inspections: ${err}`))

};

Step 3: Update the searchInspections Function's Authentication

Edit the searchInspections function and set its Authentication to System

Authentication

And since the function will never be called externally, go ahead and set Private to ON.

Private

Step 4: Test

Copy the callback URL from the HTTPS Endpoints Settings:

Callback URL

Paste the URL in your browser, appending ?searchTerm=<term>. For example, let's search for "health risk"

https://data.mongodb-api.com/app/inspectionsearchservice-lxafd/endpoint/inspections/search?searchTerm=health%20risk

Result

creating-a-search-microservice's People

Contributors

mongodb-atlas-app-services[bot] avatar wbleonard avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.