Hello,
i'm trying to send a request with a specific from and size parameters , by default there are set to ( from=0 , size=10) .
here's a snippet of my code where i construct my search query
Query query = NativeQuery.builder()
.withQuery(boolQuery._toQuery())
.withAggregation("id", compositeAggregation._toAggregation())
.build();
could anyone guide me on how to specify the 'size' and 'from' parameters in my query using the elasticsearch api ?
i appreciate your help ! thank you
dadoonet
(David Pilato)
January 31, 2024, 9:51am
2
The size is not part of a query but part of a search request.
Something like this:
client.search(sr -> sr.index("index_name").size(10).from(0), Void.class);
thanks for the hint , i tried to implement the solution you provided but i didn't work in my case , i'm using an aggregate.
reactiveElasticsearchOperations.aggregate( query, Void.class, IndexCoordinates.of(appIndex));
dadoonet
(David Pilato)
January 31, 2024, 12:55pm
4
Could you share a bit more of your code? Like a test case?
I'd like to see the classes you are using.
I don't have a test case yet , but here's a snippet of the code :
public Flux<MyService> findServices(LocalDateTime from, LocalDateTime to) {
return reactiveElasticsearchOperations.aggregate(
buildQuery(from, to),
Void.class,
IndexCoordinates.of(appIndex))
.flatMap(getServicesFromAggregation()) ;
}
Query buildQuery(LocalDateTime from, LocalDateTime to) {
RangeQuery rangeQuery = QueryBuilders.range()
.field(@timestamp)
.gte(JsonData.of(from.format(formatter)))
.lte(JsonData.of(to.format(formatter)))
.format("date_optional_time")
.build();
BoolQuery boolQuery = QueryBuilders.bool()
.must(rangeQuery._toQuery())
.mustNot(QueryBuilders.match().field("level").query("DEBUG").build()._toQuery())
.build();
CompositeAggregationSource src = CompositeAggregationSource.of(s -> s
.terms(term -> term
.field("src")
.missingBucket(false)));
CompositeAggregationSource description = CompositeAggregationSource.of(s -> s
.terms(term -> term
.field("description")
.missingBucket(false)));
CompositeAggregation compositeAggregation = CompositeAggregation.of(c -> c.size(100)
.sources(Map.of("id_field", srcCompositeAggregationSource),
Map.of(appDescriptionField, descriptionCompositeAggregationSource)));
return NativeQuery.builder()
.withQuery(boolQuery._toQuery())
.withAggregation("id", compositeAggregation._toAggregation())
.build();
}
dadoonet
(David Pilato)
January 31, 2024, 1:41pm
6
What is this object? reactiveElasticsearchOperations
it's the interface ReactiveElasticsearchOperations .
dadoonet
(David Pilato)
January 31, 2024, 1:53pm
8
It's not an Elasticsearch class right?
It's part of spring data elasticsearch.
dadoonet
(David Pilato)
January 31, 2024, 1:58pm
10
So I guess you need to ask them...
system
(system)
Closed
February 28, 2024, 1:59pm
11
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.