Skip to content

Latest commit

 

History

History
65 lines (53 loc) · 1.34 KB

File metadata and controls

65 lines (53 loc) · 1.34 KB
endpoint mget
lang go
es_version 9.3
client github.com/elastic/go-elasticsearch/v9

Elasticsearch 9.3 mget endpoint (Go example)

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)
    }
}

Cross-index retrieval

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())

Selecting fields

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())