Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining string query (JSON) from SearchQuery object

For debugging purposes, I need to know what query spring-data-elasticsearch is sending to the ElasticSearch cluster. I have tried to call the toString method on the SearchQuery object, and doesn't return what I need.

What I am doing in Java (using spring-data-elasticsearch) is:

private FilterBuilder getFilterBuilder(String id) {
    return orFilter(
        termFilter("yaddayaddayadda.id", id),
        termFilter("blahblahblah.id", id)
    );
}


SearchQuery sq = NativeSearchQueryBuilder()
    .withQuery(new MatchAllQuery())
    .withFilter(fb)
    .build();

And I expect to return something like this plain query executed in ES cluster REST API is returning:

{
    "query": {
        "filtered": {
            "filter": {
                "or": [
                    {
                        "term": {
                            "yaddayaddayadda.id": "9"
                        }
                    },
                    {
                        "term": {
                            "blahblahblah.id": "9"
                        }
                    }
                ]
            }
        }
    }
}

Thanks in advance!

like image 300
jlgarhdez Avatar asked Aug 08 '26 16:08

jlgarhdez


1 Answers

One way to achieve this is to log the queries on the ES/server-side into the slowlog file. Open your elasticsearch.yml config file and towards the bottom uncomment/edit the two lines below:

...
index.search.slowlog.threshold.query.info: 1ms
...
index.search.slowlog.threshold.fetch.info: 1ms
...

The advantage of this solution is that whatever client technology you're using to query your ES server (Spring Data, Ruby, Browser, Javascript, etc), you'll be able to dump and debug your queries in a single location.

like image 175
Val Avatar answered Aug 10 '26 15:08

Val