﻿---
title: CRUD operations
description: This page covers common CRUD (Create, Read, Update, Delete) operations with the Go client using the typed API and the esdsl builders. For the low-level...
url: https://www.elastic.co/elastic/docs-builder/docs/3175/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 using the [typed API](https://www.elastic.co/elastic/docs-builder/docs/3175/reference/elasticsearch/clients/go/typed-api) and the [`esdsl`](https://www.elastic.co/elastic/docs-builder/docs/3175/reference/elasticsearch/clients/go/typed-api/esdsl) builders. For the low-level equivalents, see the [low-level API](https://www.elastic.co/elastic/docs-builder/docs/3175/reference/elasticsearch/clients/go/low-level-api).
```go
import "github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
```


## Creating an index

For this example, let's create an index named `test-index` and provide a mapping for the field `price` which will be an integer.
The [`esdsl`](https://www.elastic.co/elastic/docs-builder/docs/3175/reference/elasticsearch/clients/go/typed-api/esdsl) mapping builders provide a fluent syntax for defining index mappings. The correct `type` field is set automatically for each property:
```go
res, err := es.Indices.Create("test-index").
    Mappings(
        esdsl.NewTypeMapping().
            AddProperty("price", esdsl.NewIntegerNumberProperty()),
    ).
    Do(context.Background())
```


## 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.
```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").                        
    Document(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())
```


## 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.
```go
res, err := es.Get(
    "index_name",
    "doc_id",    
).Do(context.Background())
```


## 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, use 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.