You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
3.8 KiB
160 lines
3.8 KiB
8 years ago
|
// Copyright 2012-present Oliver Eilhard. All rights reserved.
|
||
8 years ago
|
// Use of this source code is governed by a MIT-license.
|
||
|
// See http://olivere.mit-license.org/license.txt for details.
|
||
|
|
||
|
package elastic
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
|
||
8 years ago
|
"golang.org/x/net/context"
|
||
|
|
||
|
"gopkg.in/olivere/elastic.v5/uritemplates"
|
||
8 years ago
|
)
|
||
|
|
||
|
// SuggestService returns suggestions for text.
|
||
8 years ago
|
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters.html.
|
||
8 years ago
|
type SuggestService struct {
|
||
|
client *Client
|
||
|
pretty bool
|
||
|
routing string
|
||
|
preference string
|
||
8 years ago
|
index []string
|
||
8 years ago
|
suggesters []Suggester
|
||
|
}
|
||
|
|
||
8 years ago
|
// NewSuggestService creates a new instance of SuggestService.
|
||
8 years ago
|
func NewSuggestService(client *Client) *SuggestService {
|
||
|
builder := &SuggestService{
|
||
8 years ago
|
client: client,
|
||
8 years ago
|
}
|
||
|
return builder
|
||
|
}
|
||
|
|
||
8 years ago
|
// Index adds one or more indices to use for the suggestion request.
|
||
|
func (s *SuggestService) Index(index ...string) *SuggestService {
|
||
|
s.index = append(s.index, index...)
|
||
8 years ago
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Pretty asks Elasticsearch to return indented JSON.
|
||
8 years ago
|
func (s *SuggestService) Pretty(pretty bool) *SuggestService {
|
||
|
s.pretty = pretty
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Routing specifies the routing value.
|
||
8 years ago
|
func (s *SuggestService) Routing(routing string) *SuggestService {
|
||
|
s.routing = routing
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Preference specifies the node or shard the operation should be
|
||
|
// performed on (default: random).
|
||
8 years ago
|
func (s *SuggestService) Preference(preference string) *SuggestService {
|
||
|
s.preference = preference
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// Suggester adds a suggester to the request.
|
||
8 years ago
|
func (s *SuggestService) Suggester(suggester Suggester) *SuggestService {
|
||
|
s.suggesters = append(s.suggesters, suggester)
|
||
|
return s
|
||
|
}
|
||
|
|
||
8 years ago
|
// buildURL builds the URL for the operation.
|
||
|
func (s *SuggestService) buildURL() (string, url.Values, error) {
|
||
|
var err error
|
||
|
var path string
|
||
8 years ago
|
|
||
8 years ago
|
if len(s.index) > 0 {
|
||
|
path, err = uritemplates.Expand("/{index}/_suggest", map[string]string{
|
||
|
"index": strings.Join(s.index, ","),
|
||
8 years ago
|
})
|
||
8 years ago
|
} else {
|
||
|
path = "/_suggest"
|
||
|
}
|
||
|
if err != nil {
|
||
|
return "", url.Values{}, err
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Add query string parameters
|
||
|
params := url.Values{}
|
||
8 years ago
|
if s.pretty {
|
||
|
params.Set("pretty", fmt.Sprintf("%v", s.pretty))
|
||
|
}
|
||
|
if s.routing != "" {
|
||
|
params.Set("routing", s.routing)
|
||
|
}
|
||
|
if s.preference != "" {
|
||
|
params.Set("preference", s.preference)
|
||
|
}
|
||
8 years ago
|
return path, params, nil
|
||
|
}
|
||
|
|
||
|
// Do executes the request.
|
||
|
func (s *SuggestService) Do(ctx context.Context) (SuggestResult, error) {
|
||
|
path, params, err := s.buildURL()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
8 years ago
|
|
||
|
// Set body
|
||
|
body := make(map[string]interface{})
|
||
|
for _, s := range s.suggesters {
|
||
|
src, err := s.Source(false)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
body[s.Name()] = src
|
||
|
}
|
||
|
|
||
|
// Get response
|
||
8 years ago
|
res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// There is a _shard object that cannot be deserialized.
|
||
|
// So we use json.RawMessage instead.
|
||
|
var suggestions map[string]*json.RawMessage
|
||
|
if err := s.client.decoder.Decode(res.Body, &suggestions); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
ret := make(SuggestResult)
|
||
|
for name, result := range suggestions {
|
||
|
if name != "_shards" {
|
||
|
var sug []Suggestion
|
||
|
if err := s.client.decoder.Decode(*result, &sug); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
ret[name] = sug
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// SuggestResult is the outcome of SuggestService.Do.
|
||
8 years ago
|
type SuggestResult map[string][]Suggestion
|
||
|
|
||
8 years ago
|
// Suggestion is a single suggester outcome.
|
||
8 years ago
|
type Suggestion struct {
|
||
|
Text string `json:"text"`
|
||
|
Offset int `json:"offset"`
|
||
|
Length int `json:"length"`
|
||
|
Options []suggestionOption `json:"options"`
|
||
|
}
|
||
|
|
||
|
type suggestionOption struct {
|
||
|
Text string `json:"text"`
|
||
|
Score float64 `json:"score"`
|
||
|
Freq int `json:"freq"`
|
||
|
Payload interface{} `json:"payload"`
|
||
|
CollateMatch bool `json:"collate_match"`
|
||
|
}
|