# Elasticsearch API and Ingestion Pipeline

## Elasticsearch API

***`Task:`*** ***`Create a new index containing three documents. After creation, delete one document using the DELETE method and another using a query-based deletion. Then, reindex the remaining documents and perform an index flush.`***

First, let's open Kibana Dev Tools and initiate the creation of a new index.

<figure><img src="/files/jJKN8zS1sVTwMEyEpxkK" alt=""><figcaption></figcaption></figure>

```json
PUT /weinnovate 
```

<figure><img src="/files/2gCq4ubHPWLocEOwS9DE" alt=""><figcaption></figcaption></figure>

Now, let's add three documents to this index:

```json
POST /weinnovate/_doc/one
{
  "name": "Fares",
  "city": "Al Mahalla",
  "age": "23"
}

POST /weinnovate/_doc/two
{
  "name": "Omar",
  "city": "Cairo",
  "age": "23"
}

POST /weinnovate/_doc/three
{
  "name": "Ahmed",
  "city": "Mansoura",
  "age": "25"
}
```

<figure><img src="/files/Uj3Tdzz3A4cfFEo8b0n4" alt=""><figcaption></figcaption></figure>

Now, let's proceed with deleting the first document.

```json
DELETE /weinnovate/_doc/one
GET /weinnovate/_search
```

<figure><img src="/files/Hb3NluGsRKjdoHjNmNS4" alt=""><figcaption></figcaption></figure>

Next, let's try delete one document by query.

```json
POST /weinnovate/_delete_by_query
{
  "query":{
    "match":{
      "name": "Omar"
    }
  }
}

GET /weinnovate/_search
```

<figure><img src="/files/vDDm6b1lRq6MnyAjjmlv" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/eXZGzenYXpsbaxKj7t69" alt=""><figcaption></figcaption></figure>

We now need to reindex the documents, which enables us to copy them from one index to another. Before reindexing, we need to create a new index

```json
PUT weinnovate_new
```

<figure><img src="/files/ZLGclB9KmaDxyeRfW6Zn" alt=""><figcaption></figcaption></figure>

Now, let's reindex the data from **`weinnovate`** to **`weinnovate_new`**.

```json
POST _reindex
{
  "source": {
    "index": "weinnovate"
  },
  "dest": {
    "index": "weinnovate_new"
  }
}
```

<figure><img src="/files/y7gANmH5wji0fcM2aumS" alt=""><figcaption></figcaption></figure>

```json
GET /weinnovate_new/_search
```

<figure><img src="/files/lFvgQoYuq1luN8m1JGox" alt=""><figcaption></figcaption></figure>

Flushing an index in Elasticsearch ensures that all operations are written to disk. This can help free up memory and optimize performance.

```json
POST weinnovate_new/_flush
```

<figure><img src="/files/dbujlwJrOWLMfrd3Pywd" alt=""><figcaption></figcaption></figure>

## Ingestion Pipline

Ingest pipelines let you perform common transformations on your data before indexing. For example, you can use pipelines to remove fields, extract values from text, and enrich your data.

***`Task: Create a pipeline with three distinct processors for a new index.`***

First, we will create a pipeline named **`weinnovate_pipeline`**, which will include three processors.

```json
PUT _ingest/pipeline/weinnovate_pipline
{
  "description": "New Pipline for the weinnovate index",
  "processors": [
    {"append":{
      "field": "Status",
      "value": ["Active"]
      }
    }, 
    {"convert":{
      "field": "age",
      "type": "integer"
      }
    }, 
    {"uppercase":{
        "field": "name"
      }
    }
  ]
}
```

* The **`append`** processor adds a new value **`"Active"`** to the **`Status`** field.
* The **`convert`** processor converts the value of **`"age"`** to an **`integer`**.
* The **`uppercase`** processor converts the **`"name"`** field to **`uppercase`**.

<figure><img src="/files/ekb1XxcPQ9XDC0zkCSLK" alt=""><figcaption></figcaption></figure>

We need to confirm that the pipeline was created successfully.

```json
GET _ingest/pipeline/weinnovate_pipeline
```

<figure><img src="/files/rAu2yUOF1u9KznYY2OzA" alt=""><figcaption></figcaption></figure>

Now, I want to apply this pipeline to the previously created **`"weinnovate"`** index. To do this, we first need to reindex the existing data while applying the pipeline during the reindexing process.

```json
POST _reindex
{
  "source": {
    "index": "weinnovate"
  },
  "dest": {
    "index": "weinnovate_two",  
    "pipeline": "weinnovate_pipline"
  }
}
```

<figure><img src="/files/8D2RxZpZ7eVe6zKmkfIZ" alt=""><figcaption></figcaption></figure>

Let's verify this by retrieving the documents from the **`weinnovate_two`** index.

```json
GET /weinnovate_two/_search
```

<figure><img src="/files/bmxB8bg0yuMsYEimBmlT" alt=""><figcaption></figcaption></figure>

Alternatively, we can first create the pipeline and then apply it when creating a new index.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://faresbltagy.gitbook.io/footprintinglabs/build-elk-lab/elasticsearch-api-and-ingestion-pipeline.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
