I'm new to Goleng and Elastic. I have a static query that I took out of the Kibana, now I want to use this query to build a dynamic query code according to the parameters that the customer will send
how i add or remove "event_state.status" according to user parameters.
how i convert this json to map[string]interface {}
.
The client package i was use its https://github.com/elastic/go-elasticsearch
this is the json query :
{
"sort": [
{
"@timestamp": {
"order": "desc",
"unmapped_type": "boolean"
}
}
],
"from": 0,
"size": 10000,
"query": {
"bool": {
"filter": [
{
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match_phrase": {
"webhook_id": "12345"
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"bool": {
"should": [
{
"match": {
"event_state.status": "rejected"
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"match": {
"event_state.status": "succeeded"
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"match": {
"event_state.status": "expired"
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"match": {
"event_state.status": "inactive"
}
}
],
"minimum_should_match": 1
}
}
],
"minimum_should_match": 1
}
}
]
}
},
{
"range": {
"created_at": {
"format": "strict_date_optional_time",
"gte": "2022-02-16T03:19:50.267Z",
"lte": "2022-02-17T03:19:50.267Z"
}
}
}
]
}
},
"collapse": {
"field": "uuid.keyword",
"inner_hits": {
"name": "order by attempt_count",
"size": 1,
"sort": [
{
"event_state.attempt_count": "desc"
}
]
}
}
}
CodePudding user response:
You can store the query string in a raw string constant, the parts that you need to replace can be fitted with formatting directives such a %s, %d, %v.
Pass the raw string constant to fmt.Sprintf along with the variables in the correct order.
CodePudding user response:
You might want to look at using the text/template module which is part of the go std library. https://pkg.go.dev/text/template
//The information you want to pass to your template
type Inventory struct {
Material string
Count uint
}
var outputBuff bytes.Buffer
// define your template here
queryTemplate := "{{.Count}} items are made of {{.Material}}"
queryParameters := Inventory{"wool", 17}
tmpl, err := template.New("test").Parse(queryTemplate)
if err != nil { panic(err) }
err = tmpl.Execute(&outputBuff, queryParameters)
if err != nil { panic(err) }
authoredQuery := outputBuff.String()