Dorian
(Dorian)
January 5, 2023, 4:45pm
1
Hello,
How can i get all the value that correspond to a specific field ?
For example i want all the existing value for the field "affaireName", check the exemple below :
SearchRequest request = SearchRequest.of(a -> a.index(index)
.size(0)
.aggregations("price-histogram", hist -> hist.histogram(h -> h
.field("affaireName")
.interval(100.0)))
.query(new Query.Builder().bool(boolQuery).build()));
SearchResponse<Void> search = client.search(request, Void.class);
I already try this but it doesn’t work.
dadoonet
(David Pilato)
January 5, 2023, 5:20pm
2
Bonjour
I think you are looking for a terms aggregation .
Though it won't give exactly "all values" unless the number of distinct values is low...
But have a look at it. It will e a good start I think.
Dorian
(Dorian)
January 5, 2023, 7:58pm
3
Merci
I tried to use a terms aggregation and implemented it a couple but i dont understand how to add it to the query.
Can you help me ?
TermsAggregation termsAggregation = new TermsAggregation.Builder().field("affaireName")
.size(100)
.build();
BoolQuery boolQuery = new BoolQuery.Builder()
.filter(new Query.Builder().range(rangeEchelle).build())
.filter(new Query.Builder().range(rangeDate).build())
.must(new Query.Builder().queryString(queryString).build())
.build();
SearchRequest request = SearchRequest.of(a -> a.index(index)
.size(0)
.aggregations( termsAggregation)
.query(new Query.Builder().bool(boolQuery).build()));
SearchResponse<Void> search = client.search(
request,
Void.class);
dadoonet
(David Pilato)
January 6, 2023, 7:33am
4
In the docs, it says you need to use something like:
GET /_search
{
"aggs": {
"affaires": {
"terms": { "field": "affaireName", "size": 100 }
}
}
}
Which translates to Java to something like this (I did not test it):
SearchResponse<Void> response = esClient.search(b -> b
.index("index")
.size(0)
.query(q -> q.match(t -> t
.field("name")
.query(searchText)
)
)
.aggregations("affaires", a -> a
.terms(h -> h
.field("affaireName")
.size(100)
)
),
Void.class
);
Dorian
(Dorian)
January 6, 2023, 12:22pm
5
Great thanks my request is working now, I was wrong about the name of the field.
Now i have this code :
SearchResponse<Void> response = client.search(b -> b
.index(index)
.size(0)
.query(new Query.Builder().bool(boolQuery).build())
.aggregations("affaires", a -> a
.terms(h -> h
.field("affaire")
.size(100))),
Void.class);
System.out.println("Facet affaires");
System.out.println(response.aggregations().get("affaires").sterms().buckets().array());
response.aggregations().get("affaires").sterms().buckets().array().forEach((bucket) -> {
System.out.println(bucket.key() + " : " + bucket.docCount());
});
no matter what field i choose, the bucket array is empty. Do you know where could my problem come from ?
dadoonet
(David Pilato)
January 6, 2023, 6:19pm
6
May be the mapping.
But could you reproduce the problem using the Kibana Console?
system
(system)
Closed
February 3, 2023, 6:20pm
7
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.