fix(search): potential nil slice entries

This commit is contained in:
fschade
2025-08-08 08:37:49 +02:00
parent f3750f32c9
commit 8795284a76
2 changed files with 13 additions and 7 deletions

View File

@@ -120,12 +120,12 @@ func (e *Engine) Search(ctx context.Context, sir *searchService.SearchIndexReque
return nil, fmt.Errorf("failed to search: %w", err)
}
matches := make([]*searchMessage.Match, len(resp.Hits.Hits))
matches := make([]*searchMessage.Match, 0, len(resp.Hits.Hits))
totalMatches := resp.Hits.Total.Value
for i, hit := range resp.Hits.Hits {
for _, hit := range resp.Hits.Hits {
match, err := searchHitToSearchMessageMatch(hit)
if err != nil {
return nil, fmt.Errorf("failed to convert hit %d: %w", i, err)
return nil, fmt.Errorf("failed to convert hit to match: %w", err)
}
if sir.Ref != nil {
@@ -139,7 +139,7 @@ func (e *Engine) Search(ctx context.Context, sir *searchService.SearchIndexReque
}
}
matches[i] = match
matches = append(matches, match)
}
return &searchService.SearchIndexResponse{
@@ -218,6 +218,9 @@ func (e *Engine) Purge(id string) error {
req, err := BuildDocumentDeleteByQueryReq(
opensearchgoAPI.DocumentDeleteByQueryReq{
Indices: []string{e.index},
Params: opensearchgoAPI.DocumentDeleteByQueryParams{
WaitForCompletion: conversions.ToPointer(true),
},
},
NewTermQuery[string]("Path").Value(resource.Path),
)
@@ -268,6 +271,9 @@ func (e *Engine) updateSelfAndDescendants(id string, scriptProvider func(engine.
req, err := BuildUpdateByQueryReq(
opensearchgoAPI.UpdateByQueryReq{
Indices: []string{e.index},
Params: opensearchgoAPI.UpdateByQueryParams{
WaitForCompletion: conversions.ToPointer(true),
},
},
NewTermQuery[string]("Path").Value(resource.Path),
UpdateByQueryReqOptions{

View File

@@ -84,8 +84,8 @@ func applyBuilders(target map[string]any, key string, bs ...Builder) error {
return nil
}
builders := make([]map[string]any, len(bs))
for i, builder := range bs {
builders := make([]map[string]any, 0, len(bs))
for _, builder := range bs {
data, err := builder.Map()
switch {
case err != nil:
@@ -93,7 +93,7 @@ func applyBuilders(target map[string]any, key string, bs ...Builder) error {
case isEmpty(data):
continue
default:
builders[i] = data
builders = append(builders, data)
}
}