﻿---
title: Searching
description: This page covers how to build and run search queries with the Go client. For the full Elasticsearch search API reference, see the Search API documentation...
url: https://www.elastic.co/elastic/docs-builder/docs/3028/reference/elasticsearch/clients/go/using-the-api/searching
products:
  - Elasticsearch
  - Elasticsearch Client
  - Elasticsearch Go Client
---

# Searching
This page covers how to build and run search queries with the Go client. For the full Elasticsearch search API reference, see the [Search API documentation](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-search).

## Running a search

As an example, let's search for a document with a field `name` with a value of `Foo` in the index named `index_name`.
<tab-set>
  <tab-item title="Low-level API">
    ```go
    query := `{
      "query": {
        "match": {
          "name": { "query": "Foo" }
        }
      }
    }`

    res, err := client.Search(
        client.Search.WithIndex("index_name"),      
        client.Search.WithBody(strings.NewReader(query)),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    With a struct request:
    ```go
    res, err := es.Search().
        Index("index_name").
        Request(&search.Request{
            Query: &types.Query{
                Match: map[string]types.MatchQuery{
                    "name": {Query: "Foo"},
                },
            },
        }).Do(context.Background())
    ```
    It produces the following JSON:
    ```json
    {
      "query": {
        "match": {
          "name": {
            "query": "Foo"
          }
        }
      }
    }
    ```
  </tab-item>

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

    ```go
    res, err := es.Search().
        Index("index_name").
        Query(esdsl.NewMatchQuery("name", "Foo")).
        Do(context.Background())
    ```
  </tab-item>
</tab-set>


## Request structures

The typed API models each endpoint's body as a Go `Request` struct. For example, a simple term query for `"Foo"` in the `name` field:
```go
search.Request{
    Query: &types.Query{
        Term: map[string]types.TermQuery{
            "name": {Value: "Foo"},
        },
    },
}
```


## Raw JSON

If you want to use your own pre-baked JSON queries using templates or a specific encoder, you can pass the body directly. Both API styles support raw JSON payloads:
<tab-set>
  <tab-item title="Low-level API">
    ```go
    res, err := client.Search(
        client.Search.WithIndex("index_name"),
        client.Search.WithBody(strings.NewReader(`{
          "query": {
            "term": {
              "user.id": {
                "value": "kimchy",
                "boost": 1.0
              }
            }
          }
        }`)),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    ```
  </tab-item>

  <tab-item title="Fully-typed API">
    ```go
    res, err := es.Search().
        Index("index_name").
        Raw([]byte(`{
          "query": {
            "term": {
              "user.id": {
                "value": "kimchy",
                "boost": 1.0
              }
            }
          }
        }`)).Do(context.Background())
    ```
    No further validation or serialization is done on what is sent through this method. Setting a payload with `Raw` takes precedence over any request structure you may submit before running the query.
  </tab-item>
</tab-set>