opensearch SearchRequest query logging

60 Views Asked by At

I am using "opensearch-java:2.8.1". In the Elasticsearch java client, I was able to logging my query in the following way.

    SearchRequest searchRequest = searchRequestBuilder.build();
    log.info("query: {}", searchRequest.toString());

However, in the opensearch java client, the implementation of toString is different.

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Is there any way I can log a query?

1

There are 1 best solutions below

0
Kartoos On

The toString() method you are referring to, comes from Object.java itself which is parent of all the classes in java. Since, SearchRequest is actually not overriding it, hence the the implementation is being taken from Object.java.

The implementation difference between toString() comes from the fact that it is implemented in elasticsearch-java but not implemented in opensearch-java.

Now coming to the issue of logging information from SearchRequest, you will need to use some other methods which can give you your desired info. For example I see

searchRequest.q() # Query in the Lucene query string syntax

Or as per this stackoverflow answer, if you want to print query:

searchRequest.source().toString()