# Return term frequency of a single field

**URL:** <https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546>\
**Category:** Elasticsearch\
**Created:** [November 4, 2012, 10:38am UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546 "2012-11-04T10:38:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kennedy\_Idialu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kennedy_idialu/32/2532_2.png) [@Kennedy\_Idialu](https://discuss.elastic.co/u/Kennedy_Idialu)\
**Post date:** [November 4, 2012, 10:38am UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546/1 "2012-11-04T10:38:38Z")

</div>

Hi,

I have being trying to use facet to get the term frequency of a field. My  
query returns just one hit, so I would like to have the facet return the  
terms that have the most frequency in a particular field.

My mapping:

{  
"mappings":{  
"document":{  
"properties":{  
"tags":{  
"type":"object",  
"properties":{  
"title":{  
"fields":{  
"partial":{  
"search\_analyzer":"main",  
"index\_analyzer":"partial",  
"type":"string",  
"index" : "analyzed"  
}  
"title":{  
"type":"string",  
"analyzer":"main",  
"index" : "analyzed"  
}  
},  
"type":"multi\_field"  
}  
}  
}  
}  
}  
},

```
"settings":{
    "analysis":{
        "filter":{
            "name_ngrams":{
                "side":"front",
                "max_gram":50,
                "min_gram":2,
                "type":"edgeNGram"
            }
        },

        "analyzer":{
            "main":{
                "filter": ["standard", "lowercase", "asciifolding"],
                "type": "custom",
                "tokenizer": "standard"
            },
            "partial":{

```

"filter":["standard","lowercase","asciifolding","name\_ngrams"],  
"type": "custom",  
"tokenizer": "standard"  
}  
}  
}  
}

}

Test data:

curl -XPUT localhost:9200/testindex/document -d '{"tags": {"title": "people  
also kill people"}}'

Query:  
curl -XGET 'localhost:9200/testindex/document/\_search?pretty=1' -d '  
{  
"query":  
{  
"term": { "tags.title": "people" }  
},  
"facets": {  
"popular\_tags": { "terms": {"field": "tags.title"}}  
}  
}'

This result

"hits" : {  
"total" : 1,  
"max\_score" : 0.99381393,  
"hits" : [ {  
"\_index" : "testindex",  
"\_type" : "document",  
"\_id" : "uI5k0wggR9KAvG9o7S7L2g",  
"\_score" : 0.99381393, "\_source" : {"tags": {"title": "people also  
kill people"}}  
} ]  
},  
"facets" : {  
"popular\_tags" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 3,  
"other" : 0,  
"terms" : [ {  
"term" : "people",  
"count" : 1 _// I expect this to be 2_  
}, {  
"term" : "kill",  
"count" : 1  
}, {  
"term" : "also",  
"count" : 1  
} ]  
}  
}

The above result is not what I want. I want to have the frequency count be  
2

"hits" : {  
"total" : 1,  
"max\_score" : 0.99381393,  
"hits" : [ {  
"\_index" : "testindex",  
"\_type" : "document",  
"\_id" : "uI5k0wggR9KAvG9o7S7L2g",  
"\_score" : 0.99381393, "\_source" : {"tags": {"title": "people also  
kill people"}}  
} ]  
},  
"facets" : {  
"popular\_tags" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 3,  
"other" : 0,  
"terms" : [ {  
"term" : "people",  
"count" : 2  
}, {  
"term" : "kill",  
"count" : 1  
}, {  
"term" : "also",  
"count" : 1  
} ]  
}  
}

How do I achieve this? Is facet the wrong way to go?

--

---

<div class="post-metadata">

**Author:** ![Kennedy\_Idialu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kennedy_idialu/32/2532_2.png) [@Kennedy\_Idialu](https://discuss.elastic.co/u/Kennedy_Idialu)\
**Post date:** [November 4, 2012, 4:03pm UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546/2 "2012-11-04T16:03:08Z")

</div>

Anybody?

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 4, 2012, 9:32pm UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546/3 "2012-11-04T21:32:43Z")

</div>

IMHO, facets are computed on index not with source documents.

AFAIK, TermFacet does not use the TermFrequency in each doc but only  
computes the number of docs that contains at least your term once, not the  
frequency.

I don’t see a way to get what you are looking for.

Hey ES Gurus ! Any idea? A script?

David.

De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com) [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)]  
De la part de Kennedy Idialu  
Envoyé : dimanche 4 novembre 2012 11:39  
À : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
Objet : Return term frequency of a single field

Hi,

I have being trying to use facet to get the term frequency of a field. My  
query returns just one hit, so I would like to have the facet return the  
terms that have the most frequency in a particular field.

My mapping:

{

```
"mappings":{

    "document":{

        "properties":{

            "tags":{

                "type":"object",

                "properties":{

                    "title":{

                        "fields":{

                            "partial":{

                                "search_analyzer":"main",

                                "index_analyzer":"partial",

                                "type":"string",

                                "index" : "analyzed"

                            }

                            "title":{

                                "type":"string",

                                "analyzer":"main",

                                "index" : "analyzed"

                            }

                        },

                        "type":"multi_field"

                    }

                }

            }

        }

    }

},

"settings":{

    "analysis":{

        "filter":{

            "name_ngrams":{

                "side":"front",

                "max_gram":50,

                "min_gram":2,

                "type":"edgeNGram"

            }

        },

        "analyzer":{

            "main":{

                "filter": ["standard", "lowercase", "asciifolding"],

                "type": "custom",

                "tokenizer": "standard"

            },

            "partial":{

```

"filter":["standard","lowercase","asciifolding","name\_ngrams"],

```
                "type": "custom",

                "tokenizer": "standard"

            }

        }

    }

}

```

}

Test data:

curl -XPUT localhost:9200/testindex/document -d '{"tags": {"title": "people  
also kill people"}}'

Query:

curl -XGET 'localhost:9200/testindex/document/\_search?pretty=1' -d '

{

"query":

```
 {

    "term": { "tags.title": "people" }

 },

"facets": {

  "popular_tags": { "terms": {"field": "tags.title"}}

```

}

}'

This result

"hits" : {

```
"total" : 1,

"max_score" : 0.99381393,

"hits" : [ {

  "_index" : "testindex",

  "_type" : "document",

  "_id" : "uI5k0wggR9KAvG9o7S7L2g",

  "_score" : 0.99381393, "_source" : {"tags": {"title": "people also

```

kill people"}}

```
} ]

```

},

"facets" : {

```
"popular_tags" : {

  "_type" : "terms",

  "missing" : 0,

  "total" : 3,

  "other" : 0,

  "terms" : [ {

    "term" : "people",

    "count" : 1 // I expect this to be 2

  }, {

    "term" : "kill",

    "count" : 1

  }, {

    "term" : "also",

    "count" : 1

  } ]

}

```

}

The above result is not what I want. I want to have the frequency count be 2

"hits" : {

```
"total" : 1,

"max_score" : 0.99381393,

"hits" : [ {

  "_index" : "testindex",

  "_type" : "document",

  "_id" : "uI5k0wggR9KAvG9o7S7L2g",

  "_score" : 0.99381393, "_source" : {"tags": {"title": "people also

```

kill people"}}

```
} ]

```

},

"facets" : {

```
"popular_tags" : {

  "_type" : "terms",

  "missing" : 0,

  "total" : 3,

  "other" : 0,

  "terms" : [ {

    "term" : "people",

    "count" : 2            

  }, {

    "term" : "kill",

    "count" : 1

  }, {

    "term" : "also",

    "count" : 1

  } ]

}

```

}

How do I achieve this? Is facet the wrong way to go?

--

--

---

<div class="post-metadata">

**Author:** ![Kennedy\_Idialu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kennedy_idialu/32/2532_2.png) [@Kennedy\_Idialu](https://discuss.elastic.co/u/Kennedy_Idialu)\
**Post date:** [November 4, 2012, 9:53pm UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546/4 "2012-11-04T21:53:07Z")

</div>

I guess I will have to use the Analyze API then. thanks

On Sunday, 4 November 2012 22:32:56 UTC+1, David Pilato wrote:

> IMHO, facets are computed on index not with source documents.
> 
> AFAIK, TermFacet does not use the TermFrequency in each doc but only  
> computes the number of docs that contains at least your term once, not the  
> frequency.
> 
> I don’t see a way to get what you are looking for.
> 
> Hey ES Gurus ! Any idea? A script?
> 
> David.
> 
> _De :_ [elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\> [mailto:  
> [elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\>] _De la part de_ Kennedy Idialu  
> _Envoyé :_ dimanche 4 novembre 2012 11:39  
> _À :_ [elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\>  
> _Objet :_ Return term frequency of a single field
> 
> Hi,
> 
> I have being trying to use facet to get the term frequency of a field. My  
> query returns just one hit, so I would like to have the facet return the  
> terms that have the most frequency in a particular field.
> 
> My mapping:
> 
> {
> 
> ```
> "mappings":{
> 
> "document":{
> 
> "properties":{
> 
> "tags":{
> 
> "type":"object",
> 
> "properties":{
> 
> "title":{
> 
> "fields":{
> 
> "partial":{
> 
> "search_analyzer":"main",
> 
> "index_analyzer":"partial",
> 
> "type":"string",
> 
> "index" : "analyzed"
> 
> }
> 
> "title":{
> 
> "type":"string",
> 
> "analyzer":"main",
> 
> "index" : "analyzed"
> 
> }
> 
> },
> 
> "type":"multi_field"
> 
> }
> 
> }
> 
> }
> 
> }
> 
> }
> 
> },
> 
> "settings":{
> 
> "analysis":{
> 
> "filter":{
> 
> "name_ngrams":{
> 
> "side":"front",
> 
> "max_gram":50,
> 
> "min_gram":2,
> 
> "type":"edgeNGram"
> 
> }
> 
> },
> 
> "analyzer":{
> 
> "main":{
> 
> "filter": ["standard", "lowercase", "asciifolding"],
> 
> "type": "custom",
> 
> "tokenizer": "standard"
> 
> },
> 
> "partial":{
> 
> ```
> 
> "filter":["standard","lowercase","asciifolding","name\_ngrams"],
> 
> ```
> "type": "custom",
> 
> "tokenizer": "standard"
> 
> }
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> Test data:
> 
> curl -XPUT localhost:9200/testindex/document -d '{"tags": {"title":  
> "people also kill people"}}'
> 
> Query:
> 
> curl -XGET 'localhost:9200/testindex/document/\_search?pretty=1' -d '
> 
> {
> 
> "query":
> 
> ```
> {
> 
> "term": { "tags.title": "people" }
> 
> },
> 
> "facets": {
> 
> "popular_tags": { "terms": {"field": "tags.title"}}
> 
> ```
> 
> }
> 
> }'
> 
> This result
> 
> "hits" : {
> 
> ```
> "total" : 1,
> 
> "max_score" : 0.99381393,
> 
> "hits" : [ {
> 
> "_index" : "testindex",
> 
> "_type" : "document",
> 
> "_id" : "uI5k0wggR9KAvG9o7S7L2g",
> 
> "_score" : 0.99381393, "_source" : {"tags": {"title": "people also 
> 
> ```
> 
> kill people"}}
> 
> ```
> } ]
> 
> ```
> 
> },
> 
> "facets" : {
> 
> ```
> "popular_tags" : {
> 
> "_type" : "terms",
> 
> "missing" : 0,
> 
> "total" : 3,
> 
> "other" : 0,
> 
> "terms" : [ {
> 
> "term" : "people",
> 
> "count" : 1 *// I expect this to be 2*
> 
> }, {
> 
> "term" : "kill",
> 
> "count" : 1
> 
> }, {
> 
> "term" : "also",
> 
> "count" : 1
> 
> } ]
> 
> }
> 
> ```
> 
> }
> 
> The above result is not what I want. I want to have the frequency count be  
> 2
> 
> "hits" : {
> 
> ```
> "total" : 1,
> 
> "max_score" : 0.99381393,
> 
> "hits" : [ {
> 
> "_index" : "testindex",
> 
> "_type" : "document",
> 
> "_id" : "uI5k0wggR9KAvG9o7S7L2g",
> 
> "_score" : 0.99381393, "_source" : {"tags": {"title": "people also 
> 
> ```
> 
> kill people"}}
> 
> ```
> } ]
> 
> ```
> 
> },
> 
> "facets" : {
> 
> ```
> "popular_tags" : {
> 
> "_type" : "terms",
> 
> "missing" : 0,
> 
> "total" : 3,
> 
> "other" : 0,
> 
> "terms" : [ {
> 
> "term" : "people",
> 
> "count" : 2            
> 
> }, {
> 
> "term" : "kill",
> 
> "count" : 1
> 
> }, {
> 
> "term" : "also",
> 
> "count" : 1
> 
> } ]
> 
> }
> 
> ```
> 
> }
> 
> How do I achieve this? Is facet the wrong way to go?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [November 5, 2012, 2:22am UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546/5 "2012-11-05T02:22:57Z")

</div>

Jörg's termlist plugin could help

> **[GitHub - jprante/elasticsearch-index-termlist: Elasticsearch Index Termlist](https://github.com/jprante/elasticsearch-index-termlist)**
>
> Elasticsearch Index Termlist. Contribute to jprante/elasticsearch-index-termlist development by creating an account on GitHub.

--  
Ivan

On Sun, Nov 4, 2012 at 1:32 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> IMHO, facets are computed on index not with source documents.\*\*\*\*
> 
> AFAIK, TermFacet does not use the TermFrequency in each doc but only  
> computes the number of docs that contains at least your term once, not the  
> frequency.\*\*\*\*
> 
> * * *
> 
> I don’t see a way to get what you are looking for.\*\*\*\*
> 
> * * *
> 
> Hey ES Gurus ! Any idea? A script?\*\*\*\*
> 
> * * *
> 
> * * *
> 
> David.\*\*\*\*
> 
> * * *
> 
> * * *
> 
> _De :_ [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com) [mailto:  
> [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] _De la part de_ Kennedy Idialu  
> _Envoyé :_ dimanche 4 novembre 2012 11:39  
> _À :_ [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
> _Objet :_ Return term frequency of a single field\*\*\*\*
> 
> * * *
> 
> Hi,\*\*\*\*
> 
> * * *
> 
> I have being trying to use facet to get the term frequency of a field. My  
> query returns just one hit, so I would like to have the facet return the  
> terms that have the most frequency in a particular field.\*\*\*\*
> 
> * * *
> 
> My mapping:\*\*\*\*
> 
> * * *
> 
> {\*\*\*\*
> 
> ```
> "mappings":{ ****
> 
> "document":{ ****
> 
> "properties":{ ****
> 
> "tags":{ ****
> 
> "type":"object", ****
> 
> "properties":{ ****
> 
> "title":{ ****
> 
> "fields":{ ****
> 
> "partial":{ ****
> 
> "search_analyzer":"main", ****
> 
> "index_analyzer":"partial", ****
> 
> "type":"string", ****
> 
> "index" : "analyzed" ****
> 
> } ****
> 
> "title":{ ****
> 
> "type":"string", ****
> 
> "analyzer":"main", ****
> 
> "index" : "analyzed" ****
> 
> } ****
> 
> }, ****
> 
> "type":"multi_field" ****
> 
> } ****
> 
> } ****
> 
> } ****
> 
> } ****
> 
> } ****
> 
> }, ****
> 
> ```
> 
> * * *
> 
> ```
> "settings":{ ****
> 
> "analysis":{ ****
> 
> "filter":{ ****
> 
> "name_ngrams":{ ****
> 
> "side":"front", ****
> 
> "max_gram":50, ****
> 
> "min_gram":2, ****
> 
> "type":"edgeNGram" ****
> 
> } ****
> 
> }, ****
> 
> ```
> 
> * * *
> 
> ```
> "analyzer":{ ****
> 
> "main":{ ****
> 
> "filter": ["standard", "lowercase", "asciifolding"],**
> 
> ```
> 
> \*\*
> 
> ```
> "type": "custom", ****
> 
> "tokenizer": "standard" ****
> 
> }, ****
> 
> "partial":{ ****
> 
> ```
> 
> "filter":["standard","lowercase","asciifolding","name\_ngrams"],\*\*\*\*
> 
> ```
> "type": "custom", ****
> 
> "tokenizer": "standard" ****
> 
> } ****
> 
> } ****
> 
> } ****
> 
> } ****
> 
> ```
> 
> * * *
> 
> }\*\*\*\*
> 
> * * *
> 
> Test data:\*\*\*\*
> 
> * * *
> 
> curl -XPUT localhost:9200/testindex/document -d '{"tags": {"title":  
> "people also kill people"}}'\*\*\*\*
> 
> Query:\*\*\*\*
> 
> curl -XGET 'localhost:9200/testindex/document/\_search?pretty=1' -d '\*\*\*\*
> 
> {\*\*\*\*
> 
> "query":\*\*\*\*
> 
> ```
> { ****
> 
> "term": { "tags.title": "people" } ****
> 
> }, ****
> 
> "facets": { ****
> 
> "popular_tags": { "terms": {"field": "tags.title"}} ****
> 
> ```
> 
> }\*\*\*\*
> 
> }'\*\*\*\*
> 
> * * *
> 
> * * *
> 
> This result\*\*\*\*
> 
> "hits" : {\*\*\*\*
> 
> ```
> "total" : 1, ****
> 
> "max_score" : 0.99381393, ****
> 
> "hits" : [ { ****
> 
> "_index" : "testindex", ****
> 
> "_type" : "document", ****
> 
> "_id" : "uI5k0wggR9KAvG9o7S7L2g", ****
> 
> "_score" : 0.99381393, "_source" : {"tags": {"title": "people also
> 
> ```
> 
> kill people"}}\*\*\*\*
> 
> ```
> } ] ****
> 
> ```
> 
> },\*\*\*\*
> 
> "facets" : {\*\*\*\*
> 
> ```
> "popular_tags" : { ****
> 
> "_type" : "terms", ****
> 
> "missing" : 0, ****
> 
> "total" : 3, ****
> 
> "other" : 0, ****
> 
> "terms" : [ { ****
> 
> "term" : "people", ****
> 
> "count" : 1 *// I expect this to be 2 *****
> 
> }, { ****
> 
> "term" : "kill", ****
> 
> "count" : 1 ****
> 
> }, { ****
> 
> "term" : "also", ****
> 
> "count" : 1 ****
> 
> } ] ****
> 
> } ****
> 
> ```
> 
> }\*\*\*\*
> 
> * * *
> 
> * * *
> 
> The above result is not what I want. I want to have the frequency count be  
> 2 \*\*\*\*
> 
> * * *
> 
> "hits" : {\*\*\*\*
> 
> ```
> "total" : 1, ****
> 
> "max_score" : 0.99381393, ****
> 
> "hits" : [ { ****
> 
> "_index" : "testindex", ****
> 
> "_type" : "document", ****
> 
> "_id" : "uI5k0wggR9KAvG9o7S7L2g", ****
> 
> "_score" : 0.99381393, "_source" : {"tags": {"title": "people also
> 
> ```
> 
> kill people"}}\*\*\*\*
> 
> ```
> } ] ****
> 
> ```
> 
> },\*\*\*\*
> 
> "facets" : {\*\*\*\*
> 
> ```
> "popular_tags" : { ****
> 
> "_type" : "terms", ****
> 
> "missing" : 0, ****
> 
> "total" : 3, ****
> 
> "other" : 0, ****
> 
> "terms" : [ { ****
> 
> "term" : "people", ****
> 
> "count" : 2 ****
> 
> }, { ****
> 
> "term" : "kill", ****
> 
> "count" : 1 ****
> 
> }, { ****
> 
> "term" : "also", ****
> 
> "count" : 1 ****
> 
> } ] ****
> 
> } ****
> 
> ```
> 
> }\*\*\*\*
> 
> * * *
> 
> * * *
> 
> How do I achieve this? Is facet the wrong way to go?
> 
> * * *
> 
> * * *
> 
> * * *
> 
> --
> 
> * * *
> 
> --

--

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:06am UTC](https://discuss.elastic.co/t/return-term-frequency-of-a-single-field/9546/6 "2017-07-06T03:06:15Z")

</div>


