// Source returns the a JSON-serializable interface.
333027
func (a *AdjacencyMatrixAggregation) Source() (interface{}, error) {
	// Example:
	//	{
	//  "aggs" : {
	//		"interactions" : {
	//			"adjacency_matrix" : {
	//				"filters" : {
	//					"grpA" : { "terms" : { "accounts" : ["hillary", "sidney"] }},
	//					"grpB" : { "terms" : { "accounts" : ["donald", "mitt"] }},
	//					"grpC" : { "terms" : { "accounts" : ["vladimir", "nigel"] }}
	//				}
	//			}
	//		}
	//	}
	// This method returns only the (outer) { "adjacency_matrix" : {} } part.

	source := make(map[string]interface{})
	adjacencyMatrix := make(map[string]interface{})
	source["adjacency_matrix"] = adjacencyMatrix

	dict := make(map[string]interface{})
	for key, filter := range a.filters {
		src, err := filter.Source()
		if err != nil {
			return nil, err
		}
		dict[key] = src
	}
	adjacencyMatrix["filters"] = dict

	// AggregationBuilder (SubAggregations)
	if len(a.subAggregations) > 0 {
		aggsMap := make(map[string]interface{})
		source["aggregations"] = aggsMap
		for name, aggregate := range a.subAggregations {
			src, err := aggregate.Source()
			if err != nil {
				return nil, err
			}
			aggsMap[name] = src
		}
	}

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

	return source, nil
}
43688
func (d DocvalueField) Source() (interface{}, error) {
	if d.Format == "" {
		return d.Field, nil
	}
	return map[string]interface{}{
		"field":  d.Field,
		"format": d.Format,
	}, 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
}
1049400
func (a *BucketSortAggregation) Source() (interface{}, error) {
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["bucket_sort"] = params

	if a.from != 0 {
		params["from"] = a.from
	}
	if a.size != -1 {
		params["size"] = a.size
	}

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

	// Parses sorters to JSON-serializable interface.
	if len(a.sorters) > 0 {
		sorters := make([]interface{}, len(a.sorters))
		params["sort"] = sorters
		for idx, sorter := range a.sorters {
			src, err := sorter.Source()
			if err != nil {
				return nil, err
			}
			sorters[idx] = src
		}
	}

	// Add metadata if available.
	if len(a.meta) > 0 {
		source["meta"] = a.meta
	}

	return source, nil
}
903494
func (q *NestedQuery) Source() (interface{}, error) {
	query := make(map[string]interface{})
	nq := make(map[string]interface{})
	query["nested"] = nq

	src, err := q.query.Source()
	if err != nil {
		return nil, err
	}
	nq["query"] = src

	nq["path"] = q.path

	if q.scoreMode != "" {
		nq["score_mode"] = q.scoreMode
	}
	if q.boost != nil {
		nq["boost"] = *q.boost
	}
	if q.queryName != "" {
		nq["_name"] = q.queryName
	}
	if q.ignoreUnmapped != nil {
		nq["ignore_unmapped"] = *q.ignoreUnmapped
	}
	if q.innerHit != nil {
		src, err := q.innerHit.Source()
		if err != nil {
			return nil, err
		}
		nq["inner_hits"] = src
	}
	return query, nil
}
856457
func (s *Service) Sources(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	srcs, err := s.Store.Sources(ctx).All(ctx)
	if err != nil {
		Error(w, http.StatusInternalServerError, "Error loading sources", s.Logger)
		return
	}

	res := getSourcesResponse{
		Sources: make([]sourceResponse, len(srcs)),
	}

	sources := make([]sourceResponse, 0)
	for _, src := range srcs {
		sources = append(sources, newSourceResponse(ctx, src))
	}

	res.Sources = sources

	encodeJSON(w, http.StatusOK, res, s.Logger)
}
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
}
220567
func (q *SuggesterCategoryIndex) Source() (interface{}, error) {
	source := make(map[string]interface{})

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

	return source, nil
}
442867
func (a *AliasAddAction) 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["add"] = act
	act["alias"] = a.alias
	switch len(a.index) {
	case 1:
		act["index"] = a.index[0]
	default:
		act["indices"] = a.index
	}
	if a.filter != nil {
		f, err := a.filter.Source()
		if err != nil {
			return nil, err
		}
		act["filter"] = f
	}
	if len(a.routing) > 0 {
		act["routing"] = a.routing
	}
	if len(a.indexRouting) > 0 {
		act["index_routing"] = a.indexRouting
	}
	if len(a.searchRouting) > 0 {
		act["search_routing"] = a.searchRouting
	}
	if a.isWriteIndex != nil {
		act["is_write_index"] = *a.isWriteIndex
	}
	return src, nil
}
554519
func (r *BulkIndexRequest) Source() ([]string, error) {
	// { "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
	// { "field1" : "value1" }

	if r.source != nil {
		return r.source, nil
	}

	lines := make([]string, 2)

	// "index" ...
	indexCommand := bulkIndexRequestCommandOp{
		Index:           r.index,
		Type:            r.typ,
		Id:              r.id,
		Routing:         r.routing,
		Parent:          r.parent,
		Version:         r.version,
		VersionType:     r.versionType,
		RetryOnConflict: r.retryOnConflict,
		Pipeline:        r.pipeline,
	}
	command := bulkIndexRequestCommand{
		r.opType: indexCommand,
	}

	var err error
	var body []byte
	if r.useEasyJSON {
		// easyjson
		body, err = command.MarshalJSON()
	} else {
		// encoding/json
		body, err = json.Marshal(command)
	}
	if err != nil {
		return nil, err
	}

	lines[0] = string(body)

	// "field1" ...
	if r.doc != nil {
		switch t := r.doc.(type) {
		default:
			body, err := json.Marshal(r.doc)
			if err != nil {
				return nil, err
			}
			lines[1] = string(body)
		case json.RawMessage:
			lines[1] = string(t)
		case *json.RawMessage:
			lines[1] = string(*t)
		case string:
			lines[1] = t
		case *string:
			lines[1] = *t
		}
	} else {
		lines[1] = "{}"
	}

	r.source = lines
	return lines, nil
}
1249527
func (q MatchNoneQuery) Source() (interface{}, error) {
	// {
	//   "match_none" : { ... }
	// }
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["match_none"] = params
	if q.queryName != "" {
		params["_name"] = q.queryName
	}
	return source, nil
}
1278264
func (r *BulkUpdateRequest) Source() ([]string, error) {
	// { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } }
	// { "doc" : { "field1" : "value1", ... } }
	// or
	// { "update" : { "_index" : "test", "_type" : "type1", "_id" : "1", ... } }
	// { "script" : { ... } }

	if r.source != nil {
		return r.source, nil
	}

	lines := make([]string, 2)

	// "update" ...
	updateCommand := bulkUpdateRequestCommandOp{
		Index:           r.index,
		Type:            r.typ,
		Id:              r.id,
		Routing:         r.routing,
		Parent:          r.parent,
		Version:         r.version,
		VersionType:     r.versionType,
		RetryOnConflict: r.retryOnConflict,
	}
	command := bulkUpdateRequestCommand{
		"update": updateCommand,
	}

	var err error
	var body []byte
	if r.useEasyJSON {
		// easyjson
		body, err = command.MarshalJSON()
	} else {
		// encoding/json
		body, err = json.Marshal(command)
	}
	if err != nil {
		return nil, err
	}

	lines[0] = string(body)

	// 2nd line: {"doc" : { ... }} or {"script": {...}}
	var doc interface{}
	if r.doc != nil {
		// Automatically serialize strings as raw JSON
		switch t := r.doc.(type) {
		default:
			doc = r.doc
		case string:
			if len(t) > 0 {
				doc = json.RawMessage(t)
			}
		case *string:
			if t != nil && len(*t) > 0 {
				doc = json.RawMessage(*t)
			}
		}
	}
	data := bulkUpdateRequestCommandData{
		DocAsUpsert:    r.docAsUpsert,
		DetectNoop:     r.detectNoop,
		Upsert:         r.upsert,
		ScriptedUpsert: r.scriptedUpsert,
		Doc:            doc,
		Source:         r.returnSource,
	}
	if r.script != nil {
		script, err := r.script.Source()
		if err != nil {
			return nil, err
		}
		data.Script = script
	}

	if r.useEasyJSON {
		// easyjson
		body, err = data.MarshalJSON()
	} else {
		// encoding/json
		body, err = json.Marshal(data)
	}
	if err != nil {
		return nil, err
	}

	lines[1] = string(body)

	r.source = lines
	return lines, nil
}
693366
func (o *FuzzyCompletionSuggesterOptions) Source() (interface{}, error) {
	out := make(map[string]interface{})

	if o.editDistance != nil {
		out["fuzziness"] = o.editDistance
	}
	if o.transpositions != nil {
		out["transpositions"] = *o.transpositions
	}
	if o.minLength != nil {
		out["min_length"] = *o.minLength
	}
	if o.prefixLength != nil {
		out["prefix_length"] = *o.prefixLength
	}
	if o.unicodeAware != nil {
		out["unicode_aware"] = *o.unicodeAware
	}
	if o.maxDeterminizedStates != nil {
		out["max_determinized_states"] = *o.maxDeterminizedStates
	}

	return out, 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
}
905293
func (fn LinearDecayFunction) Source() interface{} {
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source[fn.fieldName] = params
	if fn.origin != nil {
		params["origin"] = fn.origin
	}
	params["scale"] = fn.scale
	if fn.decay != nil && *fn.decay > 0 {
		params["decay"] = *fn.decay
	}
	if fn.offset != nil {
		params["offset"] = fn.offset
	}
	if fn.multiValueMode != "" {
		source["multi_value_mode"] = fn.multiValueMode
	}
	// Notice that the weight has to be serialized in FunctionScoreQuery.
	return source
}
1218354
func (a *AliasRemoveIndexAction) Source() (interface{}, error) {
	if err := a.Validate(); err != nil {
		return nil, err
	}
	src := make(map[string]interface{})
	act := make(map[string]interface{})
	src["remove_index"] = act
	act["index"] = a.index
	return src, nil
}
838292
func (s *MultiTermvectorItem) Source() interface{} {
	source := make(map[string]interface{})

	source["_id"] = s.id

	if s.index != "" {
		source["_index"] = s.index
	}
	if s.typ != "" {
		source["_type"] = s.typ
	}
	if s.fields != nil {
		source["fields"] = s.fields
	}
	if s.fieldStatistics != nil {
		source["field_statistics"] = fmt.Sprintf("%v", *s.fieldStatistics)
	}
	if s.offsets != nil {
		source["offsets"] = s.offsets
	}
	if s.parent != "" {
		source["parent"] = s.parent
	}
	if s.payloads != nil {
		source["payloads"] = fmt.Sprintf("%v", *s.payloads)
	}
	if s.positions != nil {
		source["positions"] = fmt.Sprintf("%v", *s.positions)
	}
	if s.preference != "" {
		source["preference"] = s.preference
	}
	if s.realtime != nil {
		source["realtime"] = fmt.Sprintf("%v", *s.realtime)
	}
	if s.routing != "" {
		source["routing"] = s.routing
	}
	if s.termStatistics != nil {
		source["term_statistics"] = fmt.Sprintf("%v", *s.termStatistics)
	}
	if s.doc != nil {
		source["doc"] = s.doc
	}
	if s.perFieldAnalyzer != nil && len(s.perFieldAnalyzer) > 0 {
		source["per_field_analyzer"] = s.perFieldAnalyzer
	}

	return source
}