如何在python/django中从elasticsearch填充数据表
我已经试了一个星期了,但似乎还没有结束。我试图做的是在django应用程序中填充来自elasticsearch的 我曾经将datatables集成到我的应用程序中,并成功地用JSON格式的数组中的一些数据填充了一个表(这就是数据来自elasticsearch的方式) 现在,我的问题是:有没有人有一个使用elasticsearch提取数据的代码示例 到目前为止,我的代码是: mytable.html如何在python/django中从elasticsearch填充数据表,python,django,datatable,datatables,Python,Django,Datatable,Datatables,我已经试了一个星期了,但似乎还没有结束。我试图做的是在django应用程序中填充来自elasticsearch的 我曾经将datatables集成到我的应用程序中,并成功地用JSON格式的数组中的一些数据填充了一个表(这就是数据来自elasticsearch的方式) 现在,我的问题是:有没有人有一个使用elasticsearch提取数据的代码示例 到目前为止,我的代码是: mytable.html <div class="container"> <div cla
<div class="container">
<div class="row">
<div class="col-sm-8 col-lg-offset-1">
<table class="datatable table table-striped table-bordered hover" id="example" width="100%" cellspacing="0">
<thead>
<tr>
{% for column in columns %}
<th>{{ column }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
</tbody>
</table>
<script class="init">
$('#example').dataTable( {
"language": {
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Romanian.json"
},
searching: true,
ordering: true,
"data": {{ dataforcolumns|safe }},
"columns": [
{ "data": "Name" },
{ "data": "Date" },
{ "data": "Value" }
],
});
</script>
</div>
</div>
</div>
据我所知,elasticsearch和datatables还没有这样的实现。但是有一个会很好抱歉,对ElasticSearch不太熟悉,但是你不应该只从ElasticSearch发送一个JSON对象吗?甚至是XML?如果是这样的话,那么DataTables应该能够处理它。我希望做同样的事情……必须有一个中间人,负责接收elasticsearch生成的json响应,并将其映射到DataTables类型响应格式。然后围绕分页进行一些工作,很可能我也在寻找从弹性搜索到Mongodb的数据表迁移。在github下面出现了一个信息丰富的窗口。
class TablePrettyView(TemplateView):
template_name = 'mytable.html'
def __init__(self, **args):
self.args = args
if 'interval' not in args:
today = date.today()
week = timedelta(days=6)
args['interval'] = QueryInterval(today - week, today)
def get_query(self):
raise RuntimeError("You must implement the `get_query` method")
def datapoint(self, key, row):
if self.metric_name:
value = row[self.metric_name]['value']
else:
value = row['doc_count']
return key, value
def datapoint(self, key, row):
if self.metric_name:
value = row[self.metric_name]['value']
else:
value = row['doc_count']
return key, value
def get_rows(self, res):
def rows(obj, key_prefix, agg_name, *more_agg_names):
for row in obj[agg_name]['buckets']:
row_key = key_prefix + (row['key'],)
if more_agg_names:
for yv in rows(row, row_key, *more_agg_names):
yield yv
else:
yield self.datapoint(row_key, row)
return rows(res['aggregations'], (), *self.agg_names)
def html(self):
query = self.get_query()
res = try_cache(
cache_key([self.index, query]),
run_query, self.index, query,
)
rows = self.get_rows(res)
series = self.get_series(rows)
options = self.get_options(series)
context = Context({
'options': json.dumps(options),
})
def get(self, request, *args, **kwargs):
daterange = get_daterange(request.GET.get('daterange'))
context = {}
tablearray = [
{'Name': 'Pope', 'day 1': '16 august 2015', 'day 7': '76436757'},
{'Name': 'Alew', 'Date': '17 august 2015', 'Value': '76436757'},
]
columns = ['Name', 'Date', 'Value']
context['columns'] = columns
context['dataforcolumns'] = json.dumps(tablearray)
context['daterange'] = {'start': str(daterange[0]),
'end': str(daterange[1])}
return render(request,
self.template_name,
context)