Home  >  Article  >  Backend Development  >  How to debug/view generated queries when using olivere/elastic elasticsearch go library?

How to debug/view generated queries when using olivere/elastic elasticsearch go library?

WBOY
WBOYforward
2024-02-15 09:09:081143browse

使用 olivere/elastic elasticsearch go 库时如何调试/查看生成的查询?

Debugging and viewing the generated queries is a very important step when using the olivere/elastic elasticsearch go library. During development, we often need to ensure that the queries we build are correct and return the results we expect. PHP editor Xinyi will introduce you to some methods to debug and view the generated queries to ensure that your code is working properly. Whether in a development or production environment, these tips will help you better understand and debug your code.

Question content

I'm trying to find out what the query generated by the https://github.com/olivere/elastic library is like the actual json value query sent to the elasticsearch server.

There is some documentation on tracing logs (the one I use is shown below), but this doesn't seem to include queries.

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

I also can't seem to find anything relevant in the documentation here: https://pkg.go.dev/github.com/olivere/elastic?utm_source=godoc

Workaround

According to the documentation, you can provide your own http client:

// Get the client. You can also provide your own http client here.
Client, err := elastic.newclient(elastic.seterrorlog(errorlog))

Okay, that's the end of the documentation :)...actually you have to provide the doer interface.
I instantiated a struct implementing the doer interface and decorated http.do() to log the http.request dump:

Disclaimer: For the scope of this question, this is just a minimal example of what I'm using against an elastic instance running in a docker container. In production, don't run insecure tls, don't hardcode credentials, configure http transport as needed, etc.

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", "<password>"),
        elastic.seturl("https://<address>: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())
}

This is the output:

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

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

I assumed it would also be possible to use settracelog, but I chose a known path.

The above is the detailed content of How to debug/view generated queries when using olivere/elastic elasticsearch go library?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete