Skip to main content

Method: Search Events - C Sharp

public async static Task<SearchEventResponse> SearchEvent(SearchEventRequest search) {}
ParameterTypeDescription
searchSearchEventRequestEvent search parameters

A method of the Event Agent.

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

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

Parameter Type

SearchEventRequest

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

Properties:

PropertyTypeDescription
QueriesList<Query>Query conditions; default: empty collection
ExcludesList<Query>Exclude conditions; default: empty collection
FiltersList<Filter>Filter conditions; default: empty collection
SortsList<Sort>Sort conditions; default: empty collection
AggregationAggregation?Aggregation syntax; default: null
FromulongReturn events start from nth queried result; default: 0
SizeulongMaximum query size; default: 0

Constructors:

public SearchEventRequest()
public SearchEventRequest(
List<Query> queries, // Queries
List<Query> excludes, // Excludes
List<Filter> filters, // Filters
List<Sort> sorts, // Sorts
Aggregation aggregation, // Aggregation
ulong from, // From
ulong size // Size
)

Sub Parameter Type

Query

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

Properties:

PropertyTypeDescription
FieldstringEvent field name
QueryTypeIQueryTypeMatch type and parameters

Constructor:

public Query(
string field, // Field
IQueryType queryType // QueryType
)

IQueryType

The following classes are derived from IQueryType:

public class Match : IQueryType {}
public class MatchPhrase : IQueryType {}
public class Term : IQueryType {}

All classes has the following properties:

PropertyDescription
Valuestring value to be matched to the event field

Each has the following constructors:

public Match(string value)
public MatchPhrase(string value)
public Term(string value)

These classes represent three match types 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 matchees 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.

Properties:

PropertyTypeDescription
FieldstringEvent field name
QueryIQueryFilter type and parameters

Constructor:

public Filter(
string field, // Field
IQuery query // Query
)

IQuery

The following classes are derived from IQuery:

public class Range : IQuery {}
public class Wildcard : IQuery {}

Properties of these classes:

Property of RangeTypeDescription
Gteulong?Greater than or equal to (null = no limit)
Lteulong?Less than or equal to (null = no limit)
Property of WildcardTypeDescription
ValuestringValue with wildcard operators to be matched to the event field

Each has the following constructors:

public Range()

public Wildcard(
string value // Value
)

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.

Properties:

PropertyTypeDescription
FieldstringEvent field name
OrderSortOrderSort order

Constructor:

public Sort(
string field, // Field
SortOrder order // Order
)

SortOrder

public enum SortOrder
{
Asc = 1, // Ascending
Desc = 2 // Descending
}

Aggregation

Properties:

PropertyType
QueriesList<Query>
Sizeulong?
AfterDictionary<string, string>

Constructors:

public Aggregation()
public Aggregation(
List<Query> queries, // Queries
ulong? size, // Size
Dictionary<string, string> after // After
)

Query (Aggregation)

Properties:

PropertyType
Fieldstring
AggregationTypeIAggregation

Constructor:

public Query(
string field, // Field
IAggregation aggregationType // AggregationType
)

IAggregation (Aggregation)

The following classes are derived from IQuery:

public class Terms : IAggregation {}
public class DateHistogram : IAggregation {}

Properties of these classes:

Property of Terms (Aggregator)Type
OrderSortOrder?
Property of DateHistogram (Aggregator)Type
Intervalstring
OrderSortOrder?

Each has the following constructors:

public Terms(
SortOrder order // Order
)
public DateHistogram(
string interval, // Interval
SortOrder order // Order
)

Return Type

SearchEventResponse

Search result of matched events.

PropertyTypeDescription
EventsList<Event>Queried events (empty collection if none matches)
CountulongNumber of events to be queried (Size parameter from SearchEventRequest)
TotalulongActual queried number of events
TookulongQuery time (milllisecond)
AggregationAggregationResult?Aggregation results

Sub Return Types

Event

Queried event object.

info

The properties are not the field names for query, exclude, filter or sort conditions.

PropertyTypeDescription
LabelIdstringEvent label ID
LabelNamestringEvent label name
SourceDigitalIdentitystringSource digital identity
TargetDigitalIdentitystringTarget digital identity
MetastringMeta
TypenumberType or group
TimestampDateTimeEmit timestamp
SequenceulongEmit sequence
ExecutionIdstringExecution ID
TaskIdstringTask ID
DataProcessIdentityContextVersionedIdentityContextData process identity
LogicIdentityContextVersionedIdentityContextLogic identity

VersionedIdentityContext

Refer to: VersionedIdentityContext

AggregationResult

PropertyType
AfterDictionary<string, string>
BucketsList<Dictionary<string, string>>

Examples

Search Events

var query = await EventAgent.SearchEvent(
new SearchEventRequest
{
Queries = new List<Query>
{
new Query(
"label_name",
new Query.Match("some label name")
),
new Query(
"source_digital_identity",
new Query.Match("some source DID")
),
// more match conditions...
},
Size = 1000,
}
);

var events = query.Events;

foreach(var event in events)
{
string labelName = event.LabelName;
string meta = event.Meta;
string logicPid = event.LogicIdentityContext.PermanentIdentity.ToString();
}

Exclude Events

var query = await EventAgent.SearchEvent(
new SearchEventRequest
{
Excludes = new List<Query>
{
new Query(
"target_digital_identity",
new Query.Match("some target DID")
),
// more match conditions...
},
Size = 1000,
}
);

var events = query.Events;

Filter Events

Filter with Range

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

var query = await EventAgent.SearchEvent(
new SearchEventRequest
{
Filters = new List<Filter>
{
new Filter(
"timestamp",
new Range{
Gte = DateTimeOffset.ToUnixTimeSeconds() - 60 * 60,
Lte = DateTimeOffset.ToUnixTimeSeconds()
}
),
// more match conditions...
},
Size = 1000,
}
);

var events = query.Events;

Filter with Wildcard

var query = await EventAgent.SearchEvent(
new SearchEventRequest
{
Filters = new List<Filter>
{
new Filter(
"label_name",
new Wildcard("some?name*")
),
// more match conditions...
},
Size = 1000,
}
);

var events = query.Events;

Sort Events

var query = await EventAgent.SearchEvent(
new SearchEventRequest
{
Sorts = new List<Sort>
{
new Sort(
"target_digital_identity",
SortOrder.Desc
),
// more match conditions...
},
Size = 1000,
}
);

var events = query.Events;

Search Events with Multiple Conditions

var query = await EventAgent.SearchEvent(
new SearchEventRequest
{
Queries = new List<Query>
{
...
},
Excludes = new List<Query>
{
...
},
Filters = new List<Filter>
{
...
},
Sorts = new List<Sort>
{
...
},
From = 0,
Size = 1000,
}
);

var events = query.Events;