php - 使用 Elasticsearch 中的搜索数据填充 Google 图表

标签 php javascript json elasticsearch google-visualization

我刚刚学习 Elasticsearch 和 Javascript,并且由于 Google Charts 的易用性,我刚刚开始使用它们。

我正在尝试根据 Elasticsearch 查询呈现 Google 图表。由于数据格式不正确,图表无法呈现。这是我的非工作代码:

    <html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="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 = $.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 = 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>

我遇到的问题是,从查询返回的数据不是“字段”,而是整个查询摘要。

有没有办法在仅保留字段的情况下返回此查询?或者也许有一种方法可以查询和格式化 PHP 文件中的数据,然后我可以在图表中调用它? Google Charts 站点建议可以创建一个 PHP 文件来加载查询。这是来自他们的网站:

<?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

?>

我对最后一条评论最感兴趣..如何查询 Elasticsearch 并返回可接受的 JSON 文档?例如:

{
"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}]}
  ]
}

最佳答案

你可以尝试一下:

var data = new google.visualization.DataTable(jsonData.hits.hits);

也就是说,如果您确实需要像最后描述的那样,我认为您必须自己处理它并将 Elasticsearch 响应格式转换为您的数据格式。

以下是我在 another project 中的处理方式.

if(result != null && result.hits != null && result.hits.hits != null && result.hits.hits.length > 0){
   for(var i=0; i <= result.hits.hits.length; i++){
       var hit = result.hits.hits[i];
       // transform your hit to ...
   }
}

关于php - 使用 Elasticsearch 中的搜索数据填充 Google 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15801293/

相关文章:

javascript - ng重复打印范围内的数字

javascript - Google map api 3 json - 创建动态中心点

c# - 如何使用 C#.NET 从 JSON 字符串填充通用列表?

php - 如何将逗号分隔的数据添加到数据库中

php - 哪个更好 : Dependency Injection+Registry or Dependency Injection or Global Registry?

php - MySQL 只插入两个字段

Javascript : Event listener defined inside function of object literal making the code dead

javascript - 如何在溢出隐藏父元素内显示绝对元素

javascript - 使用 D3.js 分组条形图的问题

php - 我看不到我使用 phpmyadmin 上的表单添加的数据