Elasticsearch 如何将日期转换为 JSON 字符串表示形式?
How does Elasticsearch convert dates to JSON string representations?
我对 Elasticsearch 如何将日期转换为 JSON 字符串表示有疑问。我在这个例子中使用的是 1.4.2 版本。
首先,POST 一个包含名为 'postDate' 到 'myindex' 且类型为 'datetest' 的日期字段的简单文档:
curl -XPOST "http://localhost:9200/myindex/datetest/" -d'
{
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12"
}'
我们可以查看自动生成的 postDate 的字段映射。
curl -XGET "http://localhost:9200/myindex/_mapping/"
##postDate":{"type":"date","format":"dateOptionalTime"}
现在让我们POST另一个文件。这次 postDate 包含一个 UTC 偏移量。
curl -XPOST "http://localhost:9200/myindex/datetest/" -d'
{
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12-07:00"
}'
现在,让我们检索上面创建的文档:
curl -XGET 'http://localhost:9200/myindex/datetest/_search?q=Hello'
"hits": {
"total": 2,
"max_score": 0.11506981,
"hits": [
{
"_index": "myindex",
"_type": "datetest",
"_id": "AUw0TyJgqFHXxhSON3r8",
"_score": 0.11506981,
"_source": {
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12"
}
},
{
"_index": "myindex",
"_type": "datetest",
"_id": "AUw0VIQbqFHXxhSON3r-",
"_score": 0.095891505,
"_source": {
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12-07:00"
}
}
]
}
您看到每个返回的文档中的 postDate 格式都与 POSTed 完全一样。虽然这让我感到困惑。根据,
http://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html
"日期类型是一种映射到JSON字符串类型的特殊类型。它
遵循可以显式设置的特定格式。 所有日期都是
世界标准时间。在内部,一个日期映射到一个长型数字,加上
从字符串到长和从长到字符串的解析阶段。”(强调我的)
我的两个问题是:
1) 因为,"internally, a date maps to a number type long",如何或在哪里
elasticsearch 存储关于日期如何的元信息
POST编辑?
2) 我可以为日期指定 输出格式 吗?
不管他们是如何 POSTed 的。也就是说,我可以控制“长
到字符串”的转换与日期的输入方式无关?
源按原样存储,当您检索它时,Elasticsearch returns 您发布的同一文档。
它 "stores" long 键入索引,并使用它在内部执行查询,但您看不到它。
不要混淆来源和索引。
就像索引一个字符串 "Erik Iverson" 一样,它会在索引中保存 2 个术语 [erik] [iverson],但是当您检索文档时,您会得到原始字符串,因为源不是改变了
我对 Elasticsearch 如何将日期转换为 JSON 字符串表示有疑问。我在这个例子中使用的是 1.4.2 版本。
首先,POST 一个包含名为 'postDate' 到 'myindex' 且类型为 'datetest' 的日期字段的简单文档:
curl -XPOST "http://localhost:9200/myindex/datetest/" -d'
{
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12"
}'
我们可以查看自动生成的 postDate 的字段映射。
curl -XGET "http://localhost:9200/myindex/_mapping/"
##postDate":{"type":"date","format":"dateOptionalTime"}
现在让我们POST另一个文件。这次 postDate 包含一个 UTC 偏移量。
curl -XPOST "http://localhost:9200/myindex/datetest/" -d'
{
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12-07:00"
}'
现在,让我们检索上面创建的文档:
curl -XGET 'http://localhost:9200/myindex/datetest/_search?q=Hello'
"hits": {
"total": 2,
"max_score": 0.11506981,
"hits": [
{
"_index": "myindex",
"_type": "datetest",
"_id": "AUw0TyJgqFHXxhSON3r8",
"_score": 0.11506981,
"_source": {
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12"
}
},
{
"_index": "myindex",
"_type": "datetest",
"_id": "AUw0VIQbqFHXxhSON3r-",
"_score": 0.095891505,
"_source": {
"content": "Hello World!",
"postDate": "2009-11-15T14:12:12-07:00"
}
}
]
}
您看到每个返回的文档中的 postDate 格式都与 POSTed 完全一样。虽然这让我感到困惑。根据,
http://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html
"日期类型是一种映射到JSON字符串类型的特殊类型。它 遵循可以显式设置的特定格式。 所有日期都是 世界标准时间。在内部,一个日期映射到一个长型数字,加上 从字符串到长和从长到字符串的解析阶段。”(强调我的)
我的两个问题是:
1) 因为,"internally, a date maps to a number type long",如何或在哪里 elasticsearch 存储关于日期如何的元信息 POST编辑?
2) 我可以为日期指定 输出格式 吗? 不管他们是如何 POSTed 的。也就是说,我可以控制“长 到字符串”的转换与日期的输入方式无关?
源按原样存储,当您检索它时,Elasticsearch returns 您发布的同一文档。
它 "stores" long 键入索引,并使用它在内部执行查询,但您看不到它。
不要混淆来源和索引。
就像索引一个字符串 "Erik Iverson" 一样,它会在索引中保存 2 个术语 [erik] [iverson],但是当您检索文档时,您会得到原始字符串,因为源不是改变了