Created
April 19, 2023 21:26
-
-
Save acidjazz/d06d716a06784337156cd8fe74f48cc0 to your computer and use it in GitHub Desktop.
from domain to serverless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CopyIndex(index CatIndex, domain *opensearch.Client, serverless *opensearch.Client) (CopyResult, error) { | |
results := CopyResult{ | |
Index: index.Index, | |
Docs: 0, | |
Duration: time.Duration(0), | |
} | |
start := time.Now() | |
indexExists, err := serverless.Indices.Exists([]string{"initial-access"}) | |
if err != nil { | |
return results, err | |
} | |
if indexExists.StatusCode != 200 { | |
mappings, err := GetMappings(domain, index.Index) | |
if err != nil { | |
return results, err | |
} | |
if err := CreateIndex(serverless, index.Index, mappings); err != nil { | |
return results, err | |
} | |
} | |
scrollId := "" | |
indexer, err := opensearchutil.NewBulkIndexer(opensearchutil.BulkIndexerConfig{ | |
Client: serverless, | |
Index: index.Index, | |
NumWorkers: 96, | |
}) | |
if err != nil { | |
return results, err | |
} | |
for { | |
result, err := Search(domain, index.Index, scrollId) | |
if err != nil { | |
return results, err | |
} | |
scrollId = result.ScrollID | |
if len(result.Hits.Hits) == 0 { | |
break | |
} | |
for _, hit := range result.Hits.Hits { | |
if err := indexer.Add( | |
context.Background(), | |
opensearchutil.BulkIndexerItem{ | |
Action: "index", | |
Body: strings.NewReader(string(hit.Source)), | |
DocumentID: hit.ID, | |
}, | |
); err != nil { | |
return results, err | |
} | |
results.Docs++ | |
} | |
} | |
if err := indexer.Close(context.Background()); err != nil { | |
return results, err | |
} | |
stats := indexer.Stats() | |
after := time.Since(start) | |
log.Info(). | |
Str("index", index.Index). | |
Bool("exists", indexExists.StatusCode == 200). | |
Str("duration", after.String()). | |
Uint64("failed", stats.NumFailed). | |
Uint64("indexed", stats.NumFlushed). | |
Msg("indexed documents") | |
results.Duration = after | |
return results, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment