How to add date format in _delete_by_query

Hello Team

Could you please help me how to add a format while performing operation of "_delete_by_query" on Index were condition of deletion is based on

range --> on a field having type as "epoch" and i can pass the date to this query is like "2020-11-07"

{
    "query": {
      "range": {
        "sysTimeStamp": { "lt": "2020-11-05"  , "format": "yyyy-MM-dd"  }
      }
    }
}
' 

Could you please help me

Thanks
Tushar Nemade

I am getting error :slight_smile:

          "type" : "query_shard_exception",
          "reason" : "failed to create query: For input string: \"2020-11-07\"",
          "index_uuid" : "WKmRbiRqSr2OhANmvD3yow",
          "index" : "test-1",
          "caused_by" : {
            "type" : "number_format_exception",
            "reason" : "For input string: \"2020-11-07\""

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

May be share the mapping

Are you referring to a timestamp field with EPOCH format? Which version of Elasticsearch are you using?

Hi David

Mapping of my Filed is as below :

        "sysTimeStamp" : {
          "type" : "date",
          "format" : "epoch_millis"
        },

Hi

I am trying to delete using below Command :

curl -X POST "localhost::9200/test-0001/_delete_by_query?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
      "range": {
        "sysTimeStamp": { "lt": "2020-11-05" , "format": "yyyy-MM-dd" }
      }
    }
}
'

Hi

Yes , My Filed is of EPOCH Format.

ElasticSearch Version 7.8.0

Please excuse me for my late response. Was busy with Deployments and stuff ....

Could you share the full output of the following command?

GET YOURINDEXNAME/_mapping

This is not a full reproduction script. Please read again the instructions I shared.

1 Like

Hi David

Yes , I did understand. I shared only the information which was relevant to my error / my query which is not working...

Here is my entire mapping of said index :

    "mappings" : {
      "properties" : {
        "accountId" : {
          "type" : "long"
        },
        "action" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "clientDetails" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "commentKey" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "deviceId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "devicekey" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "eventId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "eventName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "forwarderDetails" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "initiatingScreen" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isComment" : {
          "type" : "boolean"
        },
        "latitude" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "longitude" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectAudios" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectCategs" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectDescription" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectEffects" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectFilters" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectHashtags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "objectKey" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectLangs" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "objectName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "ownerId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "playDuration" : {
          "type" : "long"
        },
        "reactionType" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "referrerAudio" : {
          "type" : "text",
          "fielddata" : true
        },
        "referrerCategory" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        },
        "replay" : {
          "type" : "boolean"
        },
        "sessionId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "shardKey" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "shareType" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "sysTimeStamp" : {
          "type" : "date",
          "format" : "epoch_millis"
        },
        "timeStamp" : {
          "type" : "date",
          "format" : "epoch_millis"
        },
        "timestamp" : {
          "type" : "long"
        },
        "totalDuration" : {
          "type" : "long"
        },
        "userAgent" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "userId" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }

I'm not able to reproduce your problem.

Here is the kind of script I was asking for. If you can provide a similar one which reproduces your issue, please share it. Also use latest elasticsearch version. I don't think you shared the version. May be you are using an old one?

DELETE test 
PUT test
{
  "mappings": {
    "properties": {
      "date": {
        "type": "date",
        "format": "epoch_millis"
      }
    }
  }
}
POST test/_doc
{
  "date": "1604913154000"
}
GET test/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "01/01/2020",
        "format": "dd/MM/yyyy"
      }
    }
  }
}
POST /test/_delete_by_query?pretty
{
  "query": {
    "range": {
      "date": {
        "gte": "01/01/2020",
        "format": "dd/MM/yyyy"
      }
    }
  }
}
GET /test/_search
1 Like

I did shared the version above ..

Also I will share my demo in here ... which is causing the error...

Hi David

Thanks for your reponse.

I found my mistake. the Index was auto-created based on reindexing curl command.

Due to this , field "sysTimestamp" was getting type as "text" instead of date and format.

Hence by _delete_by_query was failing with my error mentioned above

type" : "number_format_exception",
            "reason" : "For input string: \"2020-11-07\"

I am now creating my index using defined mapping and things are good now.

Thank You very much for your help and responses.

Tushar Nemade

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