8

I have a JSON data that maps automatically by the elastic search when I'm indexing the data. How can I exclude some fields in the mapping. I already tried to define the map manually but when I'm doing bulk index, It automatically maps the other fields.

ex. my JSON data looks like this

[
 {
    "id": "232",
    "name": "Lorem",
    "description": "Ipsum Dolor",
    "image": [
             {"key": "asadasd.jpg"},
             {"key": "asasd2d.jpg"}
    ],
    "is_active": true
 },
 ...  

My map when I'm defining it manually

PUT myindex
{
    "mappings": {
        "product": {
            "properties": {
                "id": { "type": "text },
                "name": { "type": "text"},
                "description": { "type": "text" },
                "is_active": { "type": "boolean" }
            }
        }
    }
}

What I want to achieve is the data still remain I just want to exclude the image property to be not included in the indexing.

So that When I query in the elastic search is still I get the data with image

Is that possible?

Thank you guys. I'm new in elasticsearch

{
    "id": "232",
    "name": "Lorem",
    "description": "Ipsum Dolor",
    "image": [
             {"key": "asadasd.jpg"},
             {"key": "asasd2d.jpg"}
    ],
    "is_active": true
 }

2 Answers 2

9

Yes, that's possible simply by adding dynamic: false to your mapping, like this:

PUT myindex
{
  "mappings": {
    "product": {
      "dynamic": false,            <-- add this line
      "properties": {
        "id": {
          "type": "text"
        },
        "name": {
          "type": "text"
        },
        "description": {
          "type": "text"
        },
        "is_active": {
          "type": "boolean"
        }
      }
    }
  }
}

The image array will still be in the source, but the mapping won't be modified.

Sign up to request clarification or add additional context in comments.

2 Comments

Does it work only on object fields like enabled? In the documentation example, user is an object who has property object and social network object. And property object can add new fields but that will not be indexed or stored as top level dynamic attribute is` false. But it is not the case for social network` object where top level dynamic attribute is redefined as true
Yes, it only works on object fields and an inner object field can override the inherited setting from the parent.
6

The problem with the accepted answer is, that you need to explicitly add mappings for all fields, which is not always wanted (e.g. for array types). You could disable the field like this:

PUT myindex
{
    "mappings": {
        "product": {
            "properties": {
                "id": { "type": "text },
                "name": { "type": "text"},
                "description": { "type": "text" },
                "is_active": { "type": "boolean" },
                "image": { "type": "object, "enabled": false }
            }
        }
    }
}

The image array is still going to be in the _source.

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html

1 Comment

I think this makes sense. But what if I have a non top-level field to disable? I think enabled can only be applied to the top-level field according to the doc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.