| endpoint | mget |
|---|---|
| lang | go |
| es_version | 9.3 |
| client | github.com/elastic/go-elasticsearch/v9 |
Use client.Mget() to retrieve multiple documents by ID in a single
request. This is significantly faster than issuing individual Get
calls.
response, err := client.Mget().
Index("products").
Request(&mget.Request{
Ids: []string{"prod-1", "prod-2", "prod-3"},
}).
Do(context.Background())
if err != nil {
log.Fatalf("Error: %s", err)
}
for _, doc := range response.Docs {
if hit, ok := doc.(*types.GetResult); ok {
fmt.Printf("%s: found=%t\n", hit.Id_, hit.Found)
}
}Fetch documents from different indices in a single call by specifying
Index_ per document:
index1 := "products"
index2 := "orders"
response, err := client.Mget().
Request(&mget.Request{
Docs: []types.MgetOperation{
{Index_: &index1, Id_: "prod-1"},
{Index_: &index2, Id_: "order-42"},
},
}).
Do(context.Background())Use Source_ to limit which fields are returned:
source := types.SourceFilter{Includes: []string{"name", "price"}}
response, err := client.Mget().
Index("products").
Request(&mget.Request{
Ids: []string{"prod-1", "prod-2"},
Source_: types.SourceConfig(source),
}).
Do(context.Background())