From 394f380d7268010c84ec5846b5ad185076fe7fc4 Mon Sep 17 00:00:00 2001 From: ShubhamPalriwala Date: Thu, 15 Jun 2023 18:05:17 +0530 Subject: [PATCH 1/2] fix: search functionality in inventory --- handlers/resources_handler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/resources_handler.go b/handlers/resources_handler.go index f4c92c75f..67e45be08 100644 --- a/handlers/resources_handler.go +++ b/handlers/resources_handler.go @@ -230,7 +230,7 @@ func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) { } if len(query) > 0 { - clause := fmt.Sprintf("(name ilike '%s' OR region ilike '%s' OR service ilike '%s' OR provider ilike '%s' OR account ilike '%s' OR tags @> '[{\"value\":\"%s\"}]' or tags @> '[{\"key\":\"%s\"}]')", query, query, query, query, query, query, query) + clause := fmt.Sprintf("(name LIKE '%%%s%%' OR region LIKE '%%%s%%' OR service LIKE '%%%s%%' OR provider LIKE '%%%s%%' OR account LIKE '%%%s%%' OR (tags LIKE '%%%s%%'))", query, query, query, query, query, query) whereQueries = append(whereQueries, clause) } @@ -240,7 +240,7 @@ func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) { if len(filters) == 0 { if len(query) > 0 { - whereClause := fmt.Sprintf("(name ilike '%s' OR region ilike '%s' OR service ilike '%s' OR provider ilike '%s' OR account ilike '%s' OR tags @> '[{\"value\":\"%s\"}]' or tags @> '[{\"key\":\"%s\"}]')", query, query, query, query, query, query, query) + whereClause := fmt.Sprintf("(name LIKE '%%%s%%' OR region LIKE '%%%s%%' OR service LIKE '%%%s%%' OR provider LIKE '%%%s%%' OR account LIKE '%%%s%%' OR (tags LIKE '%%%s%%'))", query, query, query, query, query, query) err = handler.db.NewRaw(fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip)).Scan(handler.ctx, &resources) if err != nil { logrus.WithError(err).Error("scan failed") From 97d7e05184b7e53ce06c742b551249701cbbae5b Mon Sep 17 00:00:00 2001 From: Mohamed Labouardy Date: Thu, 15 Jun 2023 19:07:22 +0200 Subject: [PATCH 2/2] feat: support search in postgres --- handlers/resources_handler.go | 10 ++++++++-- models/resource.go | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/handlers/resources_handler.go b/handlers/resources_handler.go index 67e45be08..9b9eadec9 100644 --- a/handlers/resources_handler.go +++ b/handlers/resources_handler.go @@ -241,7 +241,14 @@ func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) { if len(filters) == 0 { if len(query) > 0 { whereClause := fmt.Sprintf("(name LIKE '%%%s%%' OR region LIKE '%%%s%%' OR service LIKE '%%%s%%' OR provider LIKE '%%%s%%' OR account LIKE '%%%s%%' OR (tags LIKE '%%%s%%'))", query, query, query, query, query, query) - err = handler.db.NewRaw(fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip)).Scan(handler.ctx, &resources) + searchQuery := fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip) + + if handler.db.Dialect().Name() == dialect.PG { + whereClause = fmt.Sprintf("(name LIKE '%%%s%%' OR region LIKE '%%%s%%' OR service LIKE '%%%s%%' OR provider LIKE '%%%s%%' OR account LIKE '%%%s%%' OR (value->>'key' LIKE '%%%s%%') OR (value->>'value' LIKE '%%%s%%'))", query, query, query, query, query, query, query) + searchQuery = fmt.Sprintf("SELECT * FROM resources CROSS JOIN jsonb_array_elements(tags) WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip) + } + + err = handler.db.NewRaw(searchQuery).Scan(handler.ctx, &resources) if err != nil { logrus.WithError(err).Error("scan failed") } @@ -268,7 +275,6 @@ func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) { query = fmt.Sprintf("SELECT DISTINCT resources.id, resources.resource_id, resources.provider, resources.account, resources.service, resources.region, resources.name, resources.created_at, resources.fetched_at, resources.cost, resources.metadata, resources.tags, resources.link FROM resources CROSS JOIN json_each(tags) WHERE resources.id NOT IN (%s) AND type='object' AND %s ORDER BY resources.id LIMIT %d OFFSET %d", strings.Trim(string(s), "[]"), whereClause, limit, skip) } } - err = handler.db.NewRaw(query).Scan(handler.ctx, &resources) if err != nil { logrus.WithError(err).Error("scan failed") diff --git a/models/resource.go b/models/resource.go index 2348bccee..785781939 100644 --- a/models/resource.go +++ b/models/resource.go @@ -17,7 +17,7 @@ type Resource struct { Metadata map[string]string `json:"metadata"` Tags []Tag `json:"tags" bun:"tags,default:'[]'"` Link string `json:"link" bson:"link"` - Value string `bun:"-"` //to be deprecated + Value string `bun:",scanonly"` //to be deprecated } type Tag struct {