Git Product home page Git Product logo

Comments (6)

zoom2madan avatar zoom2madan commented on July 21, 2024

You can try the logger approach suggested in this stackoverflow post (second answer)
https://stackoverflow.com/questions/60849887/log-json-queries-built-through-elasticsearch-high-level-java-client-for-debuggin

I have tried this approach with below version of ES client, and it works.

co.elastic.clients
elasticsearch-java
7.16.1

from elasticsearch-java.

X-rapido avatar X-rapido commented on July 21, 2024

Part of my code

<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>7.16.2</version>
</dependency>

search test

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.tingfeng.vo.IndexProductVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class EsTest {

    @Autowired
    private ElasticsearchClient client;

    @Test
    public void search() throws Exception {
        SearchRequest searchRequest = SearchRequest.of(s -> s
                .index("demo")
                .query(q -> q
                        .term(t -> t
                                .field("goodsId")
                                .value(v -> v.stringValue("1"))
                        )));

        System.out.println("--- DSL query---:");    // How do I print DSL here??

        SearchResponse<IndexProductVo> search = client.search(searchRequest, IndexProductVo.class);
        for (Hit<IndexProductVo> hit : search.hits().hits()) {
            System.out.println(hit.source()); // Have the results
        }
    }
}

"elasticsearch-java.jar" How do I print DSL here?

from elasticsearch-java.

Dyqer avatar Dyqer commented on July 21, 2024

@X-rapido

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JsonGenerator generator = transport.jsonpMapper().jsonProvider().createGenerator(baos);
        searchRequest.serialize(generator, transport.jsonpMapper());
        generator.close();
        System.out.println(baos);

from elasticsearch-java.

X-rapido avatar X-rapido commented on July 21, 2024

@X-rapido

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JsonGenerator generator = transport.jsonpMapper().jsonProvider().createGenerator(baos);
        searchRequest.serialize(generator, transport.jsonpMapper());
        generator.close();
        System.out.println(baos);

Thank's

from elasticsearch-java.

hezhiliang avatar hezhiliang commented on July 21, 2024

hi,

before used "elasticsearch-rest-high-level-client.jar", in SearchSourceBuilder.java can print the DSL query

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQuery);
...
logger.info("=====DSL====="+searchSourceBuilder.toString());

I‘m trying "elasticsearch-java.jar" , please tell me how to print the DSL SearchRequest query?

SearchRequest searchRequest = SearchRequest.of(s -> s
                .index(indexName)
                .query(q -> q
                        .term(t -> t
                                .field("goodsId")
                                .value(v -> v.stringValue("1"))
                        )));

i',m debugging. no found the record。 please help!

The toString() method is overwritten in the "elasticsearch-rest-high-level-client" version, but not in the "elasticsearch-java (8.2.2)" version. As a result, it is not easy to print DSL statements in the development test. I see that this issue is now being optimized and look forward to a new release that addresses this issue.

from elasticsearch-java.

vincentchen007 avatar vincentchen007 commented on July 21, 2024

hello @swallez
I also had this issue on "elasticsearch-java (8.1)" version.
could you please help? thank you

from elasticsearch-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.