Is there a way to know datatype of key in Elasticsearch?

139 views Asked by At

I am facing issue like datatype of key getting changed. On creating index I have datatype as nested but for some reason, it gets changed to object. I make CRUD operations through the painless script but that seems to be fine.

Elastic version 7.3.0

Initial template:

"settings": {
  "number_of_shards": 1,
},
"mappings" : {
  "properties": {
    "deleted_at": { "type": "date" },
    "updated_at": { "type": "date" },
    "id": { "type": "integer" },
    "user_id": { "type": "integer" },

    ... some more keys

    "user_tags": {
      "type": "nested"
    },
    "user_files": {
      "type": "nested"
    },
  }
}

Mapping After some bulk insert/update

"mappings" : {
  "properties": {
    "deleted_at": { "type": "date" },
    "updated_at": { "type": "date" },
    "id": { "type": "integer" },
    "user_id": { "type": "integer" },
    "user_tags": {
      "properties": {
        ...some properties
      }
    },
    "user_files": {
      "properties": {
        ...some properties
      }
    },
  }
}

I have to reindex to fix this issue but it's happening very often. Also is there any way to know the datatype of key whether it is nested or object?

Thanks in advance.

1

There are 1 answers

0
Saeed Nasehi On

By setting "dynamic": "strict" your mapping won't be changed and unsuitable documents would not be inserted. To solve this problem you need to define all fields that you want to be inside of your nested field. For example:

{
"user_tags": {
            "type": "nested",
            "properties": {
              "code": {
                "type": "keyword",
                "store": true
              },
              "score": {
                "type": "float",
                "store": true
              }
              
            }
          }
}

If you want to just store a list you can use mapping as below:

{
    "user_tags": {
            "type": "keyword",
            "store": true
          }
}

In the second mapping you can store user_tags with this result ["tag1", "tag2", ...]