Skip to content

Commit

Permalink
parseResultBodyで構造体使うように
Browse files Browse the repository at this point in the history
  • Loading branch information
ikura-hamu committed Aug 7, 2023
1 parent fcc98b9 commit 73c5fce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions service/search/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,13 @@ func (e *esEngine) Do(q *Query) (Result, error) {
if err != nil {
return nil, err
}
var res m
var res esSearchResponse
err = json.Unmarshal(searchResultBody, &res)
if err != nil {
return nil, err
}

e.l.Debug("search result", zap.Reflect("hits", res["hits"]))
e.l.Debug("search result", zap.Reflect("hits", res.Hits))
return e.parseResultBody(res)
}

Expand Down
30 changes: 16 additions & 14 deletions service/search/es_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ type esSearchResponse struct {
Total int64 `json:"total"`
} `json:"_shards"`
Hits struct {
Hits []struct {
ID string `json:"_id"`
Index string `json:"_index"`
Score interface{} `json:"_score"`
Source esMessageDoc `json:"_source"`
Type string `json:"_type"`
Sort []int64 `json:"sort"`
} `json:"hits"`
MaxScore interface{} `json:"max_score"`
Hits []esSearchHit `json:"hits"`
MaxScore interface{} `json:"max_score"`
Total struct {
Relation string `json:"relation"`
Value int64 `json:"value"`
Expand All @@ -42,17 +35,26 @@ type esSearchResponse struct {
Took int64 `json:"took"`
}

func (e *esEngine) parseResultBody(resBody m) (Result, error) {
totalHits := resBody["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64)
hits := resBody["hits"].(map[string]interface{})["hits"].([]any)
type esSearchHit struct {
ID string `json:"_id"`
Index string `json:"_index"`
Score interface{} `json:"_score"`
Source esMessageDoc `json:"_source"`
Type string `json:"_type"`
Sort []int64 `json:"sort"`
}

func (e *esEngine) parseResultBody(resBody esSearchResponse) (Result, error) {
totalHits := resBody.Hits.Total.Value
hits := resBody.Hits.Hits

r := &esResult{
totalHits: int64(totalHits),
messages: make([]message.Message, 0, len(hits)),
}

messageIDs := utils.Map(hits, func(hit any) uuid.UUID {
return uuid.Must(uuid.FromString(hit.(map[string]any)["_id"].(string)))
messageIDs := utils.Map(hits, func(hit esSearchHit) uuid.UUID {
return uuid.Must(uuid.FromString(hit.ID))
})

messages, err := e.mm.GetIn(messageIDs)
Expand Down

0 comments on commit 73c5fce

Please sign in to comment.