![](/img/trans.png)
[英]How to make Elasticsearch date_histogram facet work on nested items
[英]Date_histogram Elasticsearch facet can't find field
我正在使用date_histogram构面来查找基于纪元时间戳的结果。 结果显示在直方图上,日期在x轴上,事件计数在y轴上。 这是我不起作用的代码:
angular.module('controllers', [])
.controller('FacetsController', function($scope, $http) {
var payload = {
query: {
match: {
run_id: '9'
}
},
facets: {
date: {
date_histogram: {
field: 'event_timestamp',
factor: '1000',
interval: 'second'
}
}
}
}
如果我使用的是ISO8601格式的字段'@timestamp',它会起作用; 但是,我现在需要它来与Epoch时间戳一起使用。
这是我的Elasticsearch中的示例,可能会导致一些答案:
{"@version":"1",
"@timestamp":"2014-07-04T13:13:35.372Z","type":"automatic",
"installer_version":"0.3.0",
"log_type":"access.log","user_id":"1",
"event_timestamp":"1404479613","run_id":"9"}
},
运行此命令时,出现以下错误: POST 400 (Bad Request)
关于这里可能出什么问题的任何想法? 我不明白为什么我与使用两个不同的字段会有如此不同,因为唯一的区别是格式。 我尽了最大的努力研究,发现我应该使用“因子”,但这似乎并不能解决我的问题。 我可能在犯一个愚蠢的初学者错误!
您需要首先设置索引。 Elasticsearch擅长使用默认值,但无法确定所提供的值是时间戳,整数还是字符串。 因此,将相同的事情告诉Elasticsearch是您的工作。
让我举例说明。 让我们考虑以下文档是您要索引的内容:
{
"@version": "1",
"@timestamp": "2014-07-04T13:13:35.372Z",
"type": "automatic",
"installer_version": "0.3.0",
"log_type": "access.log",
"user_id": "1",
"event_timestamp": "1404474613",
"run_id": "9"
}
因此,最初您没有索引,而是通过发出HTTP请求来索引文档,如下所示:
POST /test/date_experiments
{
"@version": "1",
"@timestamp": "2014-07-04T13:13:35.372Z",
"type": "automatic",
"installer_version": "0.3.0",
"log_type": "access.log",
"user_id": "1",
"event_timestamp": "1404474613",
"run_id": "9"
}
这将创建一个称为test
的新索引和一个名为date_experiments
索引test
的新doc类型。
您可以通过以下方式检查此文档类型date_experiments
的映射:
GET /test/date_experiments/_mapping
结果中得到的是由Elasticsearch生成的自动生成的映射:
{
"test": {
"date_experiments": {
"properties": {
"@timestamp": {
"type": "date",
"format": "dateOptionalTime"
},
"@version": {
"type": "string"
},
"event_timestamp": {
"type": "string"
},
"installer_version": {
"type": "string"
},
"log_type": {
"type": "string"
},
"run_id": {
"type": "string"
},
"type": {
"type": "string"
},
"user_id": {
"type": "string"
}
}
}
}
}
请注意, event_timestamp
字段的类型设置为string
。 这就是为什么您的date_histogram
无法正常工作的原因。 还要注意, @timestamp
字段的类型已经是date
因为您以标准格式推送了日期,这使Elasticsearch容易识别出您打算在该字段中推送日期。
通过向/test/date_experiments
发送DELETE
请求来删除此映射,并从头开始。
这一次而不是先推送文档,而是根据需求进行映射,以便将event_timestamp
字段视为日期。
发出以下HTTP请求:
PUT /test/date_experiments/_mapping
{
"date_experiments": {
"properties": {
"@timestamp": {
"type": "date"
},
"@version": {
"type": "string"
},
"event_timestamp": {
"type": "date"
},
"installer_version": {
"type": "string"
},
"log_type": {
"type": "string"
},
"run_id": {
"type": "string"
},
"type": {
"type": "string"
},
"user_id": {
"type": "string"
}
}
}
}
注意,我已将event_timestamp
字段的类型更改为date
。 我没有指定格式,因为Elasticsearch善于理解一些标准格式,例如@timestamp
字段中您输入日期的情况。 在这种情况下,Elasticsearch将能够理解您正在尝试推送UNIX时间戳并在内部对其进行转换,以将其视为日期并允许对其进行所有日期操作。 您可以在映射中指定日期格式,以防万一您要推送的日期不是任何标准格式。
现在,您可以像以前一样开始为文档建立索引并开始运行日期查询和构面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.