1// Copyright 2012-2015 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 5package elastic 6 7import ( 8 "encoding/json" 9) 10 11// SuggestField can be used by the caller to specify a suggest field 12// at index time. For a detailed example, see e.g. 13// http://www.elasticsearch.org/blog/you-complete-me/. 14type SuggestField struct { 15 inputs []string 16 output *string 17 payload interface{} 18 weight int 19} 20 21func NewSuggestField() *SuggestField { 22 return &SuggestField{weight: -1} 23} 24 25func (f *SuggestField) Input(input ...string) *SuggestField { 26 if f.inputs == nil { 27 f.inputs = make([]string, 0) 28 } 29 f.inputs = append(f.inputs, input...) 30 return f 31} 32 33func (f *SuggestField) Output(output string) *SuggestField { 34 f.output = &output 35 return f 36} 37 38func (f *SuggestField) Payload(payload interface{}) *SuggestField { 39 f.payload = payload 40 return f 41} 42 43func (f *SuggestField) Weight(weight int) *SuggestField { 44 f.weight = weight 45 return f 46} 47 48// MarshalJSON encodes SuggestField into JSON. 49func (f *SuggestField) MarshalJSON() ([]byte, error) { 50 source := make(map[string]interface{}) 51 52 if f.inputs != nil { 53 switch len(f.inputs) { 54 case 1: 55 source["input"] = f.inputs[0] 56 default: 57 source["input"] = f.inputs 58 } 59 } 60 61 if f.output != nil { 62 source["output"] = *f.output 63 } 64 65 if f.payload != nil { 66 source["payload"] = f.payload 67 } 68 69 if f.weight >= 0 { 70 source["weight"] = f.weight 71 } 72 73 return json.Marshal(source) 74} 75