Skip to content

Instantly share code, notes, and snippets.

@JLHasson
Created February 14, 2024 15:57
Show Gist options
  • Save JLHasson/ed387609e6b035e4e26f45257a0b1fec to your computer and use it in GitHub Desktop.
Save JLHasson/ed387609e6b035e4e26f45257a0b1fec to your computer and use it in GitHub Desktop.

Quickstart

The following is a minimal script for uploading and object to be searched, indexing and then searching that object:

import os
import time
from objective import ObjectiveClient

client = ObjectiveClient(api_key=os.environ.get("OBJECTIVE_API_KEY"))

# Add an object to the object store
print("Inserting object...")
resp = client.object_store.upsert(
    id="1",
    object={
        "title": "Sevendayz Men's Shady Records Eminem Hoodie Hoody Black Medium",
        "brand": "sevendayz",
        "imageURLHighRes": [
            "https://images-na.ssl-images-amazon.com/images/I/41gMYeiNASL.jpg"
        ],
    },
)
print(resp)

# Create a search index. Creating a search index triggers the objects to be indexed.
index = client.create_index(
    template_name="text-neural-base", fields={"searchable": ["title", "brand"]}
)
print(f"Created Index {index.id}")

status = {}
while status.get("LIVE") != 1:
    status = index.status()
    print(status)
    time.sleep(1)


# Search!
search_results = index.search(query="rap hoodies", object_fields="*")
print(search_results)

Save the code above to a file:

cat > objective_quickstart.py  # paste the code above when prompted, and hit ctrl+D to save & exit.

Now run it!

python objective_quickstart.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment