Skip to content

Commit

Permalink
#12 Return search results as : {result: [...], query:{...}} + Return …
Browse files Browse the repository at this point in the history
…c.JSON instead of jsonapi
  • Loading branch information
Estelle Maudet committed Oct 23, 2018
1 parent 8791d8e commit e4acc0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 52 deletions.
24 changes: 5 additions & 19 deletions pkg/fulltext/search/query.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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/"
)
Expand Down Expand Up @@ -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
Expand Down
65 changes: 32 additions & 33 deletions web/fulltext/fulltext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fulltext

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)

}

Expand All @@ -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 {
Expand Down

0 comments on commit e4acc0b

Please sign in to comment.