Skip to main content

Method: Search Events - JS/TS

async search(request: Search): Promise<SearchResult>
ParameterTypeDescription
requestSearchEvent search parameters

A method of the Event Agent.

Search events in the event store that fit the search parameters.

Returns type SearchResult, which includes an array of queried events.

Parameter Type​

Importable from @fstnetwork/loc-logic-sdk

Event search parameters. If one search parameter have no conditions, set it to null.

PropertyTypeDescription
queries?Query[] | nullQuery conditions
excludes?Query[] | nullExclude conditions
filters?Filter[] | nullFilter conditions
sort?Sort[] | nullSort conditions
aggregation?Aggregation | nullAggregation syntax
from?number | nullReturn events start from nth queried result
size?number | nullMaximum query size

Sub Parameter Type​

Query​

Conditions to match (or not match) value in specific fields. All conditions will be applied for searching events.

type Query =
| {
field: string;
type: "match";
value: string;
}
| {
field: string;
type: "match_phrase";
value: string;
}
| {
field: string;
type: "term";
value: string;
};
FieldsDescription
fieldEvent field name
typeMatch type ("match", "match_phrase" or "term")
valuestring value to be matched

The three match types are based on Elasticsearch API:

  • match: the value matches any word in the field (fuzzy search). Standard full-text search, suitable for most use cases.
  • match_phrase: words order in the value matches words order in the field. For example, value "has been" matches it has been raining.
  • term: the value matches exactly to the field.

Filter​

Conditions to filter value in specific fields using either range filter or wildcard filter. All conditions will be applied for filtering events.

type Filter =
| {
field: string;
gte?: number | null;
lte?: number | null;
type: "range";
}
| {
field: string;
type: "wildcard";
value: string;
};
FieldsTypeDescription
fieldstringEvent field name
typestringFilter type ("range" or "wildcard")
gte?number | null(Range filter only) greater than or equal to (null = no limit)
lte?number | null(Range filter only) less than or equal to (null = no limit)
valuestring(Wildcard filter only) Value with wildcard operators to be matched

The wildcard filter value can contain the following operators:

  • ?: any single character
  • *: zero to more than one characters

For example, event-?-* matches event-A-1 and event-B-123, etc.

Sort​

Conditions to sort events by specific fields. All conditions will be applied for sorting events.

interface Sort {
field: string;
order: SortOrder;
}

type SortOrder = "asc" | "desc";
Sort OrderDescription
"asc"Ascending
"desc"Descending

Aggregation​

interface Aggregation {
after?: {
[k: string]: string;
};
queries: Query[];
size?: number | null;
}

Return Type​

SearchResult​

Importable from @fstnetwork/loc-logic-sdk

Search result of matched events.

PropertyTypeDescription
eventsEvent[]Queried events (empty array if none matches)
countnumberNumber of events to be queried (size parameter from Search)
totalnumberActual queried number of events
tooknumberQuery time (milliseconds)
aggregation?AggregationResult | nullAggregation results

Sub Return Types​

Event​

Queried event object.

info

Not to be confused with Event.Event for emit(). The properties are not the field names for query, exclude, filter or sort conditions either.

PropertyTypeDescription
labelLabelEvent label
sourceDigitalIdentitystringSource digital identity
targetDigitalIdentitystringTarget digital identity
metastringMeta
typenumberType or group
timestampstringEmit timestamp
sequencestringEmit sequence
executionIdstringExecution ID
taskIdstringTask ID
dataProcessIdentityContextVersionedIdentityContextData process identity
logicIdentityContextVersionedIdentityContextLogic identity

Label​

Event label id/name.

PropertyTypeDescription
idstringEvent label ID
namestringEvent label name

VersionedIdentityContext​

Refer to: VersionedIdentityContext

AggregationResult​

interface AggregationResult {
after: {
[k: string]: string;
};
buckets: Bucket[];
[k: string]: unknown;
}

Examples​

Search Events​

const query = await EventAgent.search({
queries: [
{
field: "label_name",
type: "match",
value: "some label name",
},
{
field: "source_digital_identity",
type: "match",
value: "some source DID",
},
// more match conditions...
],
excludes: null,
filters: null,
sorts: null,
aggregation: null,
from: 0,
size: 1000,
});

const events = query?.events;

// iterate through events
events.forEach((event) => {
const labelName = event.label.name;
const meta = event.meta;
const logicPid = event.logicIdentityContext.permanentIdentity;
// ...
});

Exclude Events​

const query = await EventAgent.search({
queries: null,
excludes: [
{
field: "target_digital_identity",
type: "match",
value: "some target DID",
},
// more exclude conditions...
],
filters: null,
sorts: null,
aggregation: null,
from: 0,
size: 1000,
});

const events = query?.events;

Filter Events​

Filter with Range​

Filter events emitted in the past hour (the timestamp field can be compared using unix time):

const query = await EventAgent.search({
queries: null,
excludes: null,
filters: [
{
field: "timestamp",
gte: Date.now() - 60 * 60 * 1000,
lte: Date.now(),
type: "range",
},
// more filter conditions...
],
sorts: null,
aggregation: null,
from: 0,
size: 1000,
});

const events = query?.events;

Filter with Wildcard​

const query = await EventAgent.search({
queries: null,
excludes: null,
filters: [
{
field: "label_name",
type: "wildcard",
value: "some?name*",
},
// more filter conditions...
],
sorts: null,
aggregation: null,
from: 0,
size: 1000,
});

const events = query?.events;

Sort Events​

const query = await EventAgent.search({
queries: null,
excludes: null,
filters: null,
sorts: [
{
field: "target_digital_identity",
order: "desc",
},
// more sort conditions...
],
aggregation: null,
from: 0,
size: 1000,
});

const events = query?.events;