Help in migrating java client code 7x to 8x

I have written java code in Spring boot using elasticsearch-rest-high-level-client 7.17.4
now I want to migrate to elastic-search 8
how I can write term query

       SearchRequest searchRequest = new SearchRequest(ZEEBE_JOB);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if (taskkey != null) {
            BoolQueryBuilder filter = boolQueryBuilder.filter(QueryBuilders.termQuery("key", taskkey));

            log.info("task query: {}", filter);
            //      boolQueryBuilder.filter(QueryBuilders.termQuery("intent", "CREATED"));
        }
        if (processInstanceKey != null) {
            BoolQueryBuilder filter = boolQueryBuilder.filter(QueryBuilders.termQuery("value.processInstanceKey", processInstanceKey));
            log.info("process instance: {}", filter);
        }
        if (assignee != null) {
            BoolQueryBuilder filter = boolQueryBuilder.filter(QueryBuilders.termQuery("value.customHeaders.io.camunda.zeebe:assignee", assignee));
            log.info("assignee: {}", filter);
        }

        if (start != null && end != null) {
            long startMillis = start.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
            log.info("startMillis  : {}", startMillis);

            long endMillis = end.atStartOfDay().plusDays(1).minusSeconds(1).toInstant(ZoneOffset.UTC).toEpochMilli();
            log.info("endMillis  : {}", endMillis);

            BoolQueryBuilder filter = boolQueryBuilder.filter(QueryBuilders.rangeQuery("timestamp").gte(start).lte(end).format("yyyy-MM-dd"));
            log.info("time filter : {}", filter);
        }

        if (intent != null) {
            BoolQueryBuilder filter = boolQueryBuilder.filter(QueryBuilders.termQuery("intent", intent));
            log.info("intent : {}", filter);
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.from(from);

        searchRequest.source(searchSourceBuilder);
        List<TaskDto> taskList = new ArrayList<>();

        SearchResponse searchResponse = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHit[] hits = searchResponse.getHits().getHits();
        long totalRecords = searchResponse.getHits().getTotalHits().value;

        searchSourceBuilder.sort("timestamp", SortOrder.DESC);

        int recordFrom = searchSourceBuilder.from();
        int size1 = searchSourceBuilder.size();


Hello and welcome!
This is how you start writing a term query, like this example in the documentation, using the 8.X elasticsearch java client:

esClient.search(s -> s
    .index("index_name")
        .query(q -> q
            .term(t -> t
                .field("user.id")
                .value("kimchy")
                .boost(1.0F)))
, YourUserClass.class);

Thanks @ltrotta ,
It is helpful.
Is it possible to add multiple queries into one request, or do I have to write them multiple times?

Yes you can combine/concatenate queries, but it would output just one result, so if you're looking for specific values like in the examples you sent I would stick with separate queries.

1 Like