[英]Elasticsearch terms stats query not grouping correctly
I have a terms stats query very similar to this one: 我有一个术语统计查询与此非常相似:
Sum Query in Elasticsearch Elasticsearch中的总和查询
However, my key_field
is a date. 但是,我的key_field
是一个日期。
I was expecting to receive results grouped by the full key_field value ["2014-01-20", "2014-01-21", "2014-01-22"] but it appears to be splitting the key field when it encounters a "-". 我希望收到按完整key_field值[“ 2014-01-20”,“ 2014-01-21”,“ 2014-01-22”]分组的结果,但是当遇到“ - ”。 What I received is actually grouped by ["2014", "01", "20", "21", "22"]. 我收到的实际上是按[“ 2014”,“ 01”,“ 20”,“ 21”,“ 22”]分组的。
Why is it splitting my key? 为什么要拆分我的钥匙?
You probably have your key_field
mapped with a string
-type using the standard
-analyzer. 您可能使用standard
-analyzer将key_field
映射为string
-type。
That'll tokenize 2014-01-20
into 2014
, 01
, and 20
. 这将令牌化2014-01-20
到2014
, 01
,和20
。
You probably want to index your date as having type date
. 您可能希望将日期编入类型为date
索引。 You can also have it as a string without analyzing it. 您也可以将其作为字符串而不进行分析。
Here's a runnable example you can play with: https://www.found.no/play/gist/5eb6b8d176e1cc72c9b8 这是一个可运行的示例,您可以使用: https : //www.found.no/play/gist/5eb6b8d176e1cc72c9b8
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Create indexes
curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"settings": {},
"mappings": {
"type": {
"properties": {
"date_as_a_string": {
"type": "string"
},
"date_as_nonanalyzed_string": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"type"}}
{"date":"2014-01-01T00:00:00.000Z","date_as_a_string":"2014-01-01T00:00:00.000Z","date_as_nonanalyzed_string":"2014-01-01T00:00:00.000Z","x":42}
'
# Do searches
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"facets": {
"date": {
"terms_stats": {
"key_field": "date",
"value_field": "x"
}
},
"date_as_a_string": {
"terms_stats": {
"key_field": "date_as_a_string",
"value_field": "x"
}
},
"date_as_nonanalyzed_string": {
"terms_stats": {
"key_field": "date_as_nonanalyzed_string",
"value_field": "x"
}
}
},
"size": 0
}
'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.