Post-Indexing: Updates

🏡 Home 📖 Chapter Home 👈 Prev 👉 Next
⚡  ElasticsearchBook is crafted by Jozef Sorocin (🟢 Book a consulting hour) and powered by:
Documents often require updating — fields need to be incremented, modified, overwritten, or deleted. In this section we'll discuss situations where you:
  1. know the ID of the doc in question and want to update only that particular document
  1. want to update a group of documents that have something in common (i.e. match a query)

Use Case 1: Single-doc Updates

I'm tracking visits to my site based on the slug. A sample entry in my site_visits index:
POST site_visits/_doc/home_id
  "slug": "/home",
  "visits": 0,
  "tags": ["landing", "ab_test_100"],
  "unneeded_attribute": "old"
How do I
  • add a modified_at field and set it to now
  • increment the visits count
  • remove the ab_test_100 tag
  • and delete the unneeded attribute?

Approach to #1

For adding new fields such as modified_at, you may be tempted to repeat the POST call from above with only the new field being present:
POST site_visits/_doc/home_id
  "modified_at": "2020-12-05T14:11:41.634Z"

Already purchased? Sign in here.