How Update Type of field or Update all values of field

Hello there! This is the current mappings for a index, and I want to update/edit date field from text to date

GET testing/_search

        "mappings" : {
      "inventory" : {
        "properties" : {
          "date" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "productId" : {
            "type" : "long"
          },
          "skuId" : {
            "type" : "long"
          },
          "stock" : {
            "properties" : {
              "quantity" : {
                "type" : "long"
              },
              "warehouse" : {
                "type" : "long"
              }
            }
          },
          "values" : {
            "type" : "long"
          }
        }
      }
    },

Everytime I try to update it

PUT testing/_mapping/inventory

{
  "properties": {
    "date":{
      "type": "date",
      "format": "date hour_minute_second"
    }
  }
}

but return error

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Invalid format: [date hour_minute_second]: Illegal pattern component: t"
}
],
"type": "illegal_argument_exception",
"reason": "Invalid format: [date hour_minute_second]: Illegal pattern component: t",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Illegal pattern component: t"
}
},
"status": 400
}

I think the problem is because the value is '2020-02-25 12:12:12' so I tried with format

Y-m-d H:m:s

But return error

mapper [date] of different type, current_type [text], merged_type [date]

Maybe the problem is because I didn't save the date with 'T' word, in this case how can I update all values to include a 'T' between date and time? Any idea or advice to fix my fail?
I can't reset the index and start again....
Thanks

The mapping for a field cannot be changed from text to date; you will need to reindex data into a new index that has the "date" field set as a date data type.

Take a look at the built-in formats for date; there's probably an existing format (or formats; multiple can be specified using || separator) that you can use.

1 Like

Thanks. I don't know why in previous attempts it didn't work and always just return

can't merge type [date] in [text]

I was creating a new template with index_patterns for the new index e.g

"index_patterns": [
"clone_testing"
],

After this... I made reindex

POST _reindex
{
"source": {
"index": "testing"
},
"dest": {
"index": "clone_testing"
}
}

Nevermind thanks!

Hey! Apologize for typing back.
The solution doesn't work. I'm trying to filter by range but returns values which doesn't correspond with the expected.
I created a template:

Put _template/sku_template

    "index_patterns" : [
      "sku"
    ],
    "mappings" : {
      "inventory" : {
        "properties" : {
          "skuId" : {
            "type" : "long"
          },
          "date" : {
            "type" : "date",
            "format" : "Y-m-d H:m:s"
          },
          "productId" : {
            "type" : "long"
          },
          "values" : {
            "type" : "long"
          },
          "stock" : {
            "properties" : {
              "quantity" : {
                "type" : "long"
              },
              "warehouse" : {
                "type" : "long"
              }
            }
          }
        }
      }
    },

Next

POST _reindex

POST _reindex
{
  "source": {
    "index": "sku_copy"
  },
  "dest": {
    "index": "sku"
  }
}

GET sku/_search

{
        "_index" : "sku",
        "_type" : "inventory",
        "_id" : "j9C-tW8BAn4d18TtXnWO",
        "_score" : 1.0,
        "_source" : {
          "skuId" : 1234,
          "productId" : 1234,
          "values" : [
            1
          ],
          "stock" : {
            "quantity" : 1,
            "warehouse" : 1
          },
          "date" : "2020-01-17 12:12:12"
        }
      },

This is one result, to see the date format.

I don't think this format would match for "date" : "2020-01-17 12:12:12". The response from the _reindex call indicates how many documents were created and failed - did the response indicate failures when attempting to parse dates with the format?

The value that you specify for "format" in the mapping is the one that Elasticsearch uses to parse the incoming date string. Based on the example provided, I think the format you want is

"format" : "YYYY-MM-dd HH:mm:ss"

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