Emergard
Emergard

Reputation: 115

Elasticsearch how to exclude some fields in indexing

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
 }

Upvotes: 8

Views: 7727

Answers (2)

D. Phi
D. Phi

Reputation: 625

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

Upvotes: 6

Val
Val

Reputation: 217544

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.

Upvotes: 9

Related Questions