3

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!

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and this is not an optimal solution because the queries I got were converted into binary form in logs. So my query in logs looked like: {"from":0,"size":20,"query_binary":"eyAicXVlcnkiOiB7ICJxdWVyeV9zdHJpbmciIDogeyAiZmllbGRzIiA6IFsicG9ydE5hbWUiLCAiY291bnRyeU5hbWUiXSwgInF1ZXJ5IjoiKmUqIn19LCAic29ydCIgOiBbeyAicG9ydElkIiA6IHsgIm9yZGVyIjogIkFTQyIgfX1dfQ=="}. Though when I ran this query, I got the same results but can't be debugged.
2

When using SearchRequest or SearchSourceBuilder, calling .toString() method on their instance will get you actual JSON query:

SearchRequest searchRequest = new SearchRequest("index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

// building the query
// ...

searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);

System.out.println(searchSourceBuilder.toString()); // prints json query
System.out.println(searchRequest.toString()); // prints json query + other information

Comments

1

SearchQuery Interface has a method getQuery() and getFilter() to get the information you need.

        System.out.println(searchQuery.getQuery());
        System.out.println(searchQuery.getFilter());

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.