# Mapping: how to disable array of string?

**URL:** <https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782>\
**Category:** Elasticsearch\
**Created:** [October 4, 2022, 2:01pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782 "2022-10-04T14:01:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 4, 2022, 2:01pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/1 "2022-10-04T14:01:09Z")

</div>

We want to apply a **real strict mapping** , but arrays are still accepted and mess around our data pipeline (because after we use Apache Spark to read data).

We (at our team level) have no control on the incoming data.

How can we specify a real strict mapping to reject such document?

```auto
PUT /poc-mapping
{
  "settings": {
    "number_of_shards": 1,
    "index" : {
      "mapping" : {
        "ignore_malformed" : false
      }
    }
  }, 
  "mappings": {
    "dynamic":"strict",
    "properties": {
      "request" : {
        "properties": {
          "uri" : {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

# This should not work
POST /poc-mapping/_doc/1
{
  "request" : {
    "uri" : ["nop"]
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [October 4, 2022, 3:24pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/2 "2022-10-04T15:24:44Z")

</div>

When you use **strict** :

> If new fields are detected, an exception is thrown and the document is rejected. New fields must be explicitly added to the mapping.

I believe it will only restrict if it is not the mapped field, in which case the post below will fail (field request.url no mapped).

```auto
POST /poc-mapping/_doc/1
{
  "request" : {
    "url" : ["nop"]
  }
}

```

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 4, 2022, 3:28pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/3 "2022-10-04T15:28:49Z")

</div>

yes, but the field `request.uri` is mapped, and I want it as a real string, not an array of strings

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 4, 2022, 11:14pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/5 "2022-10-04T23:14:26Z")

</div>

I got the feeling that is not possible 😕

Look like array of concrete values (string / number...) is treated like a concrete values.

---

<div class="post-metadata">

**Author:** ![xeraa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xeraa/32/48181_2.png) [@xeraa](https://discuss.elastic.co/u/xeraa)\
**Post date:** [October 4, 2022, 11:40pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/6 "2022-10-04T23:40:33Z")

</div>

I'm not aware of any mapping feature to disallow arrays. Also, how would you want to treat that scenario — reject the document?

My first idea for a workaround would be an `index.default_pipeline` that checks for multiple values in a field and then either fails the document or removes some elements. Would that work?

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 5, 2022, 12:07am UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/7 "2022-10-05T00:07:52Z")

</div>

So sad we cant guarantee data integrity by using elasticsearch.

As a workaround, we have started to develop an ingestion service in front of elasticsearch, but I would prefer a solution 100% with elasticsearch mapping (easier to replicate etc...).

Especially with Apache Spark, data integrity is very important, I am really surprised this is not implemented

---

<div class="post-metadata">

**Author:** ![xeraa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xeraa/32/48181_2.png) [@xeraa](https://discuss.elastic.co/u/xeraa)\
**Post date:** [October 5, 2022, 12:30am UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/8 "2022-10-05T00:30:45Z")

</div>

I guess the data integrity view depends on your expectations; there are no constraints like `NOT NULL` either but I'm not sure those are really what you want to use a search engine for.

BTW we almost got this feature: [New field mapping flag - allow\_multiple\_values by markharwood · Pull Request #80289 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/pull/80289)  
And there's also a bigger meta-issue around the topic: [[Meta] Better handling of single-valued fields · Issue #80825 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/80825)

In the end, I'd look into a default ingest pipeline. Feels reasonable to me and also as close to the destination as possible.

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 5, 2022, 12:33am UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/9 "2022-10-05T00:33:08Z")

</div>

Such a big PR!! Thanks you, I didnt find this issue.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [October 5, 2022, 8:00am UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/10 "2022-10-05T08:00:18Z")

</div>

FYI having left elastic I’m not working on the PR any more. It was a potentially big change and there was reluctance because this might represent the start of a very slippery slope of adding all kinds of validation (arrays, enums, acceptable numeric value ranges etc).  
In many businesses elasticsearch gets fired at with all kinds of data streams that evolve constantly but still need to be captured. To cope with that leniency is more valued than strictness.  
The lesson I take from proposing this change is there’s a policy line drawn where elasticsearch mappings attempt to perform validation declaratively Vs using application-specific code.

While I agree that mappings shouldn’t validate _everything_ I think the vast majority of content stored in elasticsearch consists of single-valued fields and there should be a simple option to validate that on the way in. (It would have to be opt-in because the default has always supported multi value and too many things would break if elasticsearch changed that).

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 5, 2022, 1:26pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/11 "2022-10-05T13:26:45Z")

</div>

yes, to me there is a big gap between validation of the data content and data type itself.

If I can compare to any computing language, **Array is a data type** , and must be explicitely declared as is. All programmers know that. And this difference must be part of the data schema (here the mapping).

Then data content validation is another story.

Mixing array and concrete values are fine in most elasticsearch usage (search, aggregation ...) but **it fails when using Apache Spark, which is a bug because we use the official elastic Spark "driver".** (because spark is receiving string then string =\> kaboom)

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [October 5, 2022, 3:42pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/12 "2022-10-05T15:42:56Z")

</div>

> [@ebuildy](#):
>
> Mixing array and concrete values are fine in most elasticsearch usage

I forget exactly where but there are already aspects to elasticsearch (data streams? Transforms?) where content is grouped or routed by a choice of field which absolutely has to be single-valued and not an array.

---

<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:** [November 2, 2022, 3:43pm UTC](https://discuss.elastic.co/t/mapping-how-to-disable-array-of-string/315782/13 "2022-11-02T15:43:52Z")

</div>

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