diff --git a/pkg/core/handler/search/search.go b/pkg/core/handler/search/search.go index 35bb3623..e903e51e 100644 --- a/pkg/core/handler/search/search.go +++ b/pkg/core/handler/search/search.go @@ -35,7 +35,7 @@ import ( // @Tags search // @Produce json // @Param query query string true "The query to use for search. Required" -// @Param pattern query string true "The search pattern. Can be either sql or dsl. Required" +// @Param pattern query string true "The search pattern. Can be either sql, dsl or nl. Required" // @Param pageSize query string false "The size of the page. Default to 10" // @Param page query string false "The current page to fetch. Default to 1" // @Success 200 {array} runtime.Object "Array of runtime.Object" @@ -52,8 +52,6 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea ctx := r.Context() logger := ctxutil.GetLogger(ctx) - //res, nil := aiMgr.ConvertTextToSQL("搜索集群cluster中kind为namespace的") - // Extract URL query parameters with default value searchQuery := r.URL.Query().Get("query") searchPattern := r.URL.Query().Get("pattern") @@ -66,9 +64,41 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea searchPage = 1 } + query := searchQuery + + if searchPattern == storage.NLPatternType { + logger.Info("-------------" + searchQuery + "-------------") + res, err := aiMgr.ConvertTextToSQL(searchQuery) + if err != nil { + handler.FailureRender(ctx, w, r, err) + return + } + searchQuery = res + } + + logger.Info("-------------" + searchQuery + "-------------") logger.Info("Searching for resources...", "page", searchPage, "pageSize", searchPageSize) res, err := searchStorage.Search(ctx, searchQuery, searchPattern, &storage.Pagination{Page: searchPage, PageSize: searchPageSize}) + if err != nil { + if searchPattern == storage.NLPatternType { + fixedQuery, fixErr := aiMgr.FixSQL(query, searchQuery, err.Error()) + if fixErr != nil { + handler.FailureRender(ctx, w, r, err) + return + } + searchQuery = fixedQuery + res, err = searchStorage.Search(ctx, searchQuery, searchPattern, &storage.Pagination{Page: searchPage, PageSize: searchPageSize}) + if err != nil { + handler.FailureRender(ctx, w, r, err) + return + } + } else { + handler.FailureRender(ctx, w, r, err) + return + } + } + if err != nil { handler.FailureRender(ctx, w, r, err) return @@ -83,6 +113,7 @@ func SearchForResource(searchMgr *search.SearchManager, aiMgr *ai.AIManager, sea Object: unObj, }) } + rt.SQLQuery = searchQuery rt.Total = res.Total rt.CurrentPage = searchPage rt.PageSize = searchPageSize diff --git a/pkg/core/manager/search/types.go b/pkg/core/manager/search/types.go index a6cc5c99..0117e73e 100644 --- a/pkg/core/manager/search/types.go +++ b/pkg/core/manager/search/types.go @@ -34,6 +34,7 @@ type UniResource struct { type UniResourceList struct { metav1.TypeMeta Items []UniResource `json:"items"` + SQLQuery string `json:"sqlQuery"` Total int `json:"total"` CurrentPage int `json:"currentPage"` PageSize int `json:"pageSize"` diff --git a/pkg/infra/search/storage/elasticsearch/search.go b/pkg/infra/search/storage/elasticsearch/search.go index 2118a68d..86245eab 100644 --- a/pkg/infra/search/storage/elasticsearch/search.go +++ b/pkg/infra/search/storage/elasticsearch/search.go @@ -47,7 +47,7 @@ func (s *Storage) Search(ctx context.Context, queryStr string, patternType strin if err != nil { return nil, errors.Wrap(err, "search by DSL failed") } - case storage.SQLPatternType: + case storage.SQLPatternType, storage.NLPatternType: sr, err = s.searchBySQL(ctx, queryStr, pagination) if err != nil { return nil, errors.Wrap(err, "search by SQL failed") diff --git a/pkg/infra/search/storage/types.go b/pkg/infra/search/storage/types.go index 7cb6c73b..c90a44c1 100644 --- a/pkg/infra/search/storage/types.go +++ b/pkg/infra/search/storage/types.go @@ -31,6 +31,7 @@ import ( const ( Equals = "=" + NLPatternType = "nl" DSLPatternType = "dsl" SQLPatternType = "sql" )