Skip to content

Commit

Permalink
feat: add nl interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jueli12 committed Aug 25, 2024
1 parent 528bc27 commit da5116c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
37 changes: 34 additions & 3 deletions pkg/core/handler/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/core/manager/search/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/search/storage/elasticsearch/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/infra/search/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

const (
Equals = "="
NLPatternType = "nl"
DSLPatternType = "dsl"
SQLPatternType = "sql"
)
Expand Down

0 comments on commit da5116c

Please sign in to comment.