在Golang中使用Elasticsearch脚本字段

I'm trying to use a scriptfield to calculate the subtotal (hours * price) in the DocumentResponse struct. It works fine without the scriptfield.

It's all based on this example https://outcrawl.com/go-elastic-search-service/

Don't be surprised if it's a very simple mistake I'm completely new to coding.

My guess is that something goes wrong when unmarshaling the response.

These are the structs in question:

type DocumentResponse struct {
    ID              string          `json:"id"`
    CreatedAt       time.Time       `json:"created_at"`
    Project         string          `json:"project"`
    Hours           float64         `json:"hours"`
    Date            string          `json:"date"`
    Price           float64         `json:"price"`
    Vat             float64         `json:"vat"`
    Subtotal        float64         `json:"subtotal"`
}                            

type SearchResponse struct {
        Time      string             `json:"time"`
        Hits      string             `json:"hits"`
        Documents []DocumentResponse `json:"documents"`
}

This is where I try to search and return the struct with the scriptfield included:

    boolQ := elastic.NewMatchAllQuery()

    subtotal := elastic.NewScriptField("doc['subtotal'].value", elastic.NewScript("doc['price'].value * doc['hours'].value"))
    src := elastic.NewSearchSource().Query(boolQ).ScriptFields(subtotal)

    result, err := elasticClient.Search().
            Index(elasticIndexName).
            SearchSource(src).
            From(skip).Size(take).
            Do(c.Request.Context())
    if err != nil {  
            log.Println(err)
            errorResponse(c, http.StatusInternalServerError, "Something went wrong")
            return   
    }
    res := SearchResponse{
            Time: fmt.Sprintf("%d", result.TookInMillis),
            Hits: fmt.Sprintf("%d", result.Hits.TotalHits),
    }
    // Transform search results before returning them
    docs := make([]DocumentResponse, 0)
    for _, hit := range result.Hits.Hits {
            var doc DocumentResponse
            json.Unmarshal(*hit.Source, &doc)
            docs = append(docs, doc)
    }
    res.Documents = docs
    c.JSON(http.StatusOK, res)
}

Running something like this:

curl 'localhost:8080/search?start=2018-04-01&end=2018-04-30&project=bogus'

Does not return anything and generates an error:

2018/05/01 13:20:42 [Recovery] panic recovered:
GET /search?start=2018-04-01&end=2018-04-30&project=bogus HTTP/1.1
Host: localhost:8080
Accept: */*
User-Agent: curl/7.59.0
runtime error: invalid memory address or nil pointer dereference
/usr/lib/go/src/runtime/panic.go:502 (0x42ac98)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), 
uint32(d.siz), uint32(d.siz))
/usr/lib/go/src/runtime/panic.go:63 (0x429d0d)
panicmem: panic(memoryError)
/usr/lib/go/src/runtime/signal_unix.go:388 (0x43fa49)
sigpanic: panicmem()
/home/niklas/go/src/git.enokinetwork.com/niklas/goest/main.go:168 
(0x9bf56a)
searchEndpoint: json.Unmarshal(*hit.Source, &doc)
/home/niklas/go/src/github.com/gin-gonic/gin/context.go:110 (0x8ad492)
(*Context).Next: c.handlers[c.index](c)
/home/niklas/go/src/github.com/gin-gonic/gin/recovery.go:46 (0x8bd369)
RecoveryWithWriter.func1: c.Next()
/home/niklas/go/src/github.com/gin-gonic/gin/context.go:110 (0x8ad492)
(*Context).Next: c.handlers[c.index](c)
/home/niklas/go/src/github.com/gin-gonic/gin/logger.go:83 (0x8bc69b)
LoggerWithWriter.func1: c.Next()
/home/niklas/go/src/github.com/gin-gonic/gin/context.go:110 (0x8ad492)
(*Context).Next: c.handlers[c.index](c)
/home/niklas/go/src/github.com/gin-gonic/gin/gin.go:351 (0x8b4854)
(*Engine).handleHTTPRequest: c.Next()
/home/niklas/go/src/github.com/gin-gonic/gin/gin.go:318 (0x8b4092)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/lib/go/src/net/http/server.go:2694 (0x67fc9b)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go/src/net/http/server.go:1830 (0x67be50)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go/src/runtime/asm_amd64.s:2361 (0x456e70)
goexit: BYTE    $0x90   // NOP
duanjing7459
duanjing7459 第168行是:json.Unmarshal(*hit.Source,&doc)
大约 2 年之前 回复
douzhang1364
douzhang1364 什么是main.go:168行?
大约 2 年之前 回复
dpjpo746884
dpjpo746884 您能否在请求中使用脚本字段直接从elasticsearch服务器获取elasticsearch文档?(也许使用curl直接打到Elasticsearch服务器上)Elasticsearch往往对动态脚本不满意。
大约 2 年之前 回复

1个回答

Before doing unmarshal do nil check on hit.Source as I can see you are dereferencing hit.Source, try with this and let me know if it resolves the issue.

for _, hit := range result.Hits.Hits {
        var doc DocumentResponse
        if hit.Source !=nil{
            err:=json.Unmarshal(*hit.Source, &doc)
            if err!=nil{
                fmt.Printf("error unmarshal the doc ,error %+v",err)
                continue
            }
            docs = append(docs, doc)
        }
    }
dongmo9996
dongmo9996 您能否发布弹性搜索响应结构,hit.Source的类型是什么?
大约 2 年之前 回复
dongzongzi0379
dongzongzi0379 我试过了,它摆脱了错误。 现在,响应如下所示:{“ time”:“ 8”,“ hits”:“ 2”,“ documents”:[]}我假设documents字段为空,因为如果无法解组? 我猜我的结构是错的? 问题是我不确定响应的样子。
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐