Losing mappings when I delete documents?

I like to refresh my index daily with a cron job using the following to delete documents in a given index:

curl -XDELETE 'http://localhost:9200/'$1'/?pretty=true'
Where $1 is the index name passed in.

Problem is I seem to lose my mappings on the index.
For example, I have a field I want as 'NOT_ANALYZED'. This works on the initial load when I create the index and the mapping at once. But once I run the above delete command, my mappings are not there.

Is this the proper way to delete documents from my index? If not , how do I do so using curl command?

Using:

curl -XDELETE 'http://localhost:9200/'$1'/?pretty=true'

Will delete the entire index including all documents and the mapping. So this is expected. So you have a few choices:

  1. Save your mapping and as part of your cron job recreate the index with the mapping:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

curl -XPUT http://localhost:9200/myindex -d @mapping.json
  1. Create an index template, so that when you recreate your index it automatically gets applied:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

  1. If you're using Elasticsearch 2.x, you can use the delete by query plugin to delete all the documents and not remove the entire index:

https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html

1 Like