-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctIdFetcher.java
More file actions
53 lines (44 loc) · 1.85 KB
/
Copy pathDistinctIdFetcher.java
File metadata and controls
53 lines (44 loc) · 1.85 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
44
45
46
47
48
49
50
51
52
53
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.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DistinctIdFetcher {
private final SearchAsyncClient searchClient;
public DistinctIdFetcher(String endpoint, String indexName, String apiKey) {
this.searchClient = new SearchClientBuilder()
.credential(new AzureKeyCredential(apiKey))
.endpoint(endpoint)
.indexName(indexName)
.buildAsyncClient();
}
public Mono<Set<String>> fetchAllDistinctIds() {
SearchOptions options = new SearchOptions()
.setFacets("id,count:1000") // facet on id array field
.setTop(0); // don’t fetch docs, only facets
return searchClient.search("*", options)
.byPage()
.next()
.map(page -> {
Set<String> distinctIds = new HashSet<>();
Map<String, List<FacetResult>> facets = page.getFacets();
if (facets != null && facets.containsKey("id")) {
facets.get("id").forEach(facet ->
distinctIds.add(facet.getValue().toString()));
}
return distinctIds;
});
}
public static void main(String[] args) {
DistinctIdFetcher fetcher =
new DistinctIdFetcher("<YOUR-ENDPOINT>", "<YOUR-INDEX-NAME>", "<YOUR-API-KEY>");
fetcher.fetchAllDistinctIds().subscribe(ids -> {
System.out.println("Distinct IDs:");
ids.forEach(System.out::println);
});
}
}