215 lines
7.1 KiB
Go
215 lines
7.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"alfred/utils"
|
|
"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`
|
|
BulkUpdateQuery = `{ "update": {"_index": "%s", "_id": "%s" } }`
|
|
TermsSubQuery = `{ "terms": { "%s": [ %s ] } }`
|
|
MultiSortQuery = `"sort":[ %s ]`
|
|
SortQueryFields = `{ "%s": { "order": "%s" } }`
|
|
MatchQuery = `{ "match": { "%s": "%s" } }`
|
|
)
|
|
|
|
func createCustomQuery(key string, query string) string {
|
|
return fmt.Sprintf(CustomQuery, 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 != utils.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 != utils.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, ","))
|
|
}
|