Elasticsearch Indices and Documents

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

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
├─ ...

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();

What is a Document?

A Document is just a JSON stored in an Index.

Document = JSON

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"
}

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']),

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);

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);

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"