Skip to content

Commit

Permalink
Added: add a count for an opensearch query - without the need to get … (
Browse files Browse the repository at this point in the history
#130)

## What
Added a count only func which does not return any body - only the
affected documents
  • Loading branch information
larox11 authored Nov 26, 2024
2 parents 6be358e + 2c06c6d commit 5a7bf10
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
40 changes: 38 additions & 2 deletions pkg/openSearch/openSearchClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package openSearchClient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -73,6 +75,36 @@ func (c *Client) Search(indexName string, requestBody []byte) (responseBody []by
return result, nil
}

func (c *Client) Count(indexName string, requestBody []byte) (count int64, err error) {
log.Debug().Msgf("count requestBody: %s", string(requestBody))
request := CountReq{
Indices: []string{indexName},
Body: bytes.NewReader(requestBody),
}
countRequest, err := request.GetRequest()
if err != nil {
return 0, err
}
response, err := c.openSearchProjectClient.Client.Perform(countRequest)
if err != nil {
return 0, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
responseBody, _ := io.ReadAll(response.Body)
log.Warn().Msgf("count response - statusCode:'%d' body:'%s'", response.StatusCode, responseBody)
}

var countResp CountResp
if err := json.NewDecoder(response.Body).Decode(&countResp); err != nil {
log.Error().Msgf("error decoding count response: %v", err)
return 0, err
}

return countResp.Count, nil
}

func (c *Client) SearchStream(indexName string, requestBody []byte, scrollTimeout time.Duration, ctx context.Context) (io.Reader, error) {
reader, writer := io.Pipe()
startSignal := make(chan error, 1)
Expand All @@ -98,7 +130,9 @@ func (c *Client) SearchStream(indexName string, requestBody []byte, scrollTimeou
if searchResponse.Errors || searchResponse.Inspect().Response.IsError() {
writer.Close()
startSignal <- fmt.Errorf("search failed")
log.Error().Msgf("search response: %s: %s", searchResponse.Inspect().Response.Status(), searchResponse.Inspect().Response.String())
log.Error().Msgf("search response: %s: %s",
searchResponse.Inspect().Response.Status(),
searchResponse.Inspect().Response.String())
return
}

Expand Down Expand Up @@ -141,7 +175,9 @@ func (c *Client) SearchStream(indexName string, requestBody []byte, scrollTimeou

if scrollResult.Inspect().Response.IsError() {
writer.CloseWithError(fmt.Errorf("scroll-result error"))
log.Error().Msgf("search response: %s: %s", scrollResult.Inspect().Response.Status(), scrollResult.Inspect().Response.String())
log.Error().Msgf("search response: %s: %s",
scrollResult.Inspect().Response.Status(),
scrollResult.Inspect().Response.String())
return
}

Expand Down
55 changes: 55 additions & 0 deletions pkg/openSearch/openSearchClient/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package openSearchClient

import (
"fmt"
"io"
"net/http"
"strings"

"github.com/opensearch-project/opensearch-go/v4"
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
)

type CountReq struct {
Indices []string
Body io.Reader
Header http.Header
Params map[string]string
}

// GetRequest returns the *http.Request that gets executed by the client
func (r CountReq) GetRequest() (*http.Request, error) {
if r.Params == nil {
r.Params = make(map[string]string)
}
var path string
if len(r.Indices) > 0 {
path = fmt.Sprintf("/%s/_search", strings.Join(r.Indices, ","))
} else {
path = "/_search"
}

return opensearch.BuildRequest(
"GET",
path,
r.Body,
r.Params,
r.Header,
)
}

type CountResp struct {
Count int64 `json:"count"`
Shards ShardStats `json:"_shards"`
}

type ShardStats struct {
Total int `json:"total"`
Successful int `json:"successful"`
Skipped int `json:"skipped"`
Failed int `json:"failed"`
}

func (r CountResp) Inspect() opensearchapi.Inspect {
return opensearchapi.Inspect{}
}

0 comments on commit 5a7bf10

Please sign in to comment.