github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/es/reader/esGetSingleDocHandler.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package reader 18 19 import ( 20 "net/url" 21 "strings" 22 "time" 23 24 "github.com/nqd/flat" 25 "github.com/siglens/siglens/pkg/es/query" 26 rutils "github.com/siglens/siglens/pkg/readerUtils" 27 "github.com/siglens/siglens/pkg/segment" 28 "github.com/siglens/siglens/pkg/segment/structs" 29 . "github.com/siglens/siglens/pkg/segment/structs" 30 "github.com/siglens/siglens/pkg/utils" 31 vtable "github.com/siglens/siglens/pkg/virtualtable" 32 log "github.com/sirupsen/logrus" 33 "github.com/valyala/fasthttp" 34 ) 35 36 func ProcessSingleDocGetRequest(ctx *fasthttp.RequestCtx, myid uint64) { 37 38 var response = utils.NewSingleESResponse() 39 40 queryStart := time.Now() 41 42 idInUrl := utils.ExtractParamAsString(ctx.UserValue("idVal")) 43 44 idVal, err := url.QueryUnescape(idInUrl) 45 if err != nil { 46 log.Errorf("ProcessSingleDocGetRequest: could not decode idVal=%v, err=%v", idInUrl, err) 47 ctx.SetStatusCode(fasthttp.StatusBadRequest) 48 return 49 } 50 51 indexNameIn := utils.ExtractParamAsString(ctx.UserValue("indexName")) 52 docTypeVal := utils.ExtractParamAsString(ctx.UserValue("docType")) 53 54 // todo During search, if search request is on a alias 55 // we only search the first mapped real indexname, however we should search 56 // in multiple indexnames, if the alias was pointing to multiple of them 57 var indexNameConverted string 58 if pres, idxName := vtable.IsAlias(indexNameIn, myid); pres { 59 indexNameConverted = idxName 60 } else { 61 indexNameConverted = indexNameIn 62 } 63 64 qid := rutils.GetNextQid() 65 log.Infof("qid=%d, ProcessSingleDocGetRequest: indexNameIn=[%v], indexNameConverted=[%v], idVal=[%v]", 66 qid, indexNameIn, indexNameConverted, idVal) 67 68 response.Id = idVal 69 response.Index = indexNameIn 70 response.Type = docTypeVal 71 72 var respSrc map[string]interface{} 73 var simpleNode *ASTNode 74 75 isKibanaIndex := strings.Contains(indexNameIn, ".kibana") 76 // TODO: get error from this function 77 simpleNode = query.CreateSingleDocReqASTNode("_id", idVal, isKibanaIndex, qid) 78 79 segment.LogASTNode("esGetSingleDocHandler", simpleNode, qid) 80 sizeLimit := uint64(1) 81 qc := structs.InitQueryContext(indexNameConverted, sizeLimit, 0, myid, true) 82 segment.LogQueryContext(qc, qid) 83 result := segment.ExecuteQuery(simpleNode, &QueryAggregators{}, qid, qc) 84 85 if result == nil { 86 ctx.SetStatusCode(fasthttp.StatusServiceUnavailable) 87 utils.WriteJsonResponse(ctx, response) 88 return 89 } 90 91 ctx.SetStatusCode(fasthttp.StatusOK) 92 ctx.Response.Header.Set("Content-Type", "application/json") 93 queryResult := query.GetQueryResponseJson(result, indexNameConverted, queryStart, sizeLimit, qid, &QueryAggregators{}) 94 95 if queryResult.Hits.GetHits() == 0 { 96 utils.WriteJsonResponse(ctx, response) 97 return 98 } 99 // TODO: fix hard coded fields 100 response.Found = true 101 respSrc = queryResult.Hits.Hits[0].Source 102 103 finalSrc, err := flat.Unflatten(respSrc, nil) 104 if err != nil { 105 log.Infof("qid=%d, ProcessSingleDocGetRequest: Failed to unflatten, src=[%v], err=%v", qid, respSrc, err) 106 ctx.SetStatusCode(fasthttp.StatusServiceUnavailable) 107 utils.WriteJsonResponse(ctx, response) 108 return 109 } 110 response.Source = finalSrc 111 utils.WriteJsonResponse(ctx, response) 112 }