﻿---
title: CRUD operations
description: This page covers common CRUD (Create, Read, Update, Delete) operations with the Go client. For this example on how to create an index, lets create an...
url: https://www.elastic.co/elastic/docs-builder/docs/3016/reference/elasticsearch/clients/go/using-the-api/crud-operations
products:
  - Elasticsearch
  - Elasticsearch Client
  - Elasticsearch Go Client
---

# CRUD operations
This page covers common CRUD (Create, Read, Update, Delete) operations with the Go client.

## Creating an index

For this example on how to create an index, lets create an index named `test-index` and provide a mapping for the field `price` which will be an integer.
<tab-set>
  <tab-item title="Low-level API">
    ```go
    mapping := `{
      "mappings": {
        "properties": {
          "price": { "type": "integer" }
        }
      }
    }`

    res, err := client.Indices.Create(
        "test-index",
        client.Indices.Create.WithBody(strings.NewReader(mapping)),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    Notice how using the builder for the `IntegerNumberProperty` will automatically apply the correct value for the `type` field.
    ```go
    res, err := es.Indices.Create("test-index").
        Request(&create.Request{
            Mappings: &types.TypeMapping{
                Properties: map[string]types.Property{
                    "price": types.NewIntegerNumberProperty(),
                },
            },
        }).
        Do(context.Background())
    ```
  </tab-item>

  <tab-item title="esdsl API">
    The [`esdsl`](https://www.elastic.co/elastic/docs-builder/docs/3016/reference/elasticsearch/clients/go/typed-api/esdsl) mapping builders provide a fluent syntax for defining index mappings:
    ```go
    import "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
    ```

    ```go
    res, err := es.Indices.Create("test-index").
        Mappings(
            esdsl.NewTypeMapping().
                AddProperty("price", esdsl.NewIntegerNumberProperty()),
        ).
        Do(context.Background())
    ```
  </tab-item>
</tab-set>


## Indexing a document

The standard way of indexing a document is to provide the document body to the index request. The document will be serialized as JSON and sent to Elasticsearch.
<tab-set>
  <tab-item title="Low-level API">
    ```go
    document := struct {
        Id    int    `json:"id"`
        Name  string `json:"name"`
        Price int    `json:"price"`
    }{
        Id:    1,
        Name:  "Foo",
        Price: 10,
    }

    data, _ := json.Marshal(document)
    res, err := client.Index(
        "index_name",             
        bytes.NewReader(data),    
        client.Index.WithDocumentID("1"),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    ```go
    document := struct {
        Id    int    `json:"id"`
        Name  string `json:"name"`
        Price int    `json:"price"`
    }{
        Id:    1,
        Name:  "Foo",
        Price: 10,
    }

    res, err := es.Index("index_name").
        Id("1").                        
        Request(document).              
        Do(context.Background())
    ```
    Alternatively, you can use the `Raw` method and provide already serialized JSON:
    ```go
    res, err := es.Index("index_name").
        Raw([]byte(`{
          "id": 1,
          "name": "Foo",
          "price": 10
        }`)).Do(context.Background())
    ```
  </tab-item>
</tab-set>


## Retrieving a document

Retrieving a document follows the API as part of the argument of the endpoint. You provide the `index` and the `id`, then run the query.
<tab-set>
  <tab-item title="Low-level API">
    ```go
    res, err := client.Get(
        "index_name",
        "doc_id",    
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    ```go
    res, err := es.Get(
        "index_name",
        "doc_id",    
    ).Do(context.Background())
    ```
  </tab-item>
</tab-set>


## Checking for a document existence

If you do not wish to retrieve the content of the document and want only to check if it exists in your index:
<tab-set>
  <tab-item title="Low-level API">
    ```go
    res, err := client.Exists("index_name", "doc_id")
    if err != nil {
        // Handle error.
    }
    defer res.Body.Close()

    if !res.IsError() {
        // The document exists!
    }
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    The typed API provides the `IsSuccess` shortcut:
    ```go
    if exists, err := es.Exists("index_name", "doc_id").IsSuccess(nil); exists {
        // The document exists!
    } else if err != nil {
       // Handle error.
    }
    ```
    Result is `true` if everything succeeds, `false` if the document doesn't exist. If an error occurs during the request, you will get `false` and the relevant error.
  </tab-item>
</tab-set>