// Source returns the serialized JSON to be sent to Elasticsearch as
// part of a MultiGet search.
326399
func (item *MultiGetItem) Source() interface{} {
	source := make(map[string]interface{})

	source["_id"] = item.id

	if item.index != "" {
		source["_index"] = item.index
	}
	if item.typ != "" {
		source["_type"] = item.typ
	}
	if item.fsc != nil {
		source["_source"] = item.fsc.Source()
	}
	if item.fields != nil {
		source["fields"] = item.fields
	}
	if item.routing != "" {
		source["_routing"] = item.routing
	}
	if item.version != nil {
		source["version"] = fmt.Sprintf("%d", *item.version)
	}
	if item.versionType != "" {
		source["version_type"] = item.versionType
	}

	return source
}
66508
func (q *SuggesterCategoryQuery) Source() interface{} {
	source := make(map[string]interface{})

	switch len(q.values) {
	case 0:
		source[q.name] = q.values
	case 1:
		source[q.name] = q.values[0]
	default:
		source[q.name] = q.values
	}

	return source
}
367773
func (t *TermsLookup) Source() (interface{}, error) {
	src := make(map[string]interface{})
	if t.index != "" {
		src["index"] = t.index
	}
	if t.typ != "" {
		src["type"] = t.typ
	}
	if t.id != "" {
		src["id"] = t.id
	}
	if t.path != "" {
		src["path"] = t.path
	}
	if t.routing != "" {
		src["routing"] = t.routing
	}
	return src, nil
}
1055145
func (fn *ScriptFunction) Source() (interface{}, error) {
	source := make(map[string]interface{})
	if fn.script != nil {
		src, err := fn.script.Source()
		if err != nil {
			return nil, err
		}
		source["script"] = src
	}
	// Notice that the weight has to be serialized in FunctionScoreQuery.
	return source, nil
}
515983
func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["percentiles_bucket"] = params

	if p.format != "" {
		params["format"] = p.format
	}
	if p.gapPolicy != "" {
		params["gap_policy"] = p.gapPolicy
	}

	// Add buckets paths
	switch len(p.bucketsPaths) {
	case 0:
	case 1:
		params["buckets_path"] = p.bucketsPaths[0]
	default:
		params["buckets_path"] = p.bucketsPaths
	}

	// Add percents
	if len(p.percents) > 0 {
		params["percents"] = p.percents
	}

	// Add Meta data if available
	if len(p.meta) > 0 {
		source["meta"] = p.meta
	}

	return source, nil
}
1257671
func (s *SearchSource) Source() (interface{}, error) {
	source := make(map[string]interface{})

	if s.from != -1 {
		source["from"] = s.from
	}
	if s.size != -1 {
		source["size"] = s.size
	}
	if s.timeout != "" {
		source["timeout"] = s.timeout
	}
	if s.terminateAfter != nil {
		source["terminate_after"] = *s.terminateAfter
	}
	if s.query != nil {
		src, err := s.query.Source()
		if err != nil {
			return nil, err
		}
		source["query"] = src
	}
	if s.postQuery != nil {
		src, err := s.postQuery.Source()
		if err != nil {
			return nil, err
		}
		source["post_filter"] = src
	}
	if s.minScore != nil {
		source["min_score"] = *s.minScore
	}
	if s.version != nil {
		source["version"] = *s.version
	}
	if s.explain != nil {
		source["explain"] = *s.explain
	}
	if s.profile {
		source["profile"] = s.profile
	}
	if s.fetchSourceContext != nil {
		src, err := s.fetchSourceContext.Source()
		if err != nil {
			return nil, err
		}
		source["_source"] = src
	}
	if s.storedFieldNames != nil {
		switch len(s.storedFieldNames) {
		case 1:
			source["stored_fields"] = s.storedFieldNames[0]
		default:
			source["stored_fields"] = s.storedFieldNames
		}
	}
	if len(s.docvalueFields) > 0 {
		src, err := s.docvalueFields.Source()
		if err != nil {
			return nil, err
		}
		source["docvalue_fields"] = src
	}
	if len(s.scriptFields) > 0 {
		sfmap := make(map[string]interface{})
		for _, scriptField := range s.scriptFields {
			src, err := scriptField.Source()
			if err != nil {
				return nil, err
			}
			sfmap[scriptField.FieldName] = src
		}
		source["script_fields"] = sfmap
	}
	if len(s.sorters) > 0 {
		var sortarr []interface{}
		for _, sorter := range s.sorters {
			src, err := sorter.Source()
			if err != nil {
				return nil, err
			}
			sortarr = append(sortarr, src)
		}
		source["sort"] = sortarr
	}
	if v := s.trackScores; v != nil {
		source["track_scores"] = *v
	}
	if v := s.trackTotalHits; v != nil {
		source["track_total_hits"] = v
	}
	if len(s.searchAfterSortValues) > 0 {
		source["search_after"] = s.searchAfterSortValues
	}
	if s.sliceQuery != nil {
		src, err := s.sliceQuery.Source()
		if err != nil {
			return nil, err
		}
		source["slice"] = src
	}
	if len(s.indexBoosts) > 0 {
		source["indices_boost"] = s.indexBoosts
	}
	if len(s.aggregations) > 0 {
		aggsMap := make(map[string]interface{})
		for name, aggregate := range s.aggregations {
			src, err := aggregate.Source()
			if err != nil {
				return nil, err
			}
			aggsMap[name] = src
		}
		source["aggregations"] = aggsMap
	}
	if s.highlight != nil {
		src, err := s.highlight.Source()
		if err != nil {
			return nil, err
		}
		source["highlight"] = src
	}
	if len(s.suggesters) > 0 {
		suggesters := make(map[string]interface{})
		for _, s := range s.suggesters {
			src, err := s.Source(false)
			if err != nil {
				return nil, err
			}
			suggesters[s.Name()] = src
		}
		if s.globalSuggestText != "" {
			suggesters["text"] = s.globalSuggestText
		}
		source["suggest"] = suggesters
	}
	if len(s.rescores) > 0 {
		// Strip empty rescores from request
		var rescores []*Rescore
		for _, r := range s.rescores {
			if !r.IsEmpty() {
				rescores = append(rescores, r)
			}
		}
		if len(rescores) == 1 {
			rescores[0].defaultRescoreWindowSize = s.defaultRescoreWindowSize
			src, err := rescores[0].Source()
			if err != nil {
				return nil, err
			}
			source["rescore"] = src
		} else {
			var slice []interface{}
			for _, r := range rescores {
				r.defaultRescoreWindowSize = s.defaultRescoreWindowSize
				src, err := r.Source()
				if err != nil {
					return nil, err
				}
				slice = append(slice, src)
			}
			source["rescore"] = slice
		}
	}
	if len(s.stats) > 0 {
		source["stats"] = s.stats
	}
	// TODO ext builders

	if s.collapse != nil {
		src, err := s.collapse.Source()
		if err != nil {
			return nil, err
		}
		source["collapse"] = src
	}

	if len(s.innerHits) > 0 {
		// Top-level inner hits
		// See http://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-request-inner-hits.html#top-level-inner-hits
		// "inner_hits": {
		//   "<inner_hits_name>": {
		//     "<path|type>": {
		//       "<path-to-nested-object-field|child-or-parent-type>": {
		//         <inner_hits_body>,
		//         [,"inner_hits" : { [<sub_inner_hits>]+ } ]?
		//       }
		//     }
		//   },
		//   [,"<inner_hits_name_2>" : { ... } ]*
		// }
		m := make(map[string]interface{})
		for name, hit := range s.innerHits {
			if hit.path != "" {
				src, err := hit.Source()
				if err != nil {
					return nil, err
				}
				path := make(map[string]interface{})
				path[hit.path] = src
				m[name] = map[string]interface{}{
					"path": path,
				}
			} else if hit.typ != "" {
				src, err := hit.Source()
				if err != nil {
					return nil, err
				}
				typ := make(map[string]interface{})
				typ[hit.typ] = src
				m[name] = map[string]interface{}{
					"type": typ,
				}
			} else {
				// TODO the Java client throws here, because either path or typ must be specified
				_ = m
			}
		}
		source["inner_hits"] = m
	}

	return source, nil
}
279236
func (q RawStringQuery) Source() (interface{}, error) {
	var f interface{}
	err := json.Unmarshal([]byte(q), &f)
	return f, err
}
1214147
func (s *MgetService) Source() (interface{}, error) {
	source := make(map[string]interface{})
	items := make([]interface{}, len(s.items))
	for i, item := range s.items {
		src, err := item.Source()
		if err != nil {
			return nil, err
		}
		items[i] = src
	}
	source["docs"] = items
	return source, nil
}
489489
func (s *SliceQuery) Source() (interface{}, error) {
	m := make(map[string]interface{})
	if s.field != "" {
		m["field"] = s.field
	}
	if s.id != nil {
		m["id"] = *s.id
	}
	if s.max != nil {
		m["max"] = *s.max
	}
	return m, nil
}
1246341
func (q *SuggesterCategoryQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})

	switch len(q.values) {
	case 0:
		source[q.name] = make([]string, 0)
	default:
		contexts := make([]interface{}, 0)
		for val, boost := range q.values {
			context := make(map[string]interface{})
			context["context"] = val
			if boost != nil {
				context["boost"] = *boost
			}
			contexts = append(contexts, context)
		}
		source[q.name] = contexts
	}

	return source, nil
}
107266
func (a *AliasRemoveAction) Source() (interface{}, error) {
	a.removeBlankIndexNames()
	if err := a.Validate(); err != nil {
		return nil, err
	}
	src := make(map[string]interface{})
	act := make(map[string]interface{})
	src["remove"] = act
	act["alias"] = a.alias
	switch len(a.index) {
	case 1:
		act["index"] = a.index[0]
	default:
		act["indices"] = a.index
	}
	return src, nil
}
619144
func (q MoreLikeThisQuery) Source() interface{} {
	// {
	//   "match_all" : { ... }
	// }
	params := make(map[string]interface{})
	source := make(map[string]interface{})
	source["mlt"] = params

	if q.likeText == "" && len(q.docs) == 0 && len(q.ids) == 0 {
		// We have no form of returning errors for invalid queries as of Elastic.
		// We also don't have access to the client here, so we can't log anything.
		// All we can do is to return an empty query, I suppose.
		// TODO Is there a better approach here?
		//return nil, errors.New(`more_like_this requires some documents to be "liked"`)
		return source
	}

	if len(q.fields) > 0 {
		params["fields"] = q.fields
	}
	if q.likeText != "" {
		params["like_text"] = q.likeText
	}
	if q.minimumShouldMatch != "" {
		params["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.minTermFreq != nil {
		params["min_term_freq"] = *q.minTermFreq
	}
	if q.maxQueryTerms != nil {
		params["max_query_terms"] = *q.maxQueryTerms
	}
	if len(q.stopWords) > 0 {
		params["stop_words"] = q.stopWords
	}
	if q.minDocFreq != nil {
		params["min_doc_freq"] = *q.minDocFreq
	}
	if q.maxDocFreq != nil {
		params["max_doc_freq"] = *q.maxDocFreq
	}
	if q.minWordLen != nil {
		params["min_word_len"] = *q.minWordLen
	}
	if q.maxWordLen != nil {
		params["max_word_len"] = *q.maxWordLen
	}
	if q.boostTerms != nil {
		params["boost_terms"] = *q.boostTerms
	}
	if q.boost != nil {
		params["boost"] = *q.boost
	}
	if q.analyzer != "" {
		params["analyzer"] = q.analyzer
	}
	if q.failOnUnsupportedField != nil {
		params["fail_on_unsupported_field"] = *q.failOnUnsupportedField
	}
	if q.queryName != "" {
		params["_name"] = q.queryName
	}
	if len(q.ids) > 0 {
		params["ids"] = q.ids
	}
	if len(q.docs) > 0 {
		docs := make([]interface{}, 0)
		for _, doc := range q.docs {
			docs = append(docs, doc.Source())
		}
		params["docs"] = docs
	}
	if q.include != nil {
		params["exclude"] = !(*q.include) // ES 1.x only has exclude
	}

	return source
}
1305959
func (s *StatsBucketAggregation) Source() (interface{}, error) {
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["stats_bucket"] = params

	if s.format != "" {
		params["format"] = s.format
	}
	if s.gapPolicy != "" {
		params["gap_policy"] = s.gapPolicy
	}

	// Add buckets paths
	switch len(s.bucketsPaths) {
	case 0:
	case 1:
		params["buckets_path"] = s.bucketsPaths[0]
	default:
		params["buckets_path"] = s.bucketsPaths
	}

	// Add Meta data if available
	if len(s.meta) > 0 {
		source["meta"] = s.meta
	}

	return source, nil
}
458445
func (item *MultiGetItem) Source() (interface{}, error) {
	source := make(map[string]interface{})

	source["_id"] = item.id

	if item.index != "" {
		source["_index"] = item.index
	}
	if item.typ != "" {
		source["_type"] = item.typ
	}
	if item.fsc != nil {
		src, err := item.fsc.Source()
		if err != nil {
			return nil, err
		}
		source["_source"] = src
	}
	if item.routing != "" {
		source["_routing"] = item.routing
	}
	if len(item.storedFields) > 0 {
		source["stored_fields"] = strings.Join(item.storedFields, ",")
	}
	if item.version != nil {
		source["version"] = fmt.Sprintf("%d", *item.version)
	}
	if item.versionType != "" {
		source["version_type"] = item.versionType
	}

	return source, nil
}
21620
func source(lines [][]byte, n int) []byte {
	n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
	if n < 0 || n >= len(lines) {
		return dunno
	}
	return bytes.TrimSpace(lines[n])
}
21678
func (fn *RandomFunction) Source() (interface{}, error) {
	source := make(map[string]interface{})
	if fn.field != "" {
		source["field"] = fn.field
	}
	if fn.seed != nil {
		source["seed"] = fn.seed
	}
	// Notice that the weight has to be serialized in FunctionScoreQuery.
	return source, nil
}
331174
func (q *MatchPhraseQuery) Source() (interface{}, error) {
	// {"match_phrase":{"name":{"query":"value","analyzer":"my_analyzer"}}}
	source := make(map[string]interface{})

	match := make(map[string]interface{})
	source["match_phrase"] = match

	query := make(map[string]interface{})
	match[q.name] = query

	query["query"] = q.value

	if q.analyzer != "" {
		query["analyzer"] = q.analyzer
	}
	if q.slop != nil {
		query["slop"] = *q.slop
	}
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}

	return source, nil
}