Sort in Java API client

Hi Experts,

I am using new java API client to connect with Elasticsearch. My requirement is to search documents in sorted order of given parameters.

I have written the below code to search and sort by "customer".

        SortOptions sort = new SortOptions.Builder().field(f -> f.field("customer").order(SortOrder.Asc)).build();
        List<SortOptions> list = new ArrayList<SortOptions>();
        list.add(sort);

        SearchResponse<Order> response = elasticsearchClient.search(b -> b.index("order").size(100).sort(list)
                .query(q -> q.bool(bq -> bq
                        .filter(fb -> fb.range(r -> r.field("orderTime").
                                        gte(JsonData.of(timeStamp("01-01-2022-01-01-01")))
                                        .lte(JsonData.of(timeStamp("01-01-2022-01-01-10")))
                                )
                        )
                        .must(query)
                )), Order.class);

I am getting below Exception.'

Exception in thread "main" co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:281)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1487)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1504)
	at model.OrderDAO.fetchRecordsQuery(OrderDAO.java:128)
	at model.OrderDAO.main(OrderDAO.java:45)

Same code work if I remove .sort() method.
Please let me know what is wrong with the parameter being passed in .sort() method and help me with the correct syntax for sorting.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.