TP-55555 | change elastic query to builder queries

This commit is contained in:
varnit-goyal_navi
2024-09-09 07:54:53 +05:30
parent 549a0f4af9
commit 38b4cc9374
5 changed files with 377 additions and 31 deletions

View File

@@ -6,10 +6,9 @@ import (
"cybertron/configs"
"encoding/json"
elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"log"
"net/http"
"strings"
)
type ElasticSearchClient struct {
@@ -45,12 +44,9 @@ func (el *ElasticSearchClient) IndexDocument(document interface{}) {
}
}
func (el *ElasticSearchClient) SearchDocuments(query *types.Query, fields []string) ([]map[string]interface{}, error) {
func (el *ElasticSearchClient) SearchDocuments(searchRequest string, fields []string) ([]map[string]interface{}, map[string]interface{}, error) {
res, err := el.client.Search().
Index(el.Config.Index).
Request(&search.Request{
Query: query,
}).Source_(fields).
Index(el.Config.Index).Raw(strings.NewReader(searchRequest)).
Do(context.TODO())
if err != nil {
log.Println("Error getting response: %s", err)
@@ -67,8 +63,16 @@ func (el *ElasticSearchClient) SearchDocuments(query *types.Query, fields []stri
}
results = append(results, doc)
}
aggregations := make(map[string]interface{})
if res.Aggregations != nil {
for aggName, agg := range res.Aggregations {
aggregations[aggName] = agg
}
}
return results, nil
log.Printf("%s", aggregations)
return results, aggregations, nil
}
func (el *ElasticSearchClient) GetDocument(documentID string) (interface{}, error) {