mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: Implement processing of raw document query results in backend (#63932)
This commit is contained in:
@@ -57,8 +57,14 @@ func parseResponse(responses []*es.SearchResponse, targets []*Query, configuredF
|
||||
|
||||
queryRes := backend.DataResponse{}
|
||||
|
||||
if isDocumentQuery(target) {
|
||||
err := processDocumentResponse(res, target, configuredFields, &queryRes)
|
||||
if isRawDataQuery(target) {
|
||||
err := processRawDataResponse(res, target, configuredFields, &queryRes)
|
||||
if err != nil {
|
||||
return &backend.QueryDataResponse{}, err
|
||||
}
|
||||
result.Responses[target.RefID] = queryRes
|
||||
} else if isRawDocumentQuery(target) {
|
||||
err := processRawDocumentResponse(res, target, &queryRes)
|
||||
if err != nil {
|
||||
return &backend.QueryDataResponse{}, err
|
||||
}
|
||||
@@ -132,7 +138,7 @@ func processLogsResponse(res *es.SearchResponse, target *Query, configuredFields
|
||||
return nil
|
||||
}
|
||||
|
||||
func processDocumentResponse(res *es.SearchResponse, target *Query, configuredFields es.ConfiguredFields, queryRes *backend.DataResponse) error {
|
||||
func processRawDataResponse(res *es.SearchResponse, target *Query, configuredFields es.ConfiguredFields, queryRes *backend.DataResponse) error {
|
||||
propNames := make(map[string]bool)
|
||||
docs := make([]map[string]interface{}, len(res.Hits.Hits))
|
||||
|
||||
@@ -173,6 +179,62 @@ func processDocumentResponse(res *es.SearchResponse, target *Query, configuredFi
|
||||
return nil
|
||||
}
|
||||
|
||||
func processRawDocumentResponse(res *es.SearchResponse, target *Query, queryRes *backend.DataResponse) error {
|
||||
docs := make([]map[string]interface{}, len(res.Hits.Hits))
|
||||
for hitIdx, hit := range res.Hits.Hits {
|
||||
doc := map[string]interface{}{
|
||||
"_id": hit["_id"],
|
||||
"_type": hit["_type"],
|
||||
"_index": hit["_index"],
|
||||
"sort": hit["sort"],
|
||||
"highlight": hit["highlight"],
|
||||
}
|
||||
|
||||
if hit["_source"] != nil {
|
||||
source, ok := hit["_source"].(map[string]interface{})
|
||||
if ok {
|
||||
for k, v := range source {
|
||||
doc[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hit["fields"] != nil {
|
||||
source, ok := hit["fields"].(map[string]interface{})
|
||||
if ok {
|
||||
for k, v := range source {
|
||||
doc[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
docs[hitIdx] = doc
|
||||
}
|
||||
|
||||
fieldVector := make([]*json.RawMessage, len(res.Hits.Hits))
|
||||
for i, doc := range docs {
|
||||
bytes, err := json.Marshal(doc)
|
||||
if err != nil {
|
||||
// We skip docs that can't be marshalled
|
||||
// should not happen
|
||||
continue
|
||||
}
|
||||
value := json.RawMessage(bytes)
|
||||
fieldVector[i] = &value
|
||||
}
|
||||
|
||||
isFilterable := true
|
||||
field := data.NewField(target.RefID, nil, fieldVector)
|
||||
field.Config = &data.FieldConfig{Filterable: &isFilterable}
|
||||
|
||||
frames := data.Frames{}
|
||||
frame := data.NewFrame(target.RefID, field)
|
||||
frames = append(frames, frame)
|
||||
|
||||
queryRes.Frames = frames
|
||||
return nil
|
||||
}
|
||||
|
||||
func processDocsToDataFrameFields(docs []map[string]interface{}, propNames []string, configuredFields es.ConfiguredFields) []*data.Field {
|
||||
size := len(docs)
|
||||
isFilterable := true
|
||||
|
||||
Reference in New Issue
Block a user