Hello John,
Question 1: If I wanted to get back all the documents so I could put them on a map, what is the key, it isnât âaggsâ is it?
You do not not need to have an aggs in your query but you will be limited by the number of docs a query can return (10000). Thatâs the reason why it is preferable to use aggs compared to simple query. This is what most of the vizs in Opensearch are doing (aggs).
By default in Opensearch, if you do not specify the size to be returned the value is 10.
If you want to get more, use size:
{
"sample": {
"index": "opensearch_dashboards_sample_data_ecommerce",
"size": 250,
"query": {
"bool": {
"must": [
"_DASHBOARD_CONTEXT_",
"_TIME_RANGE_[order_date]"
]
}
},
"aggs": {
"customer": {
"terms": {
"field": "customer_full_name.keyword",
"size": 5
}
}
}
}
}
By default you do not get the total number neither. In order to get the total number in the result, use track_total_hits:
{
"sample": {
"index": "opensearch_dashboards_sample_data_ecommerce",
"size": 250,
"track_total_hits" : true,
"query": {
"bool": {
"must": [
"_DASHBOARD_CONTEXT_",
"_TIME_RANGE_[order_date]"
]
}
},
"aggs": {
"customer": {
"terms": {
"field": "customer_full_name.keyword",
"size": 5
}
}
}
}
}
The result are then in the response field (this.response), you will get an object named sample with:
- an aggregations object called customer with an array containing aggs results: this.response.sample.aggregations.customer.buckets
- a hits object with the docs also within an array : this.response.sample.aggregations.hits.hits
Question 2: I guess what I am struggling with here is how to access the various track data that is being returned?
If you do not have more than 10000 docs in your index you can set the size in the query (if you have more this is still achievable but more complex and you will need to use Opensearch scroll searches and XHR request in Javascript).
{
âsampleâ: {
âindexâ: âtracksâ,
âqueryâ: {
"size": 10000,
âboolâ: {
âmustâ: [
]
}
},
âfieldsâ: [
âtrackIdâ,
âcorrelFactorâ
],
â_sourceâ: false
}
}
Then, in the JS part, you need to return the data from the response object. You will need to format the data depending on how you want them to be displayed. E.g., if you need to display the list of trackIds and trackFactors.
({
track: function() {
let data = this.response.sample.hits.hits; // Get the docs array into data var
let to_be_displayed = []; // Initialize the array used to display the data
data.forEach(item => to_be_displayed.push({ "myTrackId": item._source.trackId, "myCorrelFactor": item._source.correlFactor }) // Loop over all the data to build the array
return to_be_displayed; // This is returning the array of docs from the hit Object (hits is an array)
}
});
Finally in the template part:
{{#meta.track}}
{{myTrackId}} : {{myCorrelFactor}}
{{/meta.track}}
Hope it helps.
PS: Use the developer tools in your browser in order to see the structure of the objects you are manipulating (console tab by using console.log).
Lionel