Populate Google Chart with Search Data from Elasticsearch

erikdpratt

New Member
I'm just learning Elasticsearch and Javascript and I just started using Google Charts due to their ease of use.I'm attempting to render a Google Chart based on an Elasticsearch query. The chart does not render due to improperly formatted data. Here is my non-working code:\[code\] <html> <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://stackoverflow.com/questions/15801293/java/jquery-1.9.1.min.js""></script> <script type="text/javascript"> // Load the Visualization API and the piechart package. google.load('visualization', '1', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.setOnLoadCallback(drawChart); function drawChart() { var jsonData = http://stackoverflow.com/questions/15801293/$.ajax({ url:'http://localhost:9200/inventory/_search?pretty=true' , type: 'POST' , data : JSON.stringify( { "query" : { "match_all" : {} }, "facets" : { "tags" : { "terms" : { "field" : "qty_onhand", "size" : "10" } } } }), dataType:"json" async: false ,processData: false }).responseText; // Create our data table out of JSON data loaded from server. var data = http://stackoverflow.com/questions/15801293/new google.visualization.DataTable(jsonData); // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, {width: 400, height: 240}); } </script> </head> <body> <!--Div that will hold the pie chart--> <div id="chart_div"></div> </body></html>\[/code\]The issue I'm running into is that the data returned from the query is not the the "fields", but its the entire query summary as well.Is there a way to return this query while retaining only the fields? Or perhaps there is a way to query and format the data in a PHP file that I can then call in the chart? Google Charts site suggests that a PHP file can be created to load the query. This is from their site:\[code\]<?php // This is just an example of reading server side data and sending it to the client.// It reads a json formatted text file and outputs it.$string = file_get_contents("sampleData.json");echo $string;// Instead you can query your database and parse into JSON etc etc?>\[/code\]I'm most interested in the last comment.. how do I query Elasticsearch and return an acceptable JSON document? EX:\[code\]{"cols": [ {"id":"","label":"Topping","pattern":"","type":"string"}, {"id":"","label":"Slices","pattern":"","type":"number"} ],"rows": [ {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]}, {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]}, {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]}, {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]}, {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]} ]}\[/code\]
 
Back
Top