I want to implement the following Elasticsearch query using Golang.
{
"query": {
"bool": {
"must": {
"range": { "sales_start_date": { "gte": "2018-07-01T00:00:00.000Z" } }
},
"filter": {
"terms": { "item_holder_id": [2, 7, 9] }
}
}
}
}
'
My current code is the following (I am using http://github.com/olivere/elastic library).
query := elastic.NewBoolQuery()
query = query.Must(elastic.NewRangeQuery("sales_start_date").Gte(date).TimeZone("+09:00"))
query = query.Filter(elastic.NewTermsQuery("item_holder_id", ihids))
with ihids being an []int
.
When I execute this code I got the error
elastic: Error 400 (Bad Request): [terms] unknown token [END_ARRAY] after [item_holder_id] [type=parsing_exception]
After some research I found out that I need to write this like this
elastic.NewTermsQuery("item_holder_id", "1", "2")
but being new to Golang I guess I am missing some of the basic concepts. How can I pass an array of values like this?
Thank you for any information that can help me!
in order to complete the @Ullaakut answer:
// Convert type []int to type []interface{}
status := make([]interface{}, len(ihids))
for index, value := range ihids {
status[index] = value
}
// Ullaakut aswer
query := elastic.NewBoolQuery()
query = query.Must(elastic.NewRangeQuery("sales_start_date").Gte(date).TimeZone("+09:00"))
query = query.Filter(elastic.NewTermsQuery("item_holder_id", status...))
I had the same issue and I solved on this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With