Search code examples
elasticsearchtypeselasticsearch-mappingelasticsearch-6.8

Elasticsearch - Wrong field type


I'm running on ElasticSearch 6.8.

I tried to add a keyword type field to my index mapping. What I want is a mapping with my_field seeming like that:

"my_field": {
  "type": "keyword"
}

So in order to do that, I added a field to my mapping:

"properties": {
  ...
  "my_field": {
    "type": "keyword",
    "norms": false
  },
  ...
}

But currently, it gives me something like:

"my_field": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }
}

I need this keyword type because I need to aggregate on it, and with a text type, it gave me:

Fielddata is disabled on text fields by default. Set fielddata=true on [my_field] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

But I'm not able to set fielddata to true.

I tried many things like creating a new index instead of updating one but none of these tries worked.

  • Anyone knows how to have the correct field type ? (the solution I prefer)

  • Or how to set fielddata to true in the mapping?

Best regards, Jules


Solution

  • I just created set field-data to true on text field by using below curl command on Elasticsearch 6.X version:

    curl -X POST "localhost:9200/my_index/type?pretty" -H 'Content-Type: application/json' -d'
    > {
    >   "mappings" :{
    >   "properties": {
    >     "my_field": {
    >       "type":     "text",
    >       "fielddata": true
    >     }
    >   }
    >   }
    > }'
    

    And it created index with proper mapping.

    {
      "_index" : "my_index",
      "_type" : "type",
      "_id" : "3Jl0F3EBg44VI1hJVGnz",
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }
    

    Mapping API gives below JSON response.

    {
      "my_index": {
        "mappings": {
          "type": {
            "properties": {
              "mappings": {
                "properties": {
                  "properties": {
                    "properties": {
                      "my_field": {
                        "properties": {
                          "fielddata": {
                            "type": "boolean"
                          },
                          "type": {
                            "type": "text",
                            "fields": {
                              "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }