// Source returns JSON for the query.
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
}
983425
func (q *SimpleQueryStringQuery) Source() (interface{}, error) {
	// {
	//    "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 {
		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.flags != "" {
		query["flags"] = q.flags
	}
	if q.analyzer != "" {
		query["analyzer"] = q.analyzer
	}
	if q.defaultOperator != "" {
		query["default_operator"] = strings.ToLower(q.defaultOperator)
	}
	if q.lowercaseExpandedTerms != nil {
		query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms
	}
	if q.lenient != nil {
		query["lenient"] = *q.lenient
	}
	if q.analyzeWildcard != nil {
		query["analyze_wildcard"] = *q.analyzeWildcard
	}
	if q.locale != "" {
		query["locale"] = q.locale
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}
	if q.minimumShouldMatch != "" {
		query["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.quoteFieldSuffix != "" {
		query["quote_field_suffix"] = q.quoteFieldSuffix
	}
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if v := q.autoGenerateSynonymsPhraseQuery; v != nil {
		query["auto_generate_synonyms_phrase_query"] = *v
	}
	if v := q.fuzzyPrefixLength; v != -1 {
		query["fuzzy_prefix_length"] = v
	}
	if v := q.fuzzyMaxExpansions; v != -1 {
		query["fuzzy_max_expansions"] = v
	}
	if v := q.fuzzyTranspositions; v != nil {
		query["fuzzy_transpositions"] = *v
	}

	return source, nil
}
620970
func (q *TermQuery) Source() (interface{}, error) {
	// {"term":{"name":"value"}}
	source := make(map[string]interface{})
	tq := make(map[string]interface{})
	source["term"] = tq

	if q.boost == nil && q.queryName == "" {
		tq[q.name] = q.value
	} else {
		subQ := make(map[string]interface{})
		subQ["value"] = q.value
		if q.boost != nil {
			subQ["boost"] = *q.boost
		}
		if q.queryName != "" {
			subQ["_name"] = q.queryName
		}
		tq[q.name] = subQ
	}
	return source, nil
}
273656
func (q *SuggesterGeoQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})

	x := make(map[string]interface{})
	source[q.name] = x

	if q.location != nil {
		x["context"] = q.location.Source()
	}
	if q.precision != "" {
		x["precision"] = q.precision
	}
	if q.boost != nil {
		x["boost"] = q.boost
	}
	switch len(q.neighbours) {
	case 0:
	case 1:
		x["neighbours"] = q.neighbours[0]
	default:
		x["neighbours"] = q.neighbours
	}

	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
}
414492
func (s *IndicesShrinkService) Source(source string) *IndicesShrinkService {
	s.source = source
	return s
}
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
}
244200
func (n *Notifier) Source() Source {
	n.lock.RLock()
	defer n.lock.RUnlock()
	return n.source
}
47907
func (q TemplateQuery) Source() interface{} {
	// {
	//   "template" : {
	//     "query" : {"match_{{template}}": {}},
	//     "params" : {
	//       "template": "all"
	//     }
	//   }
	// }

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

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

	// TODO(oe): Implementation differs from online documentation at http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-template-query.html
	var fieldname string
	switch q.templateType {
	case "file": // file
		fieldname = "file"
	case "indexed", "id": // indexed
		fieldname = "id"
	default: // inline
		fieldname = "query"
	}

	tmpl[fieldname] = q.template
	if len(q.vars) > 0 {
		tmpl["params"] = q.vars
	}

	return query
}
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
}
622619
func (q CustomScoreQuery) Source() interface{} {
	// "custom_score" : {
	//     "query" : {
	//         ....
	//     },
	//     "params" : {
	//         "param1" : 2,
	//         "param2" : 3.1
	//     },
	//     "script" : "_score * doc['my_numeric_field'].value / pow(param1, param2)"
	// }

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

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

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

	csq["script"] = q.script

	// lang
	if q.lang != "" {
		csq["lang"] = q.lang
	}

	// params
	if len(q.params) > 0 {
		csq["params"] = q.params
	}

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

	return query
}
1123308
func (q *HasParentQuery) Source() (interface{}, error) {
	// {
	//   "has_parent" : {
	//       "parent_type" : "blog",
	//       "query" : {
	//           "term" : {
	//               "tag" : "something"
	//           }
	//       }
	//   }
	// }
	source := make(map[string]interface{})
	query := make(map[string]interface{})
	source["has_parent"] = query

	src, err := q.query.Source()
	if err != nil {
		return nil, err
	}
	query["query"] = src
	query["parent_type"] = q.parentType
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.score != nil {
		query["score"] = *q.score
	}
	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
	}
	if q.ignoreUnmapped != nil {
		query["ignore_unmapped"] = *q.ignoreUnmapped
	}
	return source, nil
}
909088
func (q *PrefixQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})
	query := make(map[string]interface{})
	source["prefix"] = query

	if q.boost == nil && q.rewrite == "" && q.queryName == "" {
		query[q.name] = q.prefix
	} else {
		subQuery := make(map[string]interface{})
		subQuery["value"] = q.prefix
		if q.boost != nil {
			subQuery["boost"] = *q.boost
		}
		if q.rewrite != "" {
			subQuery["rewrite"] = q.rewrite
		}
		if q.queryName != "" {
			subQuery["_name"] = q.queryName
		}
		query[q.name] = subQuery
	}

	return source, nil
}
602957
func (q *TermsSetQuery) Source() (interface{}, error) {
	// {"terms_set":{"codes":{"terms":["abc","def"],"minimum_should_match_field":"required_matches"}}}
	source := make(map[string]interface{})
	inner := make(map[string]interface{})
	params := make(map[string]interface{})
	inner[q.name] = params
	source["terms_set"] = inner

	// terms
	params["terms"] = q.values

	// minimum_should_match_field
	if match := q.minimumShouldMatchField; match != "" {
		params["minimum_should_match_field"] = match
	}

	// minimum_should_match_script
	if match := q.minimumShouldMatchScript; match != nil {
		src, err := match.Source()
		if err != nil {
			return nil, err
		}
		params["minimum_should_match_script"] = src
	}

	// Common parameters for all queries
	if q.boost != nil {
		params["boost"] = *q.boost
	}
	if q.queryName != "" {
		params["_name"] = q.queryName
	}

	return source, nil
}
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
}
143797
func (q WildcardQuery) Source() interface{} {
	// {
	//	"wildcard" : {
	//		"user" : {
	//      "wildcard" : "ki*y",
	//      "boost" : 1.0
	//    }
	// }

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

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

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

	wq["wildcard"] = q.wildcard

	if q.boost != -1.0 {
		wq["boost"] = q.boost
	}
	if q.rewrite != "" {
		wq["rewrite"] = q.rewrite
	}
	if q.queryName != "" {
		wq["_name"] = q.queryName
	}

	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
}