Combine or Negate
Group or separate items in your dataset.
You can also combine searches. Unlike our legacy search, in annotation search, Filter and Rank is a list of Annotation objects. Filtered annotations will be ANDed. When you combine both Filter and Rank, filter will be applied before ranking annotations. This is important because limiting the result set on large applications can speedup the overall query drastically when doing a ranking.
Java
NodeJS
Python
cURL
Javascript (REST)
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
// Here we search for images which we labeled with "cat" and for which the General prediction model does not find
8
// a "dog" concept.
9
MultiSearchResponse postAnnotationsSearchesResponse = stub.postAnnotationsSearches(
10
PostAnnotationsSearchesRequest.newBuilder().addSearches(
11
Search.newBuilder().setQuery(
12
Query.newBuilder()
13
.addFilters(
14
Filter.newBuilder().setAnnotation(
15
Annotation.newBuilder().setData(
16
Data.newBuilder().addConcepts( // You can search by multiple concepts.
17
Concept.newBuilder()
18
.setId("cat") // You could search by concept Name as well.
19
.setValue(1f) // Value of 0 will search for images that don't have the concept.
20
)
21
)
22
)
23
)
24
.addRanks(
25
Rank.newBuilder().setAnnotation(
26
Annotation.newBuilder().setData(
27
Data.newBuilder().addConcepts( // You can search by multiple concepts.
28
Concept.newBuilder()
29
.setId("dog") // You could search by concept Name as well.
30
.setValue(1f) // Value of 0 will search for images that don't have the concept.
31
)
32
)
33
)
34
)
35
)
36
)
37
.build()
38
);
39
40
if (postAnnotationsSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
41
throw new RuntimeException("Post annotations searches failed, status: " + postAnnotationsSearchesResponse.getStatus());
42
}
43
44
System.out.println("Found inputs " + postAnnotationsSearchesResponse.getHitsCount() + ":");
45
for (Hit hit : postAnnotationsSearchesResponse.getHitsList()) {
46
System.out.printf("\tScore %.2f for annotation % of input %s\n", hit.getScore(), hit.getAnnotation().getId(), hit.getInput().getId())
47
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
// Here we search for images which we labeled with "cat" and for which the General prediction model does not find
5
// a "dog" concept.
6
stub.PostAnnotationsSearches(
7
{
8
searches: [
9
{
10
query: {
11
filters: [
12
{
13
annotation: {
14
data: {
15
concepts: [ // You can search by multiple concepts.
16
{
17
id: "cat", // You could search by concept Name as well.
18
value: 1 // Value of 0 will search for images that don't have the concept
19
}
20
]
21
}
22
}
23
}
24
],
25
ranks: [
26
{
27
annotation: {
28
data: {
29
concepts: [ // You can search by multiple concepts.
30
{
31
id: "dog", // You could search by concept Name as well.
32
value: 0 // Value of 0 will search for images that don't have the concept
33
}
34
]
35
}
36
}
37
}
38
]
39
}
40
}
41
]
42
},
43
metadata,
44
(err, response) => {
45
if (err) {
46
throw new Error(err);
47
}
48
49
if (response.status.code !== 10000) {
50
throw new Error("Post annotations searches failed, status: " + response.status.description);
51
}
52
53
console.log("Search result:");
54
for (const hit of response.hits) {
55
console.log("\tScore " + hit.score + " for annotation: " + hit.annotation.id + " of input: ", hit.input.id);
56
}
57
}
58
);
Copied!
1
from clarifai_grpc.grpc.api import service_pb2, resources_pb2
2
from clarifai_grpc.grpc.api.status import status_code_pb2
3
4
# Insert here the initialization code as outlined on this page:
5
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
# Here we search for images which we labeled with "cat" and for which the General prediction model does not find
8
# a "dog" concept.
9
post_annotations_searches_response = stub.PostAnnotationsSearches(
10
service_pb2.PostAnnotationsSearchesRequest(
11
searches = [
12
resources_pb2.Search(
13
query=resources_pb2.Query(
14
filters=[
15
resources_pb2.Filter(
16
annotation=resources_pb2.Annotation(
17
data=resources_pb2.Data(
18
concepts=[ # You can search by multiple concepts.
19
resources_pb2.Concept(
20
id="cat", # You could search by concept Name as well.
21
value=1 # Value of 0 will search for images that don't have the concept.
22
)
23
]
24
)
25
)
26
)
27
],
28
ranks=[
29
resources_pb2.Rank(
30
annotation=resources_pb2.Annotation(
31
data=resources_pb2.Data(
32
concepts=[ # You can search by multiple concepts.
33
resources_pb2.Concept(
34
id="dog", # You could search by concept Name as well.
35
value=0 # Value of 0 will search for images that don't have the concept.
36
)
37
]
38
)
39
)
40
)
41
]
42
)
43
)
44
]
45
),
46
metadata=metadata
47
)
48
49
if post_annotations_searches_response.status.code != status_code_pb2.SUCCESS:
50
print("There was an error with your request!")
51
print("\tCode: {}".format(post_annotations_searches_response.outputs[0].status.code))
52
print("\tDescription: {}".format(post_annotations_searches_response.outputs[0].status.description))
53
print("\tDetails: {}".format(post_annotations_searches_response.outputs[0].status.details))
54
raise Exception("Post searches failed, status: " + post_annotations_searches_response.status.description)
55
56
print("Search result:")
57
for hit in post_annotations_searches_response.hits:
58
print("\tScore %.2f for annotation: %s off input: %s" % (hit.score, hit.annotation.id, hit.input.id))
Copied!
1
# Here we search for images which we labeled with "cat" and for which the General prediction model does not find
2
# a "dog" concept.
3
4
curl -X POST \
5
-H "Authorization: Key {api-key}" \
6
-H "Content-Type: application/json" \
7
-d '
8
{
9
"searches": [
10
{
11
"query": {
12
"filters": [
13
{
14
"annotation": {
15
"data": {
16
"concepts": [
17
{
18
"id":"people",
19
"value": 1
20
}
21
]
22
}
23
}
24
}
25
],
26
"ranks": [
27
{
28
"annotation": {
29
"data": {
30
"concepts": [
31
{
32
"id":"people",
33
"value": 1
34
}
35
]
36
}
37
}
38
}
39
]
40
}
41
}
42
]
43
}'\
44
https://api.clarifai.com/v2/searches
Copied!
1
const raw = JSON.stringify({
2
"user_app_id": {
3
"user_id": "{YOUR_USER_ID}",
4
"app_id": "{YOUR_APP_ID}"
5
},
6
"searches": [
7
{
8
"query": {
9
"filters": [
10
{
11
"annotation": {
12
"data": {
13
"concepts": [
14
{
15
"id":"people",
16
"value": 1
17
}
18
]
19
}
20
}
21
}
22
],
23
"ranks": [
24
{
25
"annotation": {
26
"data": {
27
"concepts": [
28
{
29
"id":"people",
30
"value": 1
31
}
32
]
33
}
34
}
35
}
36
]
37
}
38
}
39
]
40
});
41
42
const requestOptions = {
43
method: 'POST',
44
headers: {
45
'Accept': 'application/json',
46
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
47
},
48
body: raw
49
};
50
51
fetch(`https://api.clarifai.com/v2/searches`, requestOptions)
52
.then(response => response.text())
53
.then(result => console.log(result))
54
.catch(error => console.log('error', error));
Copied!
Last modified 1mo ago
Copy link
Edit on GitHub