Merge pull request #67 from mattn/fix-elasticsearch-count

This commit is contained in:
fiatjaf_
2023-06-05 11:50:16 -03:00
committed by GitHub
2 changed files with 53 additions and 1 deletions

View File

@@ -14,8 +14,9 @@ services:
condition: service_healthy
ports:
- 2700:2700
- 7447:7447
volumes:
- ./nostres:/bin/relay
- ./nostres:/bin
command: "/bin/relay"
elasticsearch:

View File

@@ -29,6 +29,10 @@ type EsSearchResult struct {
}
}
type EsCountResult struct {
Count int64
}
func buildDsl(filter *nostr.Filter) ([]byte, error) {
dsl := esquery.Bool()
@@ -217,3 +221,50 @@ func toInterfaceSlice(slice interface{}) []interface{} {
return ret
}
func (ess *ElasticsearchStorage) CountEvents(ctx context.Context, filter *nostr.Filter) (int64, error) {
if filter == nil {
return 0, errors.New("filter cannot be null")
}
count := int64(0)
// optimization: get by id
if isGetByID(filter) {
if evts, err := ess.getByID(filter); err == nil {
count += int64(len(evts))
} else {
return 0, fmt.Errorf("error getting by id: %w", err)
}
}
dsl, err := buildDsl(filter)
if err != nil {
return 0, err
}
es := ess.es
res, err := es.Count(
es.Count.WithContext(ctx),
es.Count.WithIndex(ess.IndexName),
es.Count.WithBody(bytes.NewReader(dsl)),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
if res.IsError() {
txt, _ := io.ReadAll(res.Body)
fmt.Println("oh no", string(txt))
return 0, fmt.Errorf("%s", txt)
}
var r EsCountResult
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return 0, err
}
return r.Count + count, nil
}