dongxiequ3724 2018-05-01 11:46
浏览 260

在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
  • 写回答

1条回答 默认 最新

  • duancheng6221 2018-05-02 06:50
    关注

    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)
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?