// Source returns JSON for the function score query.
507545
func (q FunctionScoreQuery) Source() interface{} {
	source := make(map[string]interface{})
	query := make(map[string]interface{})
	source["function_score"] = query

	if q.query != nil {
		query["query"] = q.query.Source()
	} else if q.filter != nil {
		query["filter"] = q.filter.Source()
	}

	if len(q.filters) == 1 && q.filters[0] == nil {
		// Weight needs to be serialized on this level.
		if weight := q.scoreFuncs[0].GetWeight(); weight != nil {
			query["weight"] = weight
		}
		// Serialize the score function
		query[q.scoreFuncs[0].Name()] = q.scoreFuncs[0].Source()
	} else {
		funcs := make([]interface{}, len(q.filters))
		for i, filter := range q.filters {
			hsh := make(map[string]interface{})
			if filter != nil {
				hsh["filter"] = filter.Source()
			}
			// Weight needs to be serialized on this level.
			if weight := q.scoreFuncs[i].GetWeight(); weight != nil {
				hsh["weight"] = weight
			}
			// Serialize the score function
			hsh[q.scoreFuncs[i].Name()] = q.scoreFuncs[i].Source()
			funcs[i] = hsh
		}
		query["functions"] = funcs
	}

	if q.scoreMode != "" {
		query["score_mode"] = q.scoreMode
	}
	if q.boostMode != "" {
		query["boost_mode"] = q.boostMode
	}
	if q.maxBoost != nil {
		query["max_boost"] = *q.maxBoost
	}
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.minScore != nil {
		query["min_score"] = *q.minScore
	}

	return source
}
92842
func (o *RegexCompletionSuggesterOptions) Source() (interface{}, error) {
	out := make(map[string]interface{})

	if o.flags != nil {
		out["flags"] = o.flags
	}
	if o.maxDeterminizedStates != nil {
		out["max_determinized_states"] = *o.maxDeterminizedStates
	}

	return out, nil
}
771695
func (r *BulkDeleteRequest) Source() ([]string, error) {
	if r.source != nil {
		return r.source, nil
	}
	command := bulkDeleteRequestCommand{
		"delete": bulkDeleteRequestCommandOp{
			Index:       r.index,
			Type:        r.typ,
			Id:          r.id,
			Routing:     r.routing,
			Parent:      r.parent,
			Version:     r.version,
			VersionType: r.versionType,
		},
	}

	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 := []string{string(body)}
	r.source = lines

	return lines, nil
}
86154
func (q MatchAllQuery) Source() interface{} {
	// {
	//   "match_all" : { ... }
	// }
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["match_all"] = params
	if q.boost != nil {
		params["boost"] = q.boost
	}
	if q.normsField != "" {
		params["norms_field"] = q.normsField
	}
	return source
}
436728
func (q FuzzyLikeThisFieldQuery) Source() interface{} {
	// {
	//	"fuzzy_like_this_field" : {
	//    "name.first": {
	//      "like_text" : "text like this one",
	//      "max_query_terms" : 12
	//    }
	// }

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

	query := make(map[string]interface{})
	source["fuzzy_like_this_field"] = query
	fq := make(map[string]interface{})
	query[q.field] = fq

	fq["like_text"] = q.likeText

	if q.maxQueryTerms != nil {
		fq["max_query_terms"] = *q.maxQueryTerms
	}
	if q.fuzziness != nil {
		fq["fuzziness"] = q.fuzziness
	}
	if q.prefixLength != nil {
		fq["prefix_length"] = *q.prefixLength
	}
	if q.ignoreTF != nil {
		fq["ignore_tf"] = *q.ignoreTF
	}
	if q.boost != nil {
		fq["boost"] = *q.boost
	}
	if q.analyzer != "" {
		fq["analyzer"] = q.analyzer
	}
	if q.failOnUnsupportedField != nil {
		fq["fail_on_unsupported_field"] = *q.failOnUnsupportedField
	}
	if q.queryName != "" {
		fq["_name"] = q.queryName
	}

	return source
}
60938
func (q *RangeQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})

	rangeQ := make(map[string]interface{})
	source["range"] = rangeQ

	params := make(map[string]interface{})
	rangeQ[q.name] = params

	params["from"] = q.from
	params["to"] = q.to
	if q.timeZone != "" {
		params["time_zone"] = q.timeZone
	}
	if q.format != "" {
		params["format"] = q.format
	}
	if q.relation != "" {
		params["relation"] = q.relation
	}
	if q.boost != nil {
		params["boost"] = *q.boost
	}
	params["include_lower"] = q.includeLower
	params["include_upper"] = q.includeUpper

	if q.queryName != "" {
		rangeQ["_name"] = q.queryName
	}

	return source, nil
}
970274
func (q *TypeQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["type"] = params
	params["value"] = q.typ
	return source, nil
}
782617
func (f *ScriptField) Source() (interface{}, error) {
	if f.script == nil {
		return nil, errors.New("ScriptField expects script")
	}
	source := make(map[string]interface{})
	src, err := f.script.Source()
	if err != nil {
		return nil, err
	}
	source["script"] = src
	return source, nil
}
478360
func (q CustomFiltersScoreQuery) Source() interface{} {
	// {
	//   "custom_filters_score" : {
	//    "query" : {
	//      "match_all" : {}
	//    },
	//    "filters" : [
	//      {
	//        "filter" : { "range" : { "age" : {"from" : 0, "to" : 10} } },
	//        "boost" : "3"
	//      },
	//      {
	//        "filter" : { "range" : { "age" : {"from" : 10, "to" : 20} } },
	//        "boost" : "2"
	//      }
	//     ],
	//     "score_mode" : "first"
	//   }
	// }

	query := make(map[string]interface{})

	cfs := make(map[string]interface{})
	query["custom_filters_score"] = cfs

	// query
	if q.query != nil {
		cfs["query"] = q.query.Source()
	}
	// filters
	clauses := make([]interface{}, 0)
	for _, filter := range q.filters {
		clauses = append(clauses, filter.Source())
	}
	cfs["filters"] = clauses

	// scoreMode
	if q.scoreMode != "" {
		cfs["score_mode"] = q.scoreMode
	}

	// max_boost
	if q.maxBoost != nil {
		cfs["max_boost"] = *q.maxBoost
	}

	// script
	if q.script != "" {
		cfs["script"] = q.script
	}

	return query
}
457999
func (fn RandomFunction) Source() interface{} {
	source := make(map[string]interface{})
	if fn.seed != nil {
		source["seed"] = fn.seed
	}
	// Notice that the weight has to be serialized in FunctionScoreQuery.
	return source
}
610719
func (q TermsQuery) Source() interface{} {
	// {"terms":{"name":["value1","value2"]}}
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["terms"] = params
	params[q.name] = q.values
	if q.minimumShouldMatch != "" {
		params["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.disableCoord != nil {
		params["disable_coord"] = *q.disableCoord
	}
	if q.boost != nil {
		params["boost"] = *q.boost
	}
	if q.queryName != "" {
		params["_name"] = q.queryName
	}
	return source
}
1218690
func (q SimpleQueryStringQuery) Source() interface{} {
	// {
	//    "simple_query_string" : {
	//      "query" : "\"fried eggs\" +(eggplant | potato) -frittata",
	//			"analyzer" : "snowball",
	//      "fields" : ["body^5","_all"],
	//      "default_operator" : "and"
	//    }
	// }

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

	query := make(map[string]interface{})
	source["simple_query_string"] = query

	query["query"] = q.queryText

	if len(q.fields) > 0 {
		fields := make([]string, 0)
		for _, field := range q.fields {
			if boost, found := q.fieldBoosts[field]; found {
				if boost != nil {
					fields = append(fields, fmt.Sprintf("%s^%f", field, *boost))
				} else {
					fields = append(fields, field)
				}
			} else {
				fields = append(fields, field)
			}
		}
		query["fields"] = fields
	}

	if q.analyzer != "" {
		query["analyzer"] = q.analyzer
	}

	if q.operator != "" {
		query["default_operator"] = strings.ToLower(q.operator)
	}

	return source
}
349854
func (q *IdsQuery) Source() (interface{}, error) {
	// {
	//	"ids" : {
	//		"type" : "my_type",
	//		"values" : ["1", "4", "100"]
	//	}
	// }

	source := make(map[string]interface{})
	query := make(map[string]interface{})
	source["ids"] = query

	// type(s)
	if len(q.types) == 1 {
		query["type"] = q.types[0]
	} else if len(q.types) > 1 {
		query["types"] = q.types
	}

	// values
	query["values"] = q.values

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

	return source, nil
}
1359343
func (fn FieldValueFactorFunction) Source() interface{} {
	source := make(map[string]interface{})
	if fn.field != "" {
		source["field"] = fn.field
	}
	if fn.factor != nil {
		source["factor"] = *fn.factor
	}
	if fn.missing != nil {
		source["missing"] = *fn.missing
	}
	if fn.modifier != "" {
		source["modifier"] = strings.ToLower(fn.modifier)
	}
	// Notice that the weight has to be serialized in FunctionScoreQuery.
	return source
}
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
}
774606
func (q NestedQuery) Source() interface{} {
	// {
	//   "nested" : {
	//     "query" : {
	//       "bool" : {
	//         "must" : [
	//           {
	//             "match" : {"obj1.name" : "blue"}
	//           },
	//           {
	//             "range" : {"obj1.count" : {"gt" : 5}}
	//           }
	//         ]
	//       }
	//     },
	//     "filter" : {
	//       ...
	//     },
	//     "path" : "obj1",
	//     "score_mode" : "avg",
	//     "boost" : 1.0
	//   }
	// }

	query := make(map[string]interface{})

	nq := make(map[string]interface{})
	query["nested"] = nq
	if q.query != nil {
		nq["query"] = q.query.Source()
	}
	if q.filter != nil {
		nq["filter"] = q.filter.Source()
	}
	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.innerHit != nil {
		nq["inner_hits"] = q.innerHit.Source()
	}
	return query
}
126381
func (s *GeoDistanceSort) Source() (interface{}, error) {
	source := make(map[string]interface{})
	x := make(map[string]interface{})
	source["_geo_distance"] = x

	// Points
	var ptarr []interface{}
	for _, pt := range s.points {
		ptarr = append(ptarr, pt.Source())
	}
	for _, geohash := range s.geohashes {
		ptarr = append(ptarr, geohash)
	}
	x[s.fieldName] = ptarr

	if s.unit != "" {
		x["unit"] = s.unit
	}
	if s.distanceType != nil {
		x["distance_type"] = *s.distanceType
	}

	if s.ascending {
		x["order"] = "asc"
	} else {
		x["order"] = "desc"
	}
	if s.sortMode != nil {
		x["mode"] = *s.sortMode
	}
	if s.nestedFilter != nil {
		src, err := s.nestedFilter.Source()
		if err != nil {
			return nil, err
		}
		x["nested_filter"] = src
	}
	if s.nestedPath != nil {
		x["nested_path"] = *s.nestedPath
	}
	if s.nestedSort != nil {
		src, err := s.nestedSort.Source()
		if err != nil {
			return nil, err
		}
		x["nested"] = src
	}
	return source, nil
}