blob: 1633196a5853a5a812c7715217a2d249b034745b (
plain) (
blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.debug;
import static com.yahoo.protect.Validator.ensureNotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.yahoo.component.provider.ComponentRegistry;
import org.apache.commons.collections.CollectionUtils;
import com.yahoo.container.Container;
import com.yahoo.jrt.Request;
import com.yahoo.prelude.cluster.ClusterSearcher;
import com.yahoo.search.Searcher;
import com.yahoo.search.handler.SearchHandler;
import com.yahoo.search.searchchain.SearchChainRegistry;
/**
* Utility functions for searchers and search chains.
*
* @author tonytv
*/
final class SearcherUtils {
private static Collection<Searcher> allSearchers() {
SearchChainRegistry searchChainRegistry = getSearchHandler().getSearchChainRegistry();
ComponentRegistry<Searcher> searcherRegistry = searchChainRegistry.getSearcherRegistry();
return searcherRegistry.allComponents();
}
private static Collection<ClusterSearcher> allClusterSearchers() {
return filter(allSearchers(), ClusterSearcher.class);
}
private static <T> Collection<T> filter(Collection<?> collection, Class<T> classToMatch) {
List<T> filtered = new ArrayList<>();
for (Object candidate : collection) {
if (classToMatch.isInstance(candidate))
filtered.add(classToMatch.cast(candidate));
}
return filtered;
}
public static Collection<ClusterSearcher> clusterSearchers(final String clusterName) {
Collection<ClusterSearcher> searchers = allClusterSearchers();
CollectionUtils.filter(searchers,
o -> clusterName.equalsIgnoreCase(((ClusterSearcher)o).getClusterModelName()));
return searchers;
}
//Return value is never null
static SearchHandler getSearchHandler() {
SearchHandler searchHandler = (SearchHandler) Container.get().getRequestHandlerRegistry().getComponent("com.yahoo.search.handler.SearchHandler");
ensureNotNull("The standard search handler is not available.", searchHandler);
return searchHandler;
}
//Retrieve all the cluster searchers as specified by the first parameter of the request.
static Collection<ClusterSearcher> clusterSearchers(Request request) {
String clusterName = request.parameters().get(0).asString();
Collection<ClusterSearcher> searchers = clusterSearchers(clusterName);
if (searchers.isEmpty())
throw new RuntimeException("No cluster named " + clusterName);
return searchers;
}
}
|