Looking for a way to use $search fuction via Rest API

Hello,

I’ll preface this by stating that I’m not a developer, I’m from a pharmaceutical background trying to create a tool for academic research.

I’m trying to use the atlas $search function (which provides a fuzzy search for my database hosted on mongo) in a Retool App. Currently, I am using the DATA API provided by atlas but I can only use ‘filter’ and ‘projection’ params to get results.

Is there some documentation about calling the $search function via POST since Retool supports RESTful as a method to pull data for my webapp.

Welcome to the community @Sandeep_N_A :slight_smile:

Is there some documentation about calling the $search function via POST since Retool supports RESTful as a method to pull data for my webapp.

The Atlas Search queries take the form of an aggregation pipeline stage. You will be need to use the POST /action/aggregate endpoint for the Atlas Data API for $search queries. The following Run an Aggregation Pipeline documentation may help you this although the example on the page does not have a $search stage used as part of the pipeline. However, please see the below example I’ve used against a test system which includes the $search stage:

curl --location --request POST 'https://data.mongodb-api.com/app/data-hkzxc/endpoint/data/beta/action/aggregate' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: API_KEY_REDACTED' \
--data-raw '{
    "collection":"namecoll",
    "database":"namedb",
    "dataSource":"M10Cluster",
    "pipeline": [
    	{
    		"$search": {
      			"index": "namecollsearchindex",
      			"text": {
        			"query": "Test",
        			"path": {
          				"wildcard": "*"
        			}
      			}
    		}
  		}
  	]
}'

Response:

{"documents":[
{"_id":"62730ee3d51e21ac65d9db06","name":"Test Name"},
{"_id":"62730f31d51e21ac65d9db0a","name":"John Test"}
]}

Currently, I am using the DATA API provided by atlas but I can only use ‘filter’ and ‘projection’ params to get results.

As shown in the above example, you’ll need to use the pipeline parameter in the request body.

The Atlas Data API is currently in preview so there are chances the above behaviour may change.

I hope the above helps.

Regards,
Jason

Thank you for your reply, This behaviour makes sense to me. I’ll try to implement it and see if it works.

1 Like

Thanks! This worked for me. Is there also a way to implement fuzzy search through /aggregate? Because I can now successfully match using $Search but am unable to get anything but an exact string match

1 Like

Thanks for confirming!

Is there also a way to implement fuzzy search through /aggregate? Because I can now successfully match using $Search but am unable to get anything but an exact string match

I would recommend creating a new topic for this query with details about the use case, current output, expected output, pipeline stages and anything else that may assist with finding a solution :slight_smile:

Regards,
Jason

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.