264 lines
8.7 KiB
Go
264 lines
8.7 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
TermQuery = `{ "term": { "%s": "%s" } }`
|
|
TermQueryForInt = `{ "term": { "%s": %d } }`
|
|
TermQueryForBool = `{ "term": { "%s": %t } }`
|
|
RangeQuery = `{ "range": { "%s": { "gte": %d, "lte": %d } } }`
|
|
RangeQueryGteString = `{ "range": { "%s": { "gte": "%s" } } }`
|
|
MustQuery = `"must": [ %s ] `
|
|
MustNotQuery = `"must_not": [ %s ]`
|
|
ShouldQuery = `"should": [ %s ] `
|
|
BoolQuery = `{ "bool":{ %s } }`
|
|
SortQuery = `"sort": [ { "%s": { "order": "%s" } } ]`
|
|
CollapseQuery = `"collapse": { "field": "%s" }`
|
|
MatchAllQuery = `{ "match_all": {} }`
|
|
FromQuery = `"from": %d`
|
|
SizeQuery = `"size": %d`
|
|
SearchQuery = `"query": %s`
|
|
FieldsQuery = `"fields": [ "%s" ]`
|
|
EsQuery = "{ %s }"
|
|
sourceQuery = `"_source": %t`
|
|
AggregationQuery = `"aggs": { %s }`
|
|
AggregationQueryFormat = `"%s": { %s }` // aggregation name, aggregation query
|
|
TermsAggregationQuery = `"terms": { "field": "%s", "size": %d }`
|
|
MinAggregationQuery = `"min": { "field": "%s" }`
|
|
MaxAggregationQuery = `"max": { "field": "%s" }`
|
|
CardinalityAggregationQuery = `"cardinality": { "field": "%s" }`
|
|
FilterAggregationQuery = `"filter": %s`
|
|
TrackTotalHitsQuery = `"track_total_hits": %t`
|
|
ScriptQuery = `"script": { "source": "%s" , "lang": "painless" }`
|
|
TermsAggregationQueryWithOrder = `"terms": { "field": "%s", "size": %d, "order" : { "%s" : "%s" } }`
|
|
TermsAggregationQueryWithoutSize = `"terms": { "field": "%s" }`
|
|
CustomQuery = `"%s": %s`
|
|
CustomQueryWithBraces = `"%s": {%s}`
|
|
BulkUpdateQuery = `{ "update": {"_index": "%s", "_id": "%s" } }`
|
|
TermsSubQuery = `{ "terms": { "%s": [ %s ] } }`
|
|
MultiSortQuery = `"sort":[ %s ]`
|
|
SortQueryFields = `{ "%s": { "order": "%s" } }`
|
|
MatchQuery = `{ "match": { "%s": "%s" } }`
|
|
SearchAfter = `"search_after": [%s]`
|
|
CompositeAggsQuery = `"composite": {"size" : %d, "sources" : [{"%s": {%s}}] }`
|
|
CompositeAggsQueryWithAfterKey = `"composite": {"size" : %d, "after": {"%s": "%s"}, "sources" : [{"%s": {%s}}] }`
|
|
|
|
TopHitsAggsQuery = `"top_hits":{"size" : %d, "_source": {"includes" : [ %s ]}}`
|
|
ValueCountAggsQuery = `"value_count" : {"field": "%s"}`
|
|
)
|
|
|
|
const (
|
|
COLON = ":"
|
|
HYPHEN = "-"
|
|
EMPTY = ""
|
|
UNDERSCORE = "_"
|
|
NEWLINE = "\n"
|
|
FORWARD_SLASH = "/"
|
|
ASTERISK = "*"
|
|
DOT = "."
|
|
COMMA = ","
|
|
SEMICOLON = ";"
|
|
AMPERSAND = "&"
|
|
)
|
|
|
|
func CreateCustomQuery(key string, query string) string {
|
|
return fmt.Sprintf(CustomQuery, key, query)
|
|
}
|
|
|
|
func CreateCustomQueryWithBraces(key string, query string) string {
|
|
return fmt.Sprintf(CustomQueryWithBraces, key, query)
|
|
}
|
|
|
|
func CreateTermSubQuery(key string, ids ...string) string {
|
|
var termQueries []string
|
|
for _, id := range ids {
|
|
idCopy := id
|
|
termQueries = append(termQueries, fmt.Sprintf(TermQuery, key, idCopy))
|
|
}
|
|
return strings.Join(termQueries, ",")
|
|
}
|
|
|
|
func CreateTermSubQueryForInt(key string, ids ...int64) string {
|
|
var termQueries []string
|
|
for _, id := range ids {
|
|
idCopy := id
|
|
termQueries = append(termQueries, fmt.Sprintf(TermQueryForInt, key, idCopy))
|
|
}
|
|
return strings.Join(termQueries, ",")
|
|
}
|
|
|
|
func CreateTermSubQueryBool(key string, ids ...bool) string {
|
|
var termQueries []string
|
|
for _, id := range ids {
|
|
idCopy := id
|
|
termQueries = append(termQueries, fmt.Sprintf(TermQueryForBool, key, idCopy))
|
|
}
|
|
return strings.Join(termQueries, ",")
|
|
}
|
|
|
|
func CreateRangeQuery(key string, greaterThan int64, lessThan int64) string {
|
|
return fmt.Sprintf(RangeQuery, key, greaterThan, lessThan)
|
|
}
|
|
|
|
func CreateRangeQueryForGteString(key string, greaterThan string) string {
|
|
return fmt.Sprintf(RangeQueryGteString, key, greaterThan)
|
|
}
|
|
|
|
func CreateMustQuery(filters ...string) string {
|
|
return fmt.Sprintf(MustQuery, strings.Join(filters, ","))
|
|
}
|
|
|
|
func CreateMatchAllQuery() string {
|
|
return MatchAllQuery
|
|
}
|
|
|
|
func CreateMatchQuery(key string, value string) string {
|
|
return fmt.Sprintf(MatchQuery, key, value)
|
|
}
|
|
|
|
func CreateShouldQuery(filters ...string) string {
|
|
return fmt.Sprintf(ShouldQuery, strings.Join(filters, ","))
|
|
}
|
|
|
|
func CreateBoolQuery(filters ...string) string {
|
|
return fmt.Sprintf(BoolQuery, strings.Join(filters, ","))
|
|
}
|
|
|
|
func CreateSearchQuery(filters ...string) string {
|
|
return fmt.Sprintf(SearchQuery, strings.Join(filters, ","))
|
|
}
|
|
|
|
func CreateFieldsQuery(fields ...string) string {
|
|
return fmt.Sprintf(FieldsQuery, strings.Join(fields, ","))
|
|
}
|
|
|
|
func CreateSortQuery(key string, order string, format string) string {
|
|
if format != EMPTY {
|
|
order += fmt.Sprintf(`", "format": "%s`, format)
|
|
}
|
|
return fmt.Sprintf(SortQuery, key, order)
|
|
}
|
|
|
|
func CreateCollapseQuery(key string) string {
|
|
return fmt.Sprintf(CollapseQuery, key)
|
|
}
|
|
|
|
func CreateCardinalityAggregationQuery(field string) string {
|
|
return fmt.Sprintf(CardinalityAggregationQuery, field)
|
|
}
|
|
|
|
func CreateFromQuery(from int64) string {
|
|
return fmt.Sprintf(FromQuery, from)
|
|
}
|
|
|
|
func CreateSizeQuery(size int64) string {
|
|
return fmt.Sprintf(SizeQuery, size)
|
|
}
|
|
|
|
func CreateEsQuery(query ...string) string {
|
|
return fmt.Sprintf(EsQuery, strings.Join(query, ","))
|
|
}
|
|
|
|
func CreateSourceQuery(source bool) string {
|
|
return fmt.Sprintf(sourceQuery, source)
|
|
}
|
|
|
|
func CreateFilterAggregationQuery(value ...string) string {
|
|
return fmt.Sprintf(FilterAggregationQuery, strings.Join(value, ","))
|
|
}
|
|
|
|
func CreateAggregationQuery(aggregations ...string) string {
|
|
return fmt.Sprintf(AggregationQuery, strings.Join(aggregations, ","))
|
|
}
|
|
|
|
func CreateMinAggregationQuery(field string) string {
|
|
return fmt.Sprintf(MinAggregationQuery, field)
|
|
}
|
|
|
|
func CreateMaxAggregationQuery(field string) string {
|
|
return fmt.Sprintf(MaxAggregationQuery, field)
|
|
}
|
|
|
|
func CreateTermsAggregationQuery(field string, size int) string {
|
|
return fmt.Sprintf(TermsAggregationQuery, field, size)
|
|
}
|
|
|
|
func CreateTermsAggregationQueryWithoutSize(field string) string {
|
|
return fmt.Sprintf(TermsAggregationQueryWithoutSize, field)
|
|
}
|
|
|
|
func BuildAggregationQuery(aggregationName string, aggregationQueries ...string) string {
|
|
return fmt.Sprintf(AggregationQueryFormat, aggregationName, strings.Join(aggregationQueries, ","))
|
|
}
|
|
|
|
func CreateTrackTotalHitsQuery(trackTotalHits bool) string {
|
|
return fmt.Sprintf(TrackTotalHitsQuery, trackTotalHits)
|
|
}
|
|
|
|
func CreateBoolShouldQuery(queries ...string) string {
|
|
return fmt.Sprintf(CreateBoolQuery(CreateShouldQuery(strings.Join(queries, ","))))
|
|
}
|
|
|
|
func CreateScriptQuery(script string) string {
|
|
return fmt.Sprintf(ScriptQuery, script)
|
|
}
|
|
|
|
func CreateMustNotQuery(filters ...string) string {
|
|
return fmt.Sprintf(MustNotQuery, strings.Join(filters, ","))
|
|
}
|
|
|
|
func CreateTermsAggregationQueryWithOrder(field string, size int, filter, order string) string {
|
|
return fmt.Sprintf(TermsAggregationQueryWithOrder, field, size, filter, order)
|
|
}
|
|
|
|
func CreateBulkUpdateQuery(index, docId string) string {
|
|
return fmt.Sprintf(BulkUpdateQuery, index, docId)
|
|
}
|
|
|
|
func CreateTermsSubQuery(key string, ids []string) string {
|
|
var idsList []string
|
|
for _, id := range ids {
|
|
idsList = append(idsList, fmt.Sprintf(`"%s"`, id))
|
|
}
|
|
return fmt.Sprintf(TermsSubQuery, key, strings.Join(idsList, ","))
|
|
}
|
|
|
|
func CreateSortQueryFields(key string, order string, format string) string {
|
|
if format != EMPTY {
|
|
order += fmt.Sprintf(`", "format": "%s`, format)
|
|
}
|
|
return fmt.Sprintf(SortQueryFields, key, order)
|
|
}
|
|
|
|
func CreateMultiSortQuery(sortQueries ...string) string {
|
|
return fmt.Sprintf(MultiSortQuery, strings.Join(sortQueries, ","))
|
|
}
|
|
|
|
func CreateSearchAfterQuery(searchQuery ...string) string {
|
|
return fmt.Sprintf(SearchAfter, strings.Join(searchQuery, ","))
|
|
}
|
|
|
|
func CreateCompositeAggsQuery(size int, key string, afterKey string, afterKeyValue string, sources ...string) string {
|
|
if afterKeyValue != EMPTY {
|
|
println("in after key query")
|
|
return fmt.Sprintf(CompositeAggsQueryWithAfterKey, size, afterKey, afterKeyValue, key, strings.Join(sources, ","))
|
|
|
|
}
|
|
return fmt.Sprintf(CompositeAggsQuery, size, key, strings.Join(sources, ","))
|
|
}
|
|
|
|
func CreateTopHitsAggsQuery(size int, fields []string) string {
|
|
var fieldsList []string
|
|
for _, field := range fields {
|
|
fieldsList = append(fieldsList, fmt.Sprintf(`"%s"`, field))
|
|
}
|
|
return fmt.Sprintf(TopHitsAggsQuery, size, strings.Join(fieldsList, ", "))
|
|
}
|
|
|
|
func CreateValueCountAggsQuery(field string) string {
|
|
return fmt.Sprintf(ValueCountAggsQuery, field)
|
|
}
|