| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"net/url" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 15 |
) |
| 16 |
|
| 17 |
// SuggestService returns suggestions for text. |
| 18 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters.html. |
| 19 |
type SuggestService struct { |
| 20 |
client *Client |
| 21 |
pretty bool |
| 22 |
routing string |
| 23 |
preference string |
| 24 |
index []string |
| 25 |
suggesters []Suggester |
| 26 |
} |
| 27 |
|
| 28 |
// NewSuggestService creates a new instance of SuggestService. |
| 29 |
func NewSuggestService(client *Client) *SuggestService { |
| 30 |
builder := &SuggestService{ |
| 31 |
client: client, |
| 32 |
} |
| 33 |
return builder |
| 34 |
} |
| 35 |
|
| 36 |
// Index adds one or more indices to use for the suggestion request. |
| 37 |
func (s *SuggestService) Index(index ...string) *SuggestService { |
| 38 |
s.index = append(s.index, index...) |
| 39 |
return s |
| 40 |
} |
| 41 |
|
| 42 |
// Pretty asks Elasticsearch to return indented JSON. |
| 43 |
func (s *SuggestService) Pretty(pretty bool) *SuggestService { |
| 44 |
s.pretty = pretty |
| 45 |
return s |
| 46 |
} |
| 47 |
|
| 48 |
// Routing specifies the routing value. |
| 49 |
func (s *SuggestService) Routing(routing string) *SuggestService { |
| 50 |
s.routing = routing |
| 51 |
return s |
| 52 |
} |
| 53 |
|
| 54 |
// Preference specifies the node or shard the operation should be |
| 55 |
// performed on (default: random). |
| 56 |
func (s *SuggestService) Preference(preference string) *SuggestService { |
| 57 |
s.preference = preference |
| 58 |
return s |
| 59 |
} |
| 60 |
|
| 61 |
// Suggester adds a suggester to the request. |
| 62 |
func (s *SuggestService) Suggester(suggester Suggester) *SuggestService { |
| 63 |
s.suggesters = append(s.suggesters, suggester) |
| 64 |
return s |
| 65 |
} |
| 66 |
|
| 67 |
// buildURL builds the URL for the operation. |
| 68 |
func (s *SuggestService) buildURL() (string, url.Values, error) { |
| 69 |
var err error |
| 70 |
var path string |
| 71 |
|
| 72 |
if len(s.index) > 0 { |
| 73 |
path, err = uritemplates.Expand("/{index}/_suggest", map[string]string{ |
| 74 |
"index": strings.Join(s.index, ","), |
| 75 |
}) |
| 76 |
} else { |
| 77 |
path = "/_suggest" |
| 78 |
} |
| 79 |
if err != nil { |
| 80 |
return "", url.Values{}, err |
| 81 |
} |
| 82 |
|
| 83 |
// Add query string parameters |
| 84 |
params := url.Values{} |
| 85 |
if s.pretty { |
| 86 |
params.Set("pretty", fmt.Sprintf("%v", s.pretty)) |
| 87 |
} |
| 88 |
if s.routing != "" { |
| 89 |
params.Set("routing", s.routing) |
| 90 |
} |
| 91 |
if s.preference != "" { |
| 92 |
params.Set("preference", s.preference) |
| 93 |
} |
| 94 |
return path, params, nil |
| 95 |
} |
| 96 |
|
| 97 |
// Do executes the request. |
| 98 |
func (s *SuggestService) Do(ctx context.Context) (SuggestResult, error) { |
| 99 |
path, params, err := s.buildURL() |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
// Set body |
| 105 |
body := make(map[string]interface{}) |
| 106 |
for _, s := range s.suggesters { |
| 107 |
src, err := s.Source(false) |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
body[s.Name()] = src |
| 112 |
} |
| 113 |
|
| 114 |
// Get response |
| 115 |
res, err := s.client.PerformRequest(ctx, "POST", path, params, body) |
| 116 |
if err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
|
| 120 |
// There is a _shard object that cannot be deserialized. |
| 121 |
// So we use json.RawMessage instead. |
| 122 |
var suggestions map[string]*json.RawMessage |
| 123 |
if err := s.client.decoder.Decode(res.Body, &suggestions); err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
ret := make(SuggestResult) |
| 128 |
for name, result := range suggestions { |
| 129 |
if name != "_shards" { |
| 130 |
var sug []Suggestion |
| 131 |
if err := s.client.decoder.Decode(*result, &sug); err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
ret[name] = sug |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return ret, nil |
| 139 |
} |
| 140 |
|
| 141 |
// SuggestResult is the outcome of SuggestService.Do. |
| 142 |
type SuggestResult map[string][]Suggestion |
| 143 |
|
| 144 |
// Suggestion is a single suggester outcome. |
| 145 |
type Suggestion struct { |
| 146 |
Text string `json:"text"` |
| 147 |
Offset int `json:"offset"` |
| 148 |
Length int `json:"length"` |
| 149 |
Options []suggestionOption `json:"options"` |
| 150 |
} |
| 151 |
|
| 152 |
type suggestionOption struct { |
| 153 |
Text string `json:"text"` |
| 154 |
Score float64 `json:"score"` |
| 155 |
Freq int `json:"freq"` |
| 156 |
Payload interface{} `json:"payload"` |
| 157 |
CollateMatch bool `json:"collate_match"` |
| 158 |
} |