I am currently doing a post query in JavaScript which returns a resposne. I am able to store this response in a variable.
xhr = new XMLHttpRequest();
var url = "http://localhost:9200/test/_search?=pretty=true";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
var data = JSON.stringify(myQuery);
xhr.send(data);
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log(xhr.response)
}
}
}
Console Output
{
"took" : 33,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 54,
"max_score" : 4.8836236,
"hits" : [ {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAzviQ3keyPu6fdK",
"_score" : 4.8836236,
"_source" : {
"message" : "o232870047\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:17.301Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o232870047",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAr_iQ3keyPu6fWd",
"_score" : 4.8795,
"_source" : {
"message" : "o231380011\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:16.699Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o231380011",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAzviQ3keyPu6fdL",
"_score" : 4.8795,
"_source" : {
"message" : "o232870052\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:17.302Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o232870052",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAvliQ3keyPu6fZJ",
"_score" : 4.871201,
"_source" : {
"message" : "o232230020\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:16.845Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o232230020",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAzviQ3keyPu6fdN",
"_score" : 4.871201,
"_source" : {
"message" : "o232870054\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:17.302Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o232870054",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAr_iQ3keyPu6fW2",
"_score" : 4.7276993,
"_source" : {
"message" : "o231470025\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:16.721Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o231470025",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAvliQ3keyPu6fYe",
"_score" : 4.7276993,
"_source" : {
"message" : "o232100007\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:16.810Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o232100007",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbBC8iQ3keyPu6for",
"_score" : 4.7276993,
"_source" : {
"message" : "o234410029\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:18.334Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o234410029",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAr_iQ3keyPu6fXg",
"_score" : 4.327994,
"_source" : {
"message" : "o231920008\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:16.751Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"host" : "ATGDEV11",
"OrderNumber" : "o231920008",
"ProductName" : "20033897\r"
}
}, {
"_index" : "main12",
"_type" : "logs",
"_id" : "AVnfbAxdiQ3keyPu6fa3",
"_score" : 4.327994,
"_source" : {
"message" : "o232520053\t20033897\r",
"@version" : "1",
"@timestamp" : "2017-01-27T10:15:17.124Z",
"path" : "C:\\utils\\Elk\\logstash\\bin\\export.log",
"OrderNumber" : "o232520053",
"ProductName" : "20033897\r"
}
} ]
},
"aggregations" : {
"movies_like_terminator" : {
"doc_count" : 54,
"buckets" : [ {
"key" : "20033894",
"doc_count" : 54,
"score" : 43.870370370370374,
"bg_count" : 54
}, {
"key" : "20019702",
"doc_count" : 14,
"score" : 8.788675506782502,
"bg_count" : 18
}, {
"key" : "20178852",
"doc_count" : 5,
"score" : 3.3696273433927755,
"bg_count" : 6
}, {
"key" : "20151147",
"doc_count" : 4,
"score" : 3.249657064471879,
"bg_count" : 4
}, {
"key" : "20109408",
"doc_count" : 7,
"score" : 3.002347789384826,
"bg_count" : 13
}, {
"key" : "6001009817444",
"doc_count" : 5,
"score" : 2.875024495394866,
"bg_count" : 7
}, {
"key" : "20111529",
"doc_count" : 3,
"score" : 2.4372427983539096,
"bg_count" : 3
}, {
"key" : "200100012",
"doc_count" : 3,
"score" : 2.4372427983539096,
"bg_count" : 3
}, {
"key" : "2006667281",
"doc_count" : 3,
"score" : 2.4372427983539096,
"bg_count" : 3
}, {
"key" : "20049703236",
"doc_count" : 3,
"score" : 2.4372427983539096,
"bg_count" : 3
} ]
}
}
}
If you scroll down to the bottom of this response I would like to extract the "key" from
"aggregations" : {
"movies_like_terminator" : {
"doc_count" : 54,
"buckets" : [ {
"key" : "20033894",
"doc_count" : 54,
"score" : 43.870370370370374,
"bg_count" : 54
}, {
"key" : "20019702",
"doc_count" : 14,
"score" : 8.788675506782502,
"bg_count" : 18
Please advise on how should I go about doing this.
How do I get hold of this object?
Should I use a loop to iterate over the object?