How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Bhavik Joshi Your Own Question

Bhavik Joshi
Bhavik Joshi, Computer Software Engineer
Category: Programming
Satisfied Customers: 276
Experience:  Product Developer at Computer Science Corporation
94617175
Type Your Programming Question Here...
Bhavik Joshi is online now
A new question is answered every 9 seconds

I have a question regarding D3.JS and Elasticsearch. I am

Customer Question

I have a question regarding D3.JS and Elasticsearch. I am able to load simple queries into D3 graphs, but I fail to load when there are sub aggregations because I'm not sure how to reference them. For example:
I have the following Elasticsearch query:
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*",
"analyze_wildcard": true
}
},
"filter": {
"bool": {
"must": [
{
"query": {
"query_string": {
"analyze_wildcard": true,
"query": my_query_name
}
}
},
{
"range": {
"@timestamp": {
"to": "now",
"from": "now-1M/M"
}
}
}
],
"must_not": []
}
}
}
},
"size": 0,
"aggs": {
"my_summary": {
"terms": {
"field": "my_field1",
"size": 10,
"order": {
"cc": "desc"
}
},
"aggs": {
"cc": {
"sum": {
"field": "my_field2"
}
}
}
}
}
}
This query successfully returns back a JSON object that I then place into the D3.JS graph via the following JS code:
var load_data = myObject.my_summary.buckets.map(function(d) {
return {
letter: d.key,
frequency: d.doc_count
}
});
The problem occurs when a response is formatted like this:
"buckets": [ { "1": { "value": 5975 }, "key": "XXXXXXXXXXXX", "doc_count": 5376 }
Really what I need is the same "key" field from the object above the frequency to be the value of that data above instead of the "doc_count"
I attempted to reference this in different ways such as myObject.my_summary.cc.buckets etc, but had no luck.
Let me know if further details are needed.
Thanks!
Submitted: 5 months ago.
Category: Programming
Customer: replied 5 months ago.
I thought it might be helpful to add the entire D3.js script for loading the data...This is an example of the bar graph:var load_data= myObject.aggregations.my_summary.buckets.map(function(d) {
return {
letter: d.key,
frequency: d.doc_count
}
});var margin = {top: 20, right: 30, bottom: 30, left: 80},
width = 1100 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);var y = d3.scale.linear()
.range([height, 0]);var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, ""); //% CHanges to percentage etcvar svg = d3.select("#barchart_3d").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");x.domain(load_data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(load_data, function(d) { return d.frequency; })]);svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("");svg.selectAll(".bar2")
.data(load_data)
.enter().append("rect")
.attr("class", "bar2")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); });
function type(d) {
d.frequency = +d.frequency;
return d;
}
Expert:  Bhavik Joshi replied 5 months ago.

Hi Welcome to JustAnswer

Expert:  Bhavik Joshi replied 5 months ago.

Can you share its CSS & other code as well ,I would like to run it from my end and see how it is working. I did't get why you are looking for a frequency value of a previous key. Can't we modify our data in a way so that we can take key & its current frequency ?

Customer: replied 5 months ago.
When I get in to the office I can send you the CSS portion of the code. It probably won't work unless you have an elasticsearch client running though.I'm not actually looking for the frequency of the previous key, it's that the aggregations have sub aggregations I cannot access. So the JSON object looks like:"buckets": [
{ "1":
{ "value": 5975
},
"key": "XXXXXXXXXXXX", "doc_count": 5376 }The "value" is in technically a different bucket then "key" and "doc_count"I am able to get to key and doc_count by the code:var load_data= myObject.aggregations.my_summary.buckets.map(function(d) {
return {
letter: d.key,
frequency: d.doc_count
}
});But, I don't know how to access the "value" field.
Customer: replied 5 months ago.
A lot of this code I unfortunately can't post. I would have to strip the confidential identifiers. Any ideas?
Expert:  Bhavik Joshi replied 5 months ago.

Hi Here in this case to access the value of object bucket you will have to use rollup function.

here is the perfect example which matches with our requirement. :- http://learnjsdata.com/group_data.html

Please let me know if you have any questions.

Customer: replied 5 months ago.
I think you may be correct, but I'm still having trouble writing it. This is what I have:var myDataCount = d3.nest()
.key(function(d) { return d.key; })
.rollup(function(v) { return v.value; })
.entries(myObject);
console.log(JSON.stringify(myDataCount));This returns an empty array though. I clearly have it incorrect.Thanks
Expert:  Bhavik Joshi replied 5 months ago.

No, Like this it won't work. How your are getting value of filed value ? "value": 5975 how your are calculating this and what does this value 5975 means. Is this sum or Aggregate of frequency ?

Customer: replied 5 months ago.
Value 5975 is a Sum. The "key" field is the text I want to display and then "doc_count" I don't need at all its just in the query for organization purposes.So I would want to show the "key" field then the "summed value" field in the graph.
Expert:  Bhavik Joshi replied 5 months ago.

Got you !. I will code that for you.

Expert:  Bhavik Joshi replied 5 months ago.

var load_data = d3.nest()
.key(function(d) { return d.key; })
.rollup(function(v) { return {
total: d3.sum(v, function(d) { return d.doc_count; }),
}; })
.entries(expenses);
console.log(JSON.stringify(load_data));

Expert:  Bhavik Joshi replied 5 months ago.

Did this worked ?

Expert:  Bhavik Joshi replied 5 months ago.

Please provide me your valuable ratings on this.