Skip to main content

Get By Predicate

The GetPred request retrieves entries from a store that match a specified predicate condition on their metadata.

  • Input: Store name and predicate condition.

  • Behavior: Returns all entries whose metadata matches the predicate condition.

  • Response: A list of matching entries.

Click to expand source code
import { createDbClient } from "ahnlich-client-node";
import { GetPred } from "ahnlich-client-node/grpc/db/query_pb";
import { PredicateCondition, Predicate, Equals } from "ahnlich-client-node/grpc/predicate_pb";
import { MetadataValue } from "ahnlich-client-node/grpc/metadata_pb";

async function getByPredicate() {
const client = createDbClient("127.0.0.1:1369");

const response = await client.getPred(
new GetPred({
store: "my_store",
condition: new PredicateCondition({
kind: {
case: "value",
value: new Predicate({
kind: {
case: "equals",
value: new Equals({
key: "label",
value: new MetadataValue({ value: { case: "rawString", value: "A" } }),
}),
},
}),
},
}),
})
);

console.log(`Found ${response.entries.length} entries with label='A'`);
}

getByPredicate();

Parameters​

ParameterTypeRequiredDescription
storestringYesThe name of the store
conditionPredicateConditionYesThe filter condition

Available Predicates​

PredicateDescription
EqualsMatch exact value
NotEqualsExclude exact value
InMatch if value is in a given set
NotInMatch if value is not in a given set
ContainsMatch if string contains substring
NotContainsMatch if string does not contain substring

Example with NotEquals​

Click to expand source code
import { createDbClient } from "ahnlich-client-node";
import { GetPred } from "ahnlich-client-node/grpc/db/query_pb";
import { PredicateCondition, Predicate, NotEquals } from "ahnlich-client-node/grpc/predicate_pb";
import { MetadataValue } from "ahnlich-client-node/grpc/metadata_pb";

async function getNotEqualsPredicate() {
const client = createDbClient("127.0.0.1:1369");

const response = await client.getPred(
new GetPred({
store: "my_store",
condition: new PredicateCondition({
kind: {
case: "value",
value: new Predicate({
kind: {
case: "notEquals",
value: new NotEquals({
key: "status",
value: new MetadataValue({ value: { case: "rawString", value: "archived" } }),
}),
},
}),
},
}),
})
);

console.log(`Found ${response.entries.length} non-archived entries`);
}

getNotEqualsPredicate();

Example with AND Condition​

Click to expand source code
import { createDbClient } from "ahnlich-client-node";
import { GetPred } from "ahnlich-client-node/grpc/db/query_pb";
import {
PredicateCondition,
Predicate,
Equals,
AndCondition
} from "ahnlich-client-node/grpc/predicate_pb";
import { MetadataValue } from "ahnlich-client-node/grpc/metadata_pb";

async function getWithAndCondition() {
const client = createDbClient("127.0.0.1:1369");

const response = await client.getPred(
new GetPred({
store: "my_store",
condition: new PredicateCondition({
kind: {
case: "and",
value: new AndCondition({
left: new PredicateCondition({
kind: {
case: "value",
value: new Predicate({
kind: {
case: "equals",
value: new Equals({
key: "category",
value: new MetadataValue({ value: { case: "rawString", value: "electronics" } }),
}),
},
}),
},
}),
right: new PredicateCondition({
kind: {
case: "value",
value: new Predicate({
kind: {
case: "equals",
value: new Equals({
key: "brand",
value: new MetadataValue({ value: { case: "rawString", value: "Apple" } }),
}),
},
}),
},
}),
}),
},
}),
})
);

console.log(`Found ${response.entries.length} Apple electronics`);
}

getWithAndCondition();

Notes​

  • For optimal performance, create predicate indices on frequently filtered fields
  • Complex conditions (AND/OR) are supported for advanced filtering
  • See Type Meanings for more details on predicates