Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Indices and Documents in Elasticsearch

Introduction

Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. At its core, it allows you to store, search, and analyze big volumes of data quickly and in near real-time. Two fundamental concepts in Elasticsearch are indices and documents.

What is an Index?

An index in Elasticsearch is like a database in a traditional relational database. It is a collection of documents that share similar characteristics. An index is identified by a name, which is used to refer to the index when performing various operations within it.

Example: Creating an index named my_index

PUT /my_index
{ "acknowledged": true, "shards_acknowledged": true, "index": "my_index" }

What is a Document?

A document is a basic unit of information that can be indexed. It is expressed in JSON (JavaScript Object Notation), which is a popular data interchange format. Each document is stored in an index and has a unique identifier.

Example: Adding a document to the my_index index

PUT /my_index/_doc/1
{"name": "John Doe", "age": 30, "occupation": "Software Engineer"}
{ "_index": "my_index", "_type": "_doc", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }

Index Operations

There are several operations that you can perform on an index. Some of the most common operations include:

  • Creating an index
  • Deleting an index
  • Getting index information

Example: Deleting the my_index index

DELETE /my_index
{ "acknowledged": true }

Document Operations

Elasticsearch provides several operations to manage documents within an index. Common document operations include:

  • Indexing a document
  • Updating a document
  • Deleting a document
  • Retrieving a document

Example: Retrieving a document from the my_index index

GET /my_index/_doc/1
{ "_index": "my_index", "_type": "_doc", "_id": "1", "_version": 1, "_seq_no": 0, "_primary_term": 1, "found": true, "_source": { "name": "John Doe", "age": 30, "occupation": "Software Engineer" } }

Conclusion

Understanding indices and documents is crucial to effectively using Elasticsearch. Indices help you organize your data, while documents store your data in a structured format. With these basics, you can start exploring more advanced features of Elasticsearch to build powerful search and analytics solutions.