diff --git a/CONTRIBUTORS b/CONTRIBUTORS index ef5f932e..0766746a 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -211,3 +211,4 @@ zakthomas [@zakthomas](https://github.com/zakthomas) Zach [@snowzach](https://github.com/snowzach) zhangxin [@visaxin](https://github.com/visaxin) @林 [@zplzpl](https://github.com/zplzpl) +@wth [@wth12138](https://github.com/wth12138) \ No newline at end of file diff --git a/search.go b/search.go index 4745c1b8..af3269c0 100644 --- a/search.go +++ b/search.go @@ -363,6 +363,21 @@ func (s *SearchService) StoredFields(fields ...string) *SearchService { return s } +// Field adds a single field to load and return as +// part of the post search request. If none are specified, the source of the +// document will be returned. +func (s *SearchService) Field(fieldName string) *SearchService { + s.searchSource = s.searchSource.Field(fieldName) + return s +} + +// Fields sets the fields to load and return as part of the search post request. +// If none are specified, the source of the document will be returned. +func (s *SearchService) Fields(fields ...string) *SearchService { + s.searchSource = s.searchSource.Fields(fields...) + return s +} + // TrackScores is applied when sorting and controls if scores will be // tracked as well. Defaults to false. func (s *SearchService) TrackScores(trackScores bool) *SearchService { diff --git a/search_source.go b/search_source.go index 1113414e..35b1f034 100644 --- a/search_source.go +++ b/search_source.go @@ -28,6 +28,7 @@ type SearchSource struct { timeout string // timeout terminateAfter *int // terminate_after storedFieldNames []string // stored_fields + fields []string // fields docvalueFields DocvalueFields // docvalue_fields scriptFields []*ScriptField // script_fields fetchSourceContext *FetchSourceContext // _source @@ -297,6 +298,21 @@ func (s *SearchSource) StoredFields(storedFieldNames ...string) *SearchSource { return s } +// Field adds a single field to load and return as +// part of the post search request. If none are specified, the source of the +// document will be returned (need _source set false) . +func (s *SearchSource) Field(FieldName string) *SearchSource { + s.storedFieldNames = append(s.fields, FieldName) + return s +} + +// Fields sets the fields to load and return as part of the search post request. +// If none are specified, the source of the document will be returned (need _source set false). +func (s *SearchSource) Fields(FieldNames ...string) *SearchSource { + s.fields = append(s.fields, FieldNames...) + return s +} + // DocvalueField adds a single field to load from the field data cache // and return as part of the search request. func (s *SearchSource) DocvalueField(fieldDataField string) *SearchSource { @@ -440,6 +456,14 @@ func (s *SearchSource) Source() (interface{}, error) { source["stored_fields"] = s.storedFieldNames } } + if s.fields != nil { + switch len(s.fields) { + case 1: + source["fields"] = s.fields[0] + default: + source["fields"] = s.fields + } + } if len(s.docvalueFields) > 0 { src, err := s.docvalueFields.Source() if err != nil { diff --git a/search_source_test.go b/search_source_test.go index 36ed886c..eb7e79bd 100644 --- a/search_source_test.go +++ b/search_source_test.go @@ -76,6 +76,24 @@ func TestSearchSourceStoredFields(t *testing.T) { } } +func TestSearchSourceFields(t *testing.T) { + matchAllQ := NewMatchAllQuery() + builder := NewSearchSource().Query(matchAllQ).Fields("message", "tags") + src, err := builder.Source() + if err != nil { + t.Fatal(err) + } + data, err := json.Marshal(src) + if err != nil { + t.Fatalf("marshaling to JSON failed: %v", err) + } + got := string(data) + expected := `{"fields":["message","tags"],"query":{"match_all":{}}}` + if got != expected { + t.Errorf("expected\n%s\n,got:\n%s", expected, got) + } +} + func TestSearchSourceFetchSourceDisabled(t *testing.T) { matchAllQ := NewMatchAllQuery() builder := NewSearchSource().Query(matchAllQ).FetchSource(false)