AI Hints
Home›Elasticsearch Tutorial›Versions, Bulk and Refresh
Elasticsearch · Lesson 18 of 120

Counting and Refresh

Near real-time.

⏱ 2 min read▶ Video at 16:27🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Elasticsearch is near real-time. A new document can be read by ID at once, but it only shows up in searches after a refresh. By default, that happens every second.

  • Get by ID: at once
  • Search: after refresh
  • Every second

Refresh on demand

We bulk index three products, without asking for a refresh. Then we force a refresh of the index. And count the documents. Run it. All three are counted.

Dev Tools Consoletext
POST _bulk?filter_path=errors
{ "index": { "_index": "products", "_id": "1" } }
{ "name": "Basecamp 2-Person Tent" }
{ "index": { "_index": "products", "_id": "2" } }
{ "name": "Storm Rain Jacket" }
{ "index": { "_index": "products", "_id": "3" } }
{ "name": "Trail Hiking Boots" }

POST products/_refresh?filter_path=_shards.successful

GET products/_count?filter_path=count
Output
# POST _bulk 200 OK
{"errors": false}

# POST products/_refresh 200 OK
{"_shards": {"successful": 1}}

# GET products/_count 200 OK
{"count": 3}
Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }

Three ways to see new data

Wait for the automatic refresh, about one second. Add refresh equals wait for, to wait until the document is searchable. Or call_refresh. Don't refresh after every document in production, because it's expensive.

keyvalue
Defaultrefresh every 1s
?refresh=wait_forwait until searchable
POST _refreshrefresh now
Avoidrefreshing per document

Summary

New documents become searchable after a refresh, about every second. Underscore count counts documents. Next, getting many documents by ID.

Key takeaways
  • Near real-time
  • _refresh
  • _count
Next up: Multi Get — many IDs, one request