# Why is the length of keyword array always 1?

**URL:** <https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164>\
**Category:** Elasticsearch\
**Created:** [March 21, 2023, 12:06pm UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164 "2023-03-21T12:06:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyq2333](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lyq2333/32/118714_2.png) [@lyq2333](https://discuss.elastic.co/u/lyq2333)\
**Post date:** [March 21, 2023, 12:06pm UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/1 "2023-03-21T12:06:38Z")

</div>

I create an index by  
`PUT my-index-000002 { "mappings": { "properties": { "content":{ "type": "keyword", "index_options": "freqs" }, "id":{ "type": "integer" } } } }`

Then I add a doc by  
`POST my-index-000002/_doc/1 { "content": ["apple","apple"] }`

I search the doc by  
`GET my-index-000002/_search { "query": { "term": { "content": { "value": "apple" } } }, "explain": true }`

But the dl in explanation is 1.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/d/cd0260cabc0f030529f291ff618dd06d959469b0.jpeg)

Is it a bug or by design？Can anyone help me？

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [March 22, 2023, 12:13am UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/2 "2023-03-22T00:13:41Z")

</div>

Welcome to our community! 😃 And Thanks heaps for providing a replication, it makes it heaps easier to see what you are doing!

From [Wrong averageFieldLength(avgdl, average length of field) and fieldLength (dl, length of field) calculation · Issue #46855 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/46855#issuecomment-533071569);

> `dl` is the field length for the document so the number of terms that it contains, we call it the norm of the field

So you've defined `content` as a `keyword`. You've passed in an array, but it's going to treat that array as a single value because of that mapping, and that's what it's reporting back to you.

---

<div class="post-metadata">

**Author:** ![lyq2333](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lyq2333/32/118714_2.png) [@lyq2333](https://discuss.elastic.co/u/lyq2333)\
**Post date:** [March 22, 2023, 1:01am UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/3 "2023-03-22T01:01:57Z")

</div>

Thanks a lot！

If the keyword array is treated as a single value, I think the avgdl should be always 1. Could you tell me the reason why the avgdl is 2 in the explanation?

Any reply will be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [March 22, 2023, 3:57am UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/4 "2023-03-22T03:57:43Z")

</div>

Not sure I can help there sorry, I struggled to find that info as it was.

Hopefully someone with more knowledge can step in!

---

<div class="post-metadata">

**Author:** ![lyq2333](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lyq2333/32/118714_2.png) [@lyq2333](https://discuss.elastic.co/u/lyq2333)\
**Post date:** [March 22, 2023, 5:38am UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/5 "2023-03-22T05:38:13Z")

</div>

Thanks for your help！ 😃

Anyone else have any ideas?

Many Thanks！

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [March 22, 2023, 1:40pm UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/6 "2023-03-22T13:40:52Z")

</div>

I think it's by design. Among the [keyword field parameters](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/keyword.html#keyword-params) is `norms`, which controls "whether field-length should be taken into account when scoring queries." The default is `false`. If I set it to "true" in the mapping definition, I see the expected result. Here's the mapping:

```auto
PUT my-index-000002
{
  "mappings": {
    "properties": {
      "content": {
        "type": "keyword",
        "index_options": "freqs",
        "norms": true
      },
      "id": {
        "type": "integer"
      }
    }
  }
}

```

Note that the formula in the explanation is `tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl))`. Scoring `dl` as `1.0` makes it do nothing in the formula; I would expect to see it as `1.0` for any keyword field score when `norms` is disabled.

Let me know if this works for you. (You'll have to delete and recreate the index, or test on a new one.)

The trade-off for `norms`, according to [the docs](https://www.elastic.co/guide/en/elasticsearch/reference/8.6/norms.html), is that the index will require more disk space.

---

<div class="post-metadata">

**Author:** ![lyq2333](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lyq2333/32/118714_2.png) [@lyq2333](https://discuss.elastic.co/u/lyq2333)\
**Post date:** [March 23, 2023, 12:56am UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/7 "2023-03-23T00:56:28Z")

</div>

Thanks! It works for me. 😃

---

<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:** [April 20, 2023, 12:56am UTC](https://discuss.elastic.co/t/why-is-the-length-of-keyword-array-always-1/328164/8 "2023-04-20T00:56:59Z")

</div>

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