Skip to content

Commit

Permalink
Seach Created
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestang12 committed Jan 17, 2021
1 parent f2a60b8 commit 98a1e36
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/url_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import (
func mapUrls() {
router.HandleFunc("/items", controllers.ItemsController.Create).Methods(http.MethodPost)
router.HandleFunc("/items/{id}", controllers.ItemsController.Get).Methods(http.MethodGet)
router.HandleFunc("/items/search", controllers.ItemsController.Search).Methods(http.MethodPost)

}
15 changes: 15 additions & 0 deletions src/clients/elasticsearch/es_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type esClientInterface interface {
setClient(*elastic.Client)
Index(string, string, interface{}) (*elastic.IndexResponse, error)
Get(string, string, string) (*elastic.GetResult, error)
Search(string, elastic.Query) (*elastic.SearchResult, error)
}

type esClient struct {
Expand Down Expand Up @@ -72,3 +73,17 @@ func (c *esClient) Get(index string, docType string, id string) (*elastic.GetRes
}
return result, nil
}

func (c *esClient) Search(index string, query elastic.Query) (*elastic.SearchResult, error) {
ctx := context.Background()
// if err := c.client.Search(index).Query(query).Validate(); err != nil {
// fmt.Println("result" + err.Error())
// return nil, nil
// }
result, err := c.client.Search(index).Query(query).RestTotalHitsAsInt(true).Do(ctx)
if err != nil {
logger.Error(fmt.Sprintf("error when trying to search documents in index %s", index), err)
return nil, err
}
return result, nil
}
28 changes: 28 additions & 0 deletions src/controllers/items_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"../../../bookstore_oauth_go/oauth"
"../../../bookstore_utils_go/rest_errors"
"../domain/items"
"../domain/queries"
"../services"
"../utils/http_utils"
)
Expand All @@ -22,6 +23,7 @@ var (
type itemsControllerInterface interface {
Create(http.ResponseWriter, *http.Request)
Get(http.ResponseWriter, *http.Request)
Search(http.ResponseWriter, *http.Request)
}

type itemsController struct {
Expand Down Expand Up @@ -90,3 +92,29 @@ func (c *itemsController) Get(w http.ResponseWriter, r *http.Request) {
}
http_utils.RespondJson(w, http.StatusOK, item)
}

func (c *itemsController) Search(w http.ResponseWriter, r *http.Request) {
byte, err := ioutil.ReadAll(r.Body)
if err != nil {
apiErr := rest_errors.NewBadRequestError("invalid json bocy")
http_utils.RespondError(w, apiErr)
return
}

defer r.Body.Close()

var query queries.EsQuery
if err := json.Unmarshal(byte, &query); err != nil {
apiErr := rest_errors.NewBadRequestError("invalid json bocy")
http_utils.RespondError(w, apiErr)
return
}

items, searchErr := services.ItemsService.Search(query)
if searchErr != nil {
http_utils.RespondError(w, searchErr)
return
}

http_utils.RespondJson(w, http.StatusOK, items)
}
26 changes: 26 additions & 0 deletions src/domain/items/item_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"../../../../bookstore_utils_go/rest_errors"
"../../clients/elasticsearch"
"../queries"
)

const (
Expand Down Expand Up @@ -45,3 +46,28 @@ func (i *Item) Get() rest_errors.RestErr {
i.Id = itemId
return nil
}

func (i *Item) Search(query queries.EsQuery) ([]Item, rest_errors.RestErr) {
result, err := elasticsearch.Client.Search(indexItem, query.Build())
if err != nil {
return nil, rest_errors.NewInternalServerError("error when trying to search documents", errors.New("database error"))
}
//fmt.Println(result)

items := make([]Item, result.TotalHits())
for index, hit := range result.Hits.Hits {
bytes, _ := hit.Source.MarshalJSON()
var item Item
if err := json.Unmarshal(bytes, &item); err != nil {
return nil, rest_errors.NewInternalServerError("error when trying to parse response", errors.New("database error"))
}
item.Id = hit.Id
items[index] = item
}

if len(items) == 0 {
return nil, rest_errors.NewBadNotFoundError("no items found matching given criteria")
}

return items, nil
}
16 changes: 16 additions & 0 deletions src/domain/queries/es_query_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package queries

import (
"github.com/olivere/elastic"
)

func (q EsQuery) Build() elastic.Query {

query := elastic.NewBoolQuery()
equalsQueries := make([]elastic.Query, 0)
for _, eq := range q.Equals {
equalsQueries = append(equalsQueries, elastic.NewMatchQuery(eq.Field, eq.Value))
}
query.Must(equalsQueries...)
return query
}
10 changes: 10 additions & 0 deletions src/domain/queries/es_query_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package queries

type EsQuery struct {
Equals []FieldValue `json:"equals"`
}

type FieldValue struct {
Field string `json:"field"`
Value interface{} `json:"value"`
}
8 changes: 8 additions & 0 deletions src/services/items_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"../../../bookstore_utils_go/rest_errors"
"../domain/items"
"../domain/queries"
)

var (
Expand All @@ -12,6 +13,7 @@ var (
type itemsServiceInterface interface {
Create(items.Item) (*items.Item, rest_errors.RestErr)
Get(string) (*items.Item, rest_errors.RestErr)
Search(queries.EsQuery) ([]items.Item, rest_errors.RestErr)
}

type itemsService struct{}
Expand All @@ -36,3 +38,9 @@ func (s *itemsService) Get(id string) (*items.Item, rest_errors.RestErr) {
return &item, nil
//return nil, rest_errors.NewRestErrpr("Not yet implement", http.StatusNotImplemented, "not_implemented", nil)
}

func (s *itemsService) Search(query queries.EsQuery) ([]items.Item, rest_errors.RestErr) {
dao := items.Item{}
return dao.Search(query)

}

0 comments on commit 98a1e36

Please sign in to comment.