blob: 45ed1b87746650214b6233c1ef95e52cd0b7e24b (
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
68
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.dispatch.searchcluster.Node;
import com.yahoo.search.result.Coverage;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import java.io.IOException;
import java.util.Optional;
/**
* SearchInvoker encapsulates an allocated connection for running a single search query.
* The invocation object can be stateful and should not be reused.
*
* @author ollivir
*/
public abstract class SearchInvoker extends CloseableInvoker {
private final Optional<Node> node;
private ResponseMonitor<SearchInvoker> monitor;
protected SearchInvoker(Optional<Node> node) {
this.node = node;
}
/**
* Retrieve the hits for the given {@link Query}. If the search is run on multiple content
* nodes, the provided {@link Execution} may be used to retrieve document summaries required
* for correct result windowing.
*/
public Result search(Query query, Execution execution) throws IOException {
sendSearchRequest(query);
InvokerResult result = getSearchResult(execution);
setFinalStatus(result.getResult().hits().getError() == null);
result.complete();
return result.getResult();
}
protected abstract void sendSearchRequest(Query query) throws IOException;
protected abstract InvokerResult getSearchResult(Execution execution) throws IOException;
protected void setMonitor(ResponseMonitor<SearchInvoker> monitor) {
this.monitor = monitor;
}
protected void responseAvailable() {
if (monitor != null) {
monitor.responseAvailable(this);
}
}
protected Optional<Integer> distributionKey() {
return node.map(Node::key);
}
protected InvokerResult errorResult(Query query, ErrorMessage errorMessage) {
Result error = new Result(query, errorMessage);
Coverage errorCoverage = new Coverage(0, 0, 0);
errorCoverage.setNodesTried(1);
error.setCoverage(errorCoverage);
return new InvokerResult(error);
}
}
|