-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueAccountDetailsService.java
More file actions
43 lines (36 loc) · 1.59 KB
/
Copy pathUniqueAccountDetailsService.java
File metadata and controls
43 lines (36 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import com.azure.core.credential.AzureKeyCredential;
import com.azure.search.documents.SearchAsyncClient;
import com.azure.search.documents.SearchClientBuilder;
import com.azure.search.documents.models.*;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;
public class UniqueAccountDetailsService {
private final SearchAsyncClient searchClient;
public UniqueAccountDetailsService(String endpoint, String indexName, String apiKey) {
this.searchClient = new SearchClientBuilder()
.credential(new AzureKeyCredential(apiKey))
.endpoint(endpoint)
.indexName(indexName)
.buildAsyncClient();
}
public Mono<Map<String, Integer>> fetchUniqueAccountDetailsCountById() {
SearchOptions options = new SearchOptions()
.setAggregations("id,countdistinct(accountDetails)")
.setTop(0);
return searchClient.search("*", options)
.byPage()
.next()
.map(page -> {
Map<String, Integer> result = new HashMap<>();
if (page.getAggregations() != null && page.getAggregations().containsKey("id")) {
page.getAggregations().get("id").forEach(r -> {
String id = r.getValue().toString();
Integer count = r.getCount(); // distinct accountDetails count
result.put(id, count);
});
}
return result;
});
}
}