DEV Community

Cover image for Elasticsearch Indices and Documents
Nico Orfanos
Nico Orfanos

Posted on • Originally published at nico.orfanos.dev

1

Elasticsearch Indices and Documents

To use Elasticsearch effectively, it's essential to understand key concepts like indexes and documents. In this blog post, we'll explore what indexes and documents are in the context of Elasticsearch.

What is an Index?

Imagine a kid's room with a drawer full of toys. In Elasticsearch, an index serves a similar purpose—it acts as a repository for related data. Think of it as a collection of documents that are grouped together for easy searching and retrieval.

To visualize this, let's consider a simple representation of a movies index:

movies
├─ Document 1
├─ Document 2
├─ Document 3
├─ ...
Enter fullscreen mode Exit fullscreen mode

Each individual piece of data within an index is called a Document.

You can create an Index using Sigmie like this:

$index = $sigmie->newIndex('movies')->create();
Enter fullscreen mode Exit fullscreen mode

What is a Document?

A Document is just a JSON stored in an Index.

Document = JSON
Enter fullscreen mode Exit fullscreen mode

In Elasticsearch, a Document is represented as a JSON object and stored within an Index. It contains the actual data that you want to search, analyze, or retrieve.

Here's an example of what a document might look like:

{
   "title": "The Shawshank Redemption"
}
Enter fullscreen mode Exit fullscreen mode

The document contains a single field, title, with the value The Shawshank Redemption.

The JSON structure is allowing us to represent various data types and structures. The Document structure is the most important part of achieving what you want using Elasticsearch.

Here’s how you can create an Instance of an Elasticsearch Document in Sigmie:

use Sigmie\Document\Document;

$document = new Document(['name' => 'The Shawshank Redemption']),
Enter fullscreen mode Exit fullscreen mode

What is Indexing

Indexing is the simple act of adding Documents into an Index. It doesn’t matter what are using Elasticsearch for, indexing is important simply because you can’t do anything without Documents in your index.

And this is the way to index a Document with Sigmie:

$index->collect()->add($document);
Enter fullscreen mode Exit fullscreen mode

An Example

Here's an example demonstrating how to add three movie documents to our "movies" index:

$documents = [
    new Document(['title' => 'The Shawshank Redemption']),
    new Document(['title' => 'Inception']),
    new Document(['title' => 'Pulp Fiction']),
];

$index->collect()->merge($documents);
Enter fullscreen mode Exit fullscreen mode

In the above code, we create an array of Document objects, each representing a movie. By invoking the merge method on the collected index and passing the documents, we add them to the index

Here is what the Index looks like once we merge the Documents.

movies
├─ "The Shawshank Redemption"
├─ "Inception"
├─ "Pulp Fiction"
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay