Skip to content

Commit

Permalink
added target.{id,name,type} to pg index (#1510)
Browse files Browse the repository at this point in the history
* added target.{id,name,type} to pg index

* feat: Add target properties to event query parsing and filtering

---------

Co-authored-by: ukrocks007 <[email protected]>
  • Loading branch information
deepakprabhakara and ukrocks007 authored Jul 17, 2024
1 parent c24daff commit 0cfcf8a
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
7 changes: 7 additions & 0 deletions migrations/pg/1721206581.do.more_indexed_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- SQL goes here
-- GIN jsonb_path_ops for equality queries
CREATE INDEX target_id_idx ON indexed_events USING GIN ((doc -> 'target' -> 'id') jsonb_path_ops);

-- expression indexes used for matching (@@) tsquery types
CREATE INDEX target_name_idx ON indexed_events USING GIN (to_tsvector('english', (doc -> 'target' -> 'name')));
CREATE INDEX target_type_idx ON indexed_events USING GIN (to_tsvector('english', (doc -> 'target' -> 'type')));
1 change: 1 addition & 0 deletions migrations/pg/1721206581.undo.more_indexed_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- SQL goes here
33 changes: 33 additions & 0 deletions src/models/event/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ export function getFilters(query: ParsedQuery, scope: Scope): Filter[] {
filters.push(orJoin(some));
}

if (query.target_id) {
const some = _.map(query.target_id, (id) => {
return {
where: `(doc -> 'target' -> 'id') @> ${nextParam()}`,
values: [quote(id)],
};
});

filters.push(orJoin(some));
}

if (query.actor_name) {
const some = _.map(query.actor_name, (name) => {
return {
Expand All @@ -161,6 +172,28 @@ export function getFilters(query: ParsedQuery, scope: Scope): Filter[] {
filters.push(orJoin(some));
}

if (query.target_name) {
const some = _.map(query.target_name, (name) => {
return {
where: `to_tsvector('english', (doc -> 'target' -> 'name')) @@ plainto_tsquery('english', ${nextParam()})`,
values: [name],
};
});

filters.push(orJoin(some));
}

if (query.target_type) {
const some = _.map(query.target_type, (typ) => {
return {
where: `to_tsvector('english', (doc -> 'target' -> 'type')) @@ plainto_tsquery('english', ${nextParam()})`,
values: [typ],
};
});

filters.push(orJoin(some));
}

if (query.description) {
filters.push({
where: `to_tsvector('english', (doc -> 'description')) @@ plainto_tsquery('english', ${nextParam()})`,
Expand Down
15 changes: 15 additions & 0 deletions src/models/event/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export interface ParsedQuery {
location?: string[];
external_id?: string[];
text?: string;
target_id?: string[];
target_name?: string[];
target_type?: string[];
}

const structuredQueryKeywords = [
Expand All @@ -63,6 +66,9 @@ const structuredQueryKeywords = [
"description",
"location",
"external_id",
"target.id",
"target.name",
"target.type",
"fields.*",
];

Expand Down Expand Up @@ -114,6 +120,15 @@ export function parseQuery(query: string): ParsedQuery {
if (intermediate["actor.name"]) {
parsed.actor_name = toArray(intermediate["actor.name"]);
}
if (intermediate["target.id"]) {
parsed.target_id = toArray(intermediate["target.id"]);
}
if (intermediate["target.name"]) {
parsed.target_name = toArray(intermediate["target.name"]);
}
if (intermediate["target.type"]) {
parsed.target_type = toArray(intermediate["target.type"]);
}
if (intermediate["external_id"]) {
parsed.external_id = toArray(intermediate["external_id"]);
}
Expand Down
92 changes: 92 additions & 0 deletions src/test/models/event/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,98 @@ const minScope = {
};

const tests = [
{
query: "target.id:'tasks'",
parsed: {
target_id: ["tasks"],
},
scope: minScope,
filters: [
{
where: `(doc -> 'target' -> 'id') @> $1`,
values: ['"tasks"'],
},
{ where: "project_id = $2", values: ["proj1"] },
{ where: "environment_id = $3", values: ["env1"] },
],
},
{
query: "target.id:tasks,1",
parsed: {
target_id: ["tasks", "1"],
},
scope: minScope,
filters: [
{
where: "((doc -> 'target' -> 'id') @> $1 OR (doc -> 'target' -> 'id') @> $2)",
values: ['"tasks"', '"1"'],
},
{ where: "project_id = $3", values: ["proj1"] },
{ where: "environment_id = $4", values: ["env1"] },
],
},
{
query: "target.name:'tasks'",
parsed: {
target_name: ["tasks"],
},
scope: minScope,
filters: [
{
where: "to_tsvector('english', (doc -> 'target' -> 'name')) @@ plainto_tsquery('english', $1)",
values: ["tasks"],
},
{ where: "project_id = $2", values: ["proj1"] },
{ where: "environment_id = $3", values: ["env1"] },
],
},
{
query: "target.name:tasks,100",
parsed: {
target_name: ["tasks", "100"],
},
scope: minScope,
filters: [
{
where:
"(to_tsvector('english', (doc -> 'target' -> 'name')) @@ plainto_tsquery('english', $1) OR to_tsvector('english', (doc -> 'target' -> 'name')) @@ plainto_tsquery('english', $2))",
values: ["tasks", "100"],
},
{ where: "project_id = $3", values: ["proj1"] },
{ where: "environment_id = $4", values: ["env1"] },
],
},
{
query: "target.type:'tasks'",
parsed: {
target_type: ["tasks"],
},
scope: minScope,
filters: [
{
where: "to_tsvector('english', (doc -> 'target' -> 'type')) @@ plainto_tsquery('english', $1)",
values: ["tasks"],
},
{ where: "project_id = $2", values: ["proj1"] },
{ where: "environment_id = $3", values: ["env1"] },
],
},
{
query: "target.type:tasks,100",
parsed: {
target_type: ["tasks", "100"],
},
scope: minScope,
filters: [
{
where:
"(to_tsvector('english', (doc -> 'target' -> 'type')) @@ plainto_tsquery('english', $1) OR to_tsvector('english', (doc -> 'target' -> 'type')) @@ plainto_tsquery('english', $2))",
values: ["tasks", "100"],
},
{ where: "project_id = $3", values: ["proj1"] },
{ where: "environment_id = $4", values: ["env1"] },
],
},
{
query: "action:foo.get",
parsed: {
Expand Down

0 comments on commit 0cfcf8a

Please sign in to comment.