Filter
Learn how to perform search with filters using Clarifai SDKs
The filter feature in Clarifai's search functionality allows users to narrow down search results based on specific criteria or conditions. Filters act as constraints that refine the set of results returned by a search query. Users can specify various filter parameters to tailor the search results to their needs. These parameters might include attributes such as input type, status code, metadata, or other properties associated with the data being searched.
Click here to know more about Filter.
Filter with Input Type
Filtering with input type in Clarifai enables users to narrow down search results based on the type of data input. For example, users can specify to only retrieve results that are of a particular input type, such as images, videos, or text. This functionality allows for more targeted searches, ensuring that search results align with the desired data format or content type.
- Python
- Typescript
from clarifai.client.user import User
from PIL import Image
import requests
from IPython.display import display
# Fill in your user ID, app ID, and personal access token (PAT)
USER_ID = ''
APP_ID = ''
PAT = ''
# Initialize a User object with your credentials
client = User(user_id=USER_ID, pat=PAT)
# Create an application with the specified app ID and base workflow
app = client.create_app(app_id=APP_ID, base_workflow="Universal", pat=PAT)
# URLs of images to be uploaded
urls = [
"https://images.pexels.com/photos/139257/pexels-photo-139257.jpeg",
"https://images.pexels.com/photos/1879386/pexels-photo-1879386.jpeg",
"https://images.pexels.com/photos/1071882/pexels-photo-1071882.jpeg"
]
# Initialize an Inputs object for uploading images and a Search object for searching
input_obj = app.inputs()
search = app.search(top_k=2)
# Upload images from the provided URLs
for i, url in enumerate(urls):
input_obj.upload_from_url(input_id=f"input{i}", image_url=url)
# Perform a search query to find images (filters for images only)
res = search.query(filters=[{'input_types': ['image']}])
# Retrieve the URL of the first image from the search results
for r in res:
hit = r.hits[0].input.data.image.url
break
# Print the URL of the found image
print(hit)
# Display the found image
hit_img = Image.open(requests.get(hit, stream=True).raw).resize((300, 250))
display(hit_img)
Output
https://images.pexels.com/photos/139257/pexels-photo-139257.jpeg

import { Input, Search, User } from "clarifai-nodejs";
const appId = "test_app"; // Placeholder for application ID
// Initialize a User object with your credentials
const client = new User({
userId: process.env.CLARIFAI_USER_ID,
pat: process.env.CLARIFAI_PAT,
appId,
});
// Create an application with the specified app ID and base workflow
await client.createApp({ appId, baseWorkflow: "Universal" });
// URLs of images to be uploaded
const urls = [
"https://images.pexels.com/photos/139257/pexels-photo-139257.jpeg",
"https://images.pexels.com/photos/1879386/pexels-photo-1879386.jpeg",
"https://images.pexels.com/photos/1071882/pexels-photo-1071882.jpeg",
];
// Initialize an Input object to manage input data
const input = new Input({
authConfig: {
userId: process.env.CLARIFAI_USER_ID,
pat: process.env.CLARIFAI_PAT,
appId,
},
});
// Upload each image from the provided URLs
for (let i = 0; i < urls.length; i++) {
await input.uploadFromUrl({ inputId: `input${i}`, imageUrl: urls[i] });
}
const search = new Search({
authConfig: {
userId: process.env.CLARIFAI_USER_ID,
pat: process.env.CLARIFAI_PAT,
appId,
},
topK: 2,
metric: "euclidean",
});
// Perform a search query to find images (filters for images only)
const res = search.query({ filters: [{ inputTypes: ["image"] }] });
// Retrieve the URL of the first image from the search results
let hit;
for await (const r of res) {
hit = r.hitsList?.[0]?.input?.data?.image?.url;
break;
}
// Print the URL of the found image
console.log(hit);