Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add field #1625

Open
wants to merge 1 commit into
base: release-branch.v7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions search_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions search_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down