// Source returns JSON for the query.
503315
func (q *MultiMatchQuery) Source() (interface{}, error) {
	//
	// {
	//   "multi_match" : {
	//     "query" : "this is a test",
	//     "fields" : [ "subject", "message" ]
	//   }
	// }

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

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

	multiMatch["query"] = q.text

	if len(q.fields) > 0 {
		var fields []string
		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)
			}
		}
		multiMatch["fields"] = fields
	}

	if q.typ != "" {
		multiMatch["type"] = q.typ
	}

	if q.operator != "" {
		multiMatch["operator"] = q.operator
	}
	if q.analyzer != "" {
		multiMatch["analyzer"] = q.analyzer
	}
	if q.boost != nil {
		multiMatch["boost"] = *q.boost
	}
	if q.slop != nil {
		multiMatch["slop"] = *q.slop
	}
	if q.fuzziness != "" {
		multiMatch["fuzziness"] = q.fuzziness
	}
	if q.prefixLength != nil {
		multiMatch["prefix_length"] = *q.prefixLength
	}
	if q.maxExpansions != nil {
		multiMatch["max_expansions"] = *q.maxExpansions
	}
	if q.minimumShouldMatch != "" {
		multiMatch["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.rewrite != "" {
		multiMatch["rewrite"] = q.rewrite
	}
	if q.fuzzyRewrite != "" {
		multiMatch["fuzzy_rewrite"] = q.fuzzyRewrite
	}
	if q.tieBreaker != nil {
		multiMatch["tie_breaker"] = *q.tieBreaker
	}
	if q.lenient != nil {
		multiMatch["lenient"] = *q.lenient
	}
	if q.cutoffFrequency != nil {
		multiMatch["cutoff_frequency"] = *q.cutoffFrequency
	}
	if q.zeroTermsQuery != "" {
		multiMatch["zero_terms_query"] = q.zeroTermsQuery
	}
	if q.queryName != "" {
		multiMatch["_name"] = q.queryName
	}
	return source, nil
}
1080456
func (q *GeoPolygonQuery) Source() (interface{}, error) {
	// "geo_polygon" : {
	//  	"person.location" : {
	//         "points" : [
	//             {"lat" : 40, "lon" : -70},
	//             {"lat" : 30, "lon" : -80},
	//             {"lat" : 20, "lon" : -90}
	//         ]
	//     }
	// }
	source := make(map[string]interface{})

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

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

	var points []interface{}
	for _, point := range q.points {
		points = append(points, point.Source())
	}
	polygon["points"] = points

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

	return source, nil
}
1209841
func (s *ReindexService) Source(source *ReindexSource) *ReindexService {
	s.source = source
	return s
}
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
}
1121197
func (r *ReindexSource) Source() (interface{}, error) {
	src, err := r.request.sourceAsMap()
	if err != nil {
		return nil, err
	}
	source, ok := src.(map[string]interface{})
	if !ok {
		return nil, errors.New("unable to use SearchRequest as map[string]interface{}")
	}

	switch len(r.request.indices) {
	case 1:
		source["index"] = r.request.indices[0]
	default:
		source["index"] = r.request.indices
	}
	switch len(r.request.types) {
	case 0:
	case 1:
		source["type"] = r.request.types[0]
	default:
		source["type"] = r.request.types
	}
	if r.remoteInfo != nil {
		src, err := r.remoteInfo.Source()
		if err != nil {
			return nil, err
		}
		source["remote"] = src
	}
	return source, nil
}
1069911
func (q MoreLikeThisFieldQuery) Source() interface{} {
	// {
	//     "more_like_this_field" : {
	//         "name.first" : {
	//             "like_text" : "text like this one",
	//             "min_term_freq" : 1,
	//             "max_query_terms" : 12
	//         }
	//     }
	// }

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

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

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

	mlt["like_text"] = q.likeText

	if q.percentTermsToMatch != nil {
		mlt["percent_terms_to_match"] = *q.percentTermsToMatch
	}

	if q.minTermFreq != nil {
		mlt["min_term_freq"] = *q.minTermFreq
	}

	if q.maxQueryTerms != nil {
		mlt["max_query_terms"] = *q.maxQueryTerms
	}

	if len(q.stopWords) > 0 {
		mlt["stop_words"] = q.stopWords
	}

	if q.minDocFreq != nil {
		mlt["min_doc_freq"] = *q.minDocFreq
	}

	if q.maxDocFreq != nil {
		mlt["max_doc_freq"] = *q.maxDocFreq
	}

	if q.minWordLen != nil {
		mlt["min_word_len"] = *q.minWordLen
	}

	if q.maxWordLen != nil {
		mlt["max_word_len"] = *q.maxWordLen
	}

	if q.boostTerms != nil {
		mlt["boost_terms"] = *q.boostTerms
	}

	if q.boost != nil {
		mlt["boost"] = *q.boost
	}

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

	if q.failOnUnsupportedField != nil {
		mlt["fail_on_unsupported_field"] = *q.failOnUnsupportedField
	}
	return source
}
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
}
1153483
func (ri *ReindexRemoteInfo) Source() (interface{}, error) {
	res := make(map[string]interface{})
	res["host"] = ri.host
	if len(ri.username) > 0 {
		res["username"] = ri.username
	}
	if len(ri.password) > 0 {
		res["password"] = ri.password
	}
	if len(ri.socketTimeout) > 0 {
		res["socket_timeout"] = ri.socketTimeout
	}
	if len(ri.connectTimeout) > 0 {
		res["connect_timeout"] = ri.connectTimeout
	}
	return res, nil
}
978881
func (q *GeoDistanceQuery) Source() (interface{}, error) {
	// {
	//   "geo_distance" : {
	//       "distance" : "200km",
	//       "pin.location" : {
	//           "lat" : 40,
	//           "lon" : -70
	//       }
	//   }
	// }

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

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

	if q.geohash != "" {
		params[q.name] = q.geohash
	} else {
		location := make(map[string]interface{})
		location["lat"] = q.lat
		location["lon"] = q.lon
		params[q.name] = location
	}

	if q.distance != "" {
		params["distance"] = q.distance
	}
	if q.distanceType != "" {
		params["distance_type"] = q.distanceType
	}
	if q.queryName != "" {
		params["_name"] = q.queryName
	}

	source["geo_distance"] = params

	return source, nil
}
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
}
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
}
574687
func (s *Script) Source() (interface{}, error) {
	if s.typ == "" && s.lang == "" && len(s.params) == 0 {
		return s.script, nil
	}
	source := make(map[string]interface{})
	// Beginning with 6.0, the type can only be "source" or "id"
	if s.typ == "" || s.typ == "inline" {
		src, err := s.rawScriptSource(s.script)
		if err != nil {
			return nil, err
		}
		source["source"] = src
	} else {
		source["id"] = s.script
	}
	if s.lang != "" {
		source["lang"] = s.lang
	}
	if len(s.params) > 0 {
		source["params"] = s.params
	}
	return source, nil
}
1141521
func (q *ParentIdQuery) Source() (interface{}, error) {
	// {
	//   "parent_id" : {
	//       "type" : "blog",
	//       "id" : "1"
	//   }
	// }
	source := make(map[string]interface{})
	query := make(map[string]interface{})
	source["parent_id"] = query

	query["type"] = q.typ
	query["id"] = q.id
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.ignoreUnmapped != nil {
		query["ignore_unmapped"] = *q.ignoreUnmapped
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}
	if q.innerHit != nil {
		src, err := q.innerHit.Source()
		if err != nil {
			return nil, err
		}
		query["inner_hits"] = src
	}
	return source, nil
}
847653
func (q DisMaxQuery) Source() interface{} {
	// {
	//  "dis_max" : {
	//    "tie_breaker" : 0.7,
	//    "boost" : 1.2,
	//    "queries" : {
	//      {
	//        "term" : { "age" : 34 }
	//      },
	//      {
	//        "term" : { "age" : 35 }
	//      }
	//    ]
	//  }
	// }

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

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

	// tieBreaker
	if q.tieBreaker != nil {
		disMax["tie_breaker"] = *q.tieBreaker
	}

	// boost
	if q.boost != nil {
		disMax["boost"] = *q.boost
	}

	// queries
	clauses := make([]interface{}, 0)
	for _, subQuery := range q.queries {
		clauses = append(clauses, subQuery.Source())
	}
	disMax["queries"] = clauses

	return query
}
340661
func (q *BoostingQuery) Source() (interface{}, error) {
	// {
	//     "boosting" : {
	//         "positive" : {
	//             "term" : {
	//                 "field1" : "value1"
	//             }
	//         },
	//         "negative" : {
	//             "term" : {
	//                 "field2" : "value2"
	//             }
	//         },
	//         "negative_boost" : 0.2
	//     }
	// }

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

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

	// Negative and positive clause as well as negative boost
	// are mandatory in the Java client.

	// positive
	if q.positiveClause != nil {
		src, err := q.positiveClause.Source()
		if err != nil {
			return nil, err
		}
		boostingClause["positive"] = src
	}

	// negative
	if q.negativeClause != nil {
		src, err := q.negativeClause.Source()
		if err != nil {
			return nil, err
		}
		boostingClause["negative"] = src
	}

	if q.negativeBoost != nil {
		boostingClause["negative_boost"] = *q.negativeBoost
	}

	if q.boost != nil {
		boostingClause["boost"] = *q.boost
	}

	return query, 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
}
714136
func (q MatchAllQuery) Source() (interface{}, error) {
	// {
	//   "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.queryName != "" {
		params["_name"] = q.queryName
	}
	return source, nil
}