// Creates the query source for the boosting 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
}
1120779
func (q *BoolQuery) Source() (interface{}, error) {
	// {
	//	"bool" : {
	//		"must" : {
	//			"term" : { "user" : "kimchy" }
	//		},
	//		"must_not" : {
	//			"range" : {
	//				"age" : { "from" : 10, "to" : 20 }
	//			}
	//		},
	//    "filter" : [
	//      ...
	//    ]
	//		"should" : [
	//			{
	//				"term" : { "tag" : "wow" }
	//			},
	//			{
	//				"term" : { "tag" : "elasticsearch" }
	//			}
	//		],
	//		"minimum_should_match" : 1,
	//		"boost" : 1.0
	//	}
	// }

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

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

	// must
	if len(q.mustClauses) == 1 {
		src, err := q.mustClauses[0].Source()
		if err != nil {
			return nil, err
		}
		boolClause["must"] = src
	} else if len(q.mustClauses) > 1 {
		var clauses []interface{}
		for _, subQuery := range q.mustClauses {
			src, err := subQuery.Source()
			if err != nil {
				return nil, err
			}
			clauses = append(clauses, src)
		}
		boolClause["must"] = clauses
	}

	// must_not
	if len(q.mustNotClauses) == 1 {
		src, err := q.mustNotClauses[0].Source()
		if err != nil {
			return nil, err
		}
		boolClause["must_not"] = src
	} else if len(q.mustNotClauses) > 1 {
		var clauses []interface{}
		for _, subQuery := range q.mustNotClauses {
			src, err := subQuery.Source()
			if err != nil {
				return nil, err
			}
			clauses = append(clauses, src)
		}
		boolClause["must_not"] = clauses
	}

	// filter
	if len(q.filterClauses) == 1 {
		src, err := q.filterClauses[0].Source()
		if err != nil {
			return nil, err
		}
		boolClause["filter"] = src
	} else if len(q.filterClauses) > 1 {
		var clauses []interface{}
		for _, subQuery := range q.filterClauses {
			src, err := subQuery.Source()
			if err != nil {
				return nil, err
			}
			clauses = append(clauses, src)
		}
		boolClause["filter"] = clauses
	}

	// should
	if len(q.shouldClauses) == 1 {
		src, err := q.shouldClauses[0].Source()
		if err != nil {
			return nil, err
		}
		boolClause["should"] = src
	} else if len(q.shouldClauses) > 1 {
		var clauses []interface{}
		for _, subQuery := range q.shouldClauses {
			src, err := subQuery.Source()
			if err != nil {
				return nil, err
			}
			clauses = append(clauses, src)
		}
		boolClause["should"] = clauses
	}

	if q.boost != nil {
		boolClause["boost"] = *q.boost
	}
	if q.minimumShouldMatch != "" {
		boolClause["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.adjustPureNegative != nil {
		boolClause["adjust_pure_negative"] = *q.adjustPureNegative
	}
	if q.queryName != "" {
		boolClause["_name"] = q.queryName
	}

	return query, nil
}
244632
func (q *SuggesterGeoQuery) Source() interface{} {
	source := make(map[string]interface{})

	if len(q.precision) == 0 {
		if q.location != nil {
			source[q.name] = q.location.Source()
		}
	} else {
		x := make(map[string]interface{})
		source[q.name] = x

		if q.location != nil {
			x["value"] = q.location.Source()
		}

		switch len(q.precision) {
		case 0:
		case 1:
			x["precision"] = q.precision[0]
		default:
			x["precision"] = q.precision
		}
	}

	return source
}
1286001
func (q FuzzyQuery) Source() interface{} {
	// {
	//	"fuzzy" : {
	//		"user" : {
	//      "value" : "ki",
	//      "boost" : 1.0,
	//      "fuzziness" : 2,
	//      "prefix_length" : 0,
	//      "max_expansions" : 100
	//    }
	// }

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

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

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

	fq["value"] = q.value

	if q.boost != -1.0 {
		fq["boost"] = q.boost
	}
	if q.transpositions != nil {
		fq["transpositions"] = *q.transpositions
	}
	if q.fuzziness != nil {
		fq["fuzziness"] = q.fuzziness
	}
	if q.prefixLength != nil {
		fq["prefix_length"] = *q.prefixLength
	}
	if q.maxExpansions != nil {
		fq["max_expansions"] = *q.maxExpansions
	}
	if q.queryName != "" {
		fq["_name"] = q.queryName
	}

	return source
}
1108810
func (q *NumericRangeQuery) Boost(boost float32) *NumericRangeQuery {
	q.options["boost"] = boost
	return q
}
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
}
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
}
1049889
func (q *IdsQuery) Boost(boost float64) *IdsQuery {
	q.boost = &boost
	return q
}
1157421
func (q *HasChildQuery) Boost(boost float64) *HasChildQuery {
	q.boost = &boost
	return q
}
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
}
338507
func (q HasChildQuery) Source() interface{} {
	// {
	//   "has_child" : {
	//       "type" : "blog_tag",
	//       "query" : {
	//           "term" : {
	//               "tag" : "something"
	//           }
	//       }
	//   }
	// }
	source := make(map[string]interface{})

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

	query["query"] = q.query.Source()
	query["type"] = q.childType
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.scoreType != "" {
		query["score_type"] = q.scoreType
	}
	if q.minChildren != nil {
		query["min_children"] = *q.minChildren
	}
	if q.maxChildren != nil {
		query["max_children"] = *q.maxChildren
	}
	if q.shortCircuitCutoff != nil {
		query["short_circuit_cutoff"] = *q.shortCircuitCutoff
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}
	if q.innerHit != nil {
		query["inner_hits"] = q.innerHit.Source()
	}
	return source
}
1169586
func (_class SRClass) QueryDataSource(sessionID SessionRef, sr SRRef, dataSource string) (_retval float64, _err error) {
	_method := "SR.query_data_source"
	_sessionIDArg, _err := convertSessionRefToXen(fmt.Sprintf("%s(%s)", _method, "session_id"), sessionID)
	if _err != nil {
		return
	}
	_srArg, _err := convertSRRefToXen(fmt.Sprintf("%s(%s)", _method, "sr"), sr)
	if _err != nil {
		return
	}
	_dataSourceArg, _err := convertStringToXen(fmt.Sprintf("%s(%s)", _method, "data_source"), dataSource)
	if _err != nil {
		return
	}
	_result, _err := _class.client.APICall(_method, _sessionIDArg, _srArg, _dataSourceArg)
	if _err != nil {
		return
	}
	_retval, _err = convertFloatToGo(_method + " -> ", _result.Value)
	return
}
645571
func (q CompletionSuggester) Source(includeName bool) interface{} {
	cs := &completionSuggesterRequest{}

	if q.text != "" {
		cs.Text = q.text
	}

	suggester := make(map[string]interface{})
	cs.Completion = suggester

	if q.analyzer != "" {
		suggester["analyzer"] = q.analyzer
	}
	if q.field != "" {
		suggester["field"] = q.field
	}
	if q.size != nil {
		suggester["size"] = *q.size
	}
	if q.shardSize != nil {
		suggester["shard_size"] = *q.shardSize
	}
	switch len(q.contextQueries) {
	case 0:
	case 1:
		suggester["context"] = q.contextQueries[0].Source()
	default:
		ctxq := make([]interface{}, 0)
		for _, query := range q.contextQueries {
			ctxq = append(ctxq, query.Source())
		}
		suggester["context"] = ctxq
	}

	// TODO(oe) Add competion-suggester specific parameters here

	if !includeName {
		return cs
	}

	source := make(map[string]interface{})
	source[q.name] = cs
	return source
}
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
}
738711
func (q *QueryStringQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})
	query := make(map[string]interface{})
	source["query_string"] = query

	query["query"] = q.queryString

	if q.defaultField != "" {
		query["default_field"] = q.defaultField
	}

	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)
			}
		}
		query["fields"] = fields
	}

	if q.tieBreaker != nil {
		query["tie_breaker"] = *q.tieBreaker
	}
	if q.defaultOperator != "" {
		query["default_operator"] = q.defaultOperator
	}
	if q.analyzer != "" {
		query["analyzer"] = q.analyzer
	}
	if q.quoteAnalyzer != "" {
		query["quote_analyzer"] = q.quoteAnalyzer
	}
	if q.maxDeterminizedStates != nil {
		query["max_determinized_states"] = *q.maxDeterminizedStates
	}
	if q.allowLeadingWildcard != nil {
		query["allow_leading_wildcard"] = *q.allowLeadingWildcard
	}
	if q.lowercaseExpandedTerms != nil {
		query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms
	}
	if q.enablePositionIncrements != nil {
		query["enable_position_increments"] = *q.enablePositionIncrements
	}
	if q.fuzziness != "" {
		query["fuzziness"] = q.fuzziness
	}
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.fuzzyPrefixLength != nil {
		query["fuzzy_prefix_length"] = *q.fuzzyPrefixLength
	}
	if q.fuzzyMaxExpansions != nil {
		query["fuzzy_max_expansions"] = *q.fuzzyMaxExpansions
	}
	if q.fuzzyRewrite != "" {
		query["fuzzy_rewrite"] = q.fuzzyRewrite
	}
	if q.phraseSlop != nil {
		query["phrase_slop"] = *q.phraseSlop
	}
	if q.analyzeWildcard != nil {
		query["analyze_wildcard"] = *q.analyzeWildcard
	}
	if q.rewrite != "" {
		query["rewrite"] = q.rewrite
	}
	if q.minimumShouldMatch != "" {
		query["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.quoteFieldSuffix != "" {
		query["quote_field_suffix"] = q.quoteFieldSuffix
	}
	if q.lenient != nil {
		query["lenient"] = *q.lenient
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}
	if q.locale != "" {
		query["locale"] = q.locale
	}
	if q.timeZone != "" {
		query["time_zone"] = q.timeZone
	}
	if q.escape != nil {
		query["escape"] = *q.escape
	}
	if q.typ != "" {
		query["type"] = q.typ
	}

	return source, nil
}
811630
func (q *MatchQuery) Source() (interface{}, error) {
	// {"match":{"name":{"query":"value","type":"boolean/phrase"}}}
	source := make(map[string]interface{})

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

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

	query["query"] = q.text

	if q.operator != "" {
		query["operator"] = q.operator
	}
	if q.analyzer != "" {
		query["analyzer"] = q.analyzer
	}
	if q.fuzziness != "" {
		query["fuzziness"] = q.fuzziness
	}
	if q.prefixLength != nil {
		query["prefix_length"] = *q.prefixLength
	}
	if q.maxExpansions != nil {
		query["max_expansions"] = *q.maxExpansions
	}
	if q.minimumShouldMatch != "" {
		query["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.fuzzyRewrite != "" {
		query["fuzzy_rewrite"] = q.fuzzyRewrite
	}
	if q.lenient != nil {
		query["lenient"] = *q.lenient
	}
	if q.fuzzyTranspositions != nil {
		query["fuzzy_transpositions"] = *q.fuzzyTranspositions
	}
	if q.zeroTermsQuery != "" {
		query["zero_terms_query"] = q.zeroTermsQuery
	}
	if q.cutoffFrequency != nil {
		query["cutoff_frequency"] = *q.cutoffFrequency
	}
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}

	return source, nil
}
975721
func (q *ScriptQuery) Source() (interface{}, error) {
	if q.script == nil {
		return nil, errors.New("ScriptQuery expected a script")
	}
	source := make(map[string]interface{})
	params := make(map[string]interface{})
	source["script"] = params

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

	if q.queryName != "" {
		params["_name"] = q.queryName
	}
	return source, nil
}