Linus Norton
Linus Norton

Reputation: 557

Elasticsearch terms stats query not grouping correctly

I have a terms stats query very similar to this one:

Sum Query in Elasticsearch

However, my key_field is a date.

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 "-". What I received is actually grouped by ["2014", "01", "20", "21", "22"].

Why is it splitting my key?

Upvotes: 0

Views: 329

Answers (1)

Alex Brasetvik
Alex Brasetvik

Reputation: 11744

You probably have your key_field mapped with a string-type using the standard-analyzer.

That'll tokenize 2014-01-20 into 2014, 01, and 20.

You probably want to index your date as having type 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

#!/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
}
'

Upvotes: 1

Related Questions