Elasticsearch Retrieve token_count standard value from search

Hi!

I am trying to add a new field into the ElasticSearch index to analyze and store the number of words a text has.

{
"settings": {
    "analysis": {
        "analyzer": {
            "character_analyzer": {
                "type": "custom",
                "tokenizer": "character_tokenizer"
            }
        },
        "tokenizer": {
            "character_tokenizer": {
                "type": "ngram",
                "min_gram": 1,
                "max_gram": 1
            }
        }
    }
},
"mappings": {
    "_doc": {
        "dynamic": "strict",
        "properties": {
            "name": {
                "type": "text",
                        "fields": {
                            "words_count": {
                                "type": "token_count",
                                "analyzer": "standard",
                                "store": true
                            }

Performing a query I am able to retrieve the documents which have the total words count I request in query:

{   
"size": 1,
"query": {
    "bool" : {
        "must" : {
            "range" : {
                "name.words_count": {
                   "gt": 3
                }
            }
        }
    }
}

But I would like to retrieve this value on _source results. Is it possible? Am I missing something?

Thanks in advance!

This is not part of the _source, as it was not part of the original JSON. You could however retrieve this as a docvalue field or just enable the store setting in the mapping and the use docvalue_fields or "docvalue_fields": ["name.length"] or "stored_fields": ["name.length"] in the query to retrieve the value.

--Alex

Thank you Alexander for you answer. It worked as a charm for me.
I have another related question regarding this if you could help me.

I was trying to understand how much space this new stored field could occupy on elastic search. Is there any way to get this metrics through the elastic search?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.