登录
首页 >  Golang >  Go问答

如何在使用olivere/elastic的Elasticsearch Go库时进行查询调试和查看

来源:stackoverflow

时间:2024-02-05 21:57:25 154浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《如何在使用olivere/elastic的Elasticsearch Go库时进行查询调试和查看》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

我试图找出 https://github.com/olivere/elastic 库生成的查询是什么,就像发送到 elasticsearch 服务器的实际 json 值查询一样。

有一些关于跟踪日志的文档(我使用的如下所示),但这似乎不包括查询。

client, err := elastic.NewClient(
...
elastic.SetTraceLog(log.New(os.Stdout,"",0)),
)

我似乎也无法在此处的文档中找到任何相关内容:https://pkg.go.dev/github.com/olivere/elastic?utm_source=godoc


正确答案


根据文档,你可以提供自己的http客户端:

// 获取客户端。您还可以在此处提供您自己的 http 客户端。
客户端,err := elastic.newclient(elastic.seterrorlog(errorlog))

好吧,文档到此结束:)...实际上您必须提供 doer 接口。
我实例化了一个实现 doer 接口的结构,并装饰了 http.do() 以记录 http.request 转储:

免责声明: 对于这个问题的范围,这只是我针对在 docker 容器中运行的弹性实例使用的一个最小示例。在生产中,不要运行不安全的 tls,不要对凭据进行硬编码,根据需要配置 http 传输等。

package main

import (
    "context"
    "crypto/tls"
    "fmt"
    "net/http"
    "net/http/httputil"

    "github.com/olivere/elastic/v7"
)

type logginghttpelasticclient struct {
    c http.client
}

func (l logginghttpelasticclient) do(r *http.request) (*http.response, error) {
    // log the http request dump
    requestdump, err := httputil.dumprequest(r, true)
    if err != nil {
        fmt.println(err)
    }
    fmt.println("reqdump: " + string(requestdump))
    return l.c.do(r)
}

func main() {
    doer := logginghttpelasticclient{
        c: http.client{
            // load a trusted ca here, if running in production
            transport: &http.transport{
                tlsclientconfig: &tls.config{insecureskipverify: true},
            },
        },
    }

    client, err := elastic.newclient(
        // provide the logging doer here
        elastic.sethttpclient(doer),

        elastic.setbasicauth("elastic", ""),
        elastic.seturl("https://
:9200"), elastic.setsniff(false), // this is specific to my docker elastic runtime ) if err != nil { panic(err) } /* generate a random http request to check if it's logged */ ac := client.alias() ac.add("myindex", "myalias").do(context.background()) }

这是输出:

reqDump: POST /_aliases HTTP/1.1
Host: 127.0.0.1:9200
Accept: application/json
Authorization: Basic base64(:)
Content-Type: application/json
User-Agent: elastic/7.0.32 (linux-amd64)

{"actions":[{"add":{"alias":"myAlias","index":"myIndex"}}]}

我假设也可以使用 settracelog,但我选择了已知路径。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何在使用olivere/elastic的Elasticsearch Go库时进行查询调试和查看》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>