From e4acc0b836af6e16c83b47c88c7b62fd68adf56b Mon Sep 17 00:00:00 2001 From: Estelle Maudet Date: Tue, 23 Oct 2018 10:14:34 +0200 Subject: [PATCH] #12 Return search results as : {result: [...], query:{...}} + Return c.JSON instead of jsonapi --- pkg/fulltext/search/query.go | 24 +++---------- web/fulltext/fulltext.go | 65 ++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/pkg/fulltext/search/query.go b/pkg/fulltext/search/query.go index 25053b3d560..73a406bf80b 100644 --- a/pkg/fulltext/search/query.go +++ b/pkg/fulltext/search/query.go @@ -1,14 +1,11 @@ package search import ( - "encoding/json" "fmt" "os" "time" "github.com/blevesearch/bleve" - "github.com/cozy/cozy-stack/pkg/couchdb" - "github.com/cozy/cozy-stack/web/jsonapi" ) type QueryRequest struct { @@ -22,24 +19,13 @@ type QueryRequest struct { } type SearchResult struct { - id string `json:"_id"` - docType string `json:"docType"` - rev string `json:"_rev"` + Id string `json:"_id"` + DocType string `json:"_type"` + Rev string `json:"_rev"` Name string `json:"name"` - Highlight map[string][]string `json:"highlight"` + Highlight map[string][]string `json:"html_highlight"` } -func (r *SearchResult) Rev() string { return r.rev } -func (r *SearchResult) ID() string { return r.id } -func (r *SearchResult) DocType() string { return r.docType } -func (r *SearchResult) Clone() couchdb.Doc { cloned := *r; return &cloned } -func (r *SearchResult) SetRev(rev string) { r.rev = rev } -func (r *SearchResult) SetID(id string) { r.id = id } -func (r *SearchResult) Relationships() jsonapi.RelationshipMap { return nil } -func (r *SearchResult) Included() []jsonapi.Object { return []jsonapi.Object{} } -func (r *SearchResult) MarshalJSON() ([]byte, error) { return json.Marshal(*r) } -func (r *SearchResult) Links() *jsonapi.LinksList { return nil } - const ( SearchPrefixPath = "bleve/query/" ) @@ -192,7 +178,7 @@ func BuildResults(request QueryRequest, searchResults *bleve.SearchResult) []Sea currFetched.Name = result.Fields["name"].(string) } if request.Rev { - currFetched.SetRev(result.Fields["_rev"].(string)) + currFetched.Rev = result.Fields["_rev"].(string) } // currFetched := SearchResult{result.ID, (result.Fields["_rev"]).(string), (result.Fields["docType"]).(string), (result.Fields["name"]).(string), result.Fragments["name"][0]} fetched[i] = currFetched diff --git a/web/fulltext/fulltext.go b/web/fulltext/fulltext.go index 785ee85430c..48fc2d84511 100644 --- a/web/fulltext/fulltext.go +++ b/web/fulltext/fulltext.go @@ -2,6 +2,7 @@ package fulltext import ( "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -14,7 +15,6 @@ import ( "github.com/cozy/cozy-stack/pkg/fulltext/indexation" "github.com/cozy/cozy-stack/pkg/fulltext/search" // "github.com/cozy/cozy-stack/web/permissions" - // "github.com/cozy/cozy-stack/pkg/couchdb" "github.com/cozy/echo" ) @@ -44,17 +44,9 @@ func SearchQuery(c echo.Context) error { request := MakeRequest(findRequest) - results, total, _ := search.QueryIndex(request) - - out := make([]jsonapi.Object, len(results)) - for i, result := range results { - fmt.Println(result.Name) - out[i] = &results[i] - } - - // TODO : return the right needed infos - return jsonapi.DataListWithTotal(c, http.StatusOK, total, out, nil) + results, _, _ := search.QueryIndex(request) + return c.JSON(http.StatusOK, map[string]interface{}{"results": results, "query": findRequest}) } func SearchQueryPrefix(c echo.Context) error { @@ -64,7 +56,9 @@ func SearchQueryPrefix(c echo.Context) error { if err := json.NewDecoder(c.Request().Body).Decode(&findRequest); err != nil { fmt.Printf("Error on decoding request: %s\n", err) - return jsonapi.NewError(http.StatusBadRequest, "Could not decode the request") + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": errors.New("Could not decode the request"), + }) } // TODO : see how to deal with permissions @@ -75,17 +69,9 @@ func SearchQueryPrefix(c echo.Context) error { request := MakeRequest(findRequest) - results, total, _ := search.QueryPrefixIndex(request) - - out := make([]jsonapi.Object, len(results)) - for i, result := range results { - fmt.Println(result.Name) - out[i] = &results[i] - } - - // TODO : return the right needed infos - return jsonapi.DataListWithTotal(c, http.StatusOK, total, out, nil) + results, _, _ := search.QueryPrefixIndex(request) + return c.JSON(http.StatusOK, map[string]interface{}{"results": results, "query": findRequest}) } func Reindex(c echo.Context) error { @@ -99,21 +85,24 @@ func Reindex(c echo.Context) error { err := indexation.ReIndex() if err != nil { fmt.Printf("Error on opening index: %s\n", err) - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } - return jsonapi.DataList(c, http.StatusOK, nil, nil) - + return c.JSON(http.StatusOK, nil) } func IndexUpdate(c echo.Context) error { err := indexation.AllIndexesUpdate() if err != nil { - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } - return jsonapi.DataList(c, http.StatusOK, nil, nil) + return c.JSON(http.StatusOK, nil) } @@ -133,34 +122,44 @@ func ReplicateIndex(c echo.Context) error { err := os.MkdirAll(path, 0700) if err != nil { fmt.Println(err) - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } tmpFile, err := ioutil.TempFile(path, "store.tmp.") if err != nil { fmt.Println(err) - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } _, err = io.Copy(tmpFile, c.Request().Body) if err != nil { fmt.Println(err) - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } err = tmpFile.Close() if err != nil { fmt.Println(err) - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } err = os.Rename(tmpFile.Name(), path+"/store") if err != nil { fmt.Println(err) - return jsonapi.DataList(c, http.StatusInternalServerError, nil, nil) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) } - return jsonapi.DataList(c, http.StatusOK, nil, nil) + return c.JSON(http.StatusOK, nil) } func MakeRequest(mapJSONRequest map[string]interface{}) search.QueryRequest {