Search and set up triggers

💡 3 min read

Learn how to search for a term and view all the database items within a collection


# Requirements

In this chapter, you'll search your Datasource for a term that only shows up in one file.

You'll also view all the items in your Datasource.

Info

You'll need to have a workspace and Datasource set up, and identifiers for these stored in variables for the HTTP calls in this chapter to work correctly.

Please follow the steps in Create folders and upload files.

If you're already comfortable with the Onna API, please add a workspace and Datasource and upload the files found in the chapter How to create a static Datasource.

Please download dogs.txt (opens new window) and put it in the same directory you run your code from.

Make sure that workspace_id and canonical_ds_id both have values.

Info

Uploaded files enter a processing queue and are searchable only after they exit the processing pipeline.

There might be some time between the two events.

You can always check the results of the API calls on your Onna account.

Make sure that you're authenticated with your Onna instance and that your code has the following:

import cgi
import io
import os
import json
import requests

...

workspace_id = ...
canonical_ds_id = ...

1
2
3
4
5
6
7
8
9
10
11

You'll set up a search for the term 'sightless creature', which only occurs in one of the resources in our Datasource.

Onna searches are conducted via a POST to the @frontsearch endpoint (opens new window).

payload = {
    "facets": [],
    "size": 10,
    "from": 0,
    "hide_children": True,
    "advanced": {"and": [{"text_contains": [{"var": "extracted_text"},"sightless"]},
                         {"text_contains": [{"var": "extracted_text"},"creature"]},
                         {"and_not": [{"text_contains": [{"var":"subresource"},"quip"]}]}
                        ]},
    }
resp = requests.post(f'{base_url}/api/{container}/{account}/@frontsearch', headers=headers, data=json.dumps(payload))
resource = resp.json()['member'][0]['@name']
print(f"View search results {resource}")
1
2
3
4
5
6
7
8
9
10
11
12
13

Next, you'll GET the resource and inspect its title.

# get the resource so you can ensure that its the one you want
resp = requests.get(f"{base_url}/api/{container}/{account}/{canonical_ds_id}/{resource}", headers=headers)
resp.json()['title']
assert resp.json['title'] == 'A_Quiet_Place.txt'
1
2
3
4

Info

The assert on the title of the matched document isn't necessary.

Searching is a significant function of the Onna platform - this barely scratches the surface of the search functionality!

# Upload another file

Uploading a file via the API requires two calls - POST to create the file, PATCH to provide the content for it.

First, POST the file to our Datasource

# upload a file with the word dogs in it

with open('dogs.txt', "rb") as read_file:
    fd = read_file.read()

headers = {'Accept': 'application/json', 'Authorization': "Bearer {}".format(jwt_token)}
payload = {
    '@type': 'Resource',
    '@behaviors': ['onna.canonical.behaviors.metadata.IMetadata'],
    'title': 'dogs.txt'
    }
resp = requests.post(f'{base_url}/api/{container}/{account}/{workspace_id}/{canonical_ds_id}', headers=headers, data=json.dumps(payload))
upload_resource_id = resp.json()['@id'] # full url
upload_resource_name = resp.json()['@name']
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Next, PATCH the file with content

# PATCH
headers = {'Accept': 'text/plain', 'Authorization': "Bearer {}".format(jwt_token),
        'Content-Type': 'application/octet-stream',
        'x-upload-size': str(os.path.getsize('dogs.txt')),
        'x-upload-filename': 'dogs.txt'
        }
resp = requests.patch(f'{base_url}/api/{container}/{account}/{workspace_id}/{canonical_ds_id}/{upload_resource_name}/@upload/file', headers=headers, data=fd)
resp.status_code
1
2
3
4
5
6
7
8

Once the uploaded file has been processed, check your email!

You should have a notification from Onna like this one:

Integrations diagram


Last Updated: 6/7/2021, 7:39:45 AM