1 | package eu.clarin.sru.fcs.aggregator.search; |
---|
2 | |
---|
3 | import eu.clarin.sru.client.SRUVersion; |
---|
4 | import java.util.List; |
---|
5 | import eu.clarin.sru.client.SRUClientException; |
---|
6 | import eu.clarin.sru.client.SRUSearchRetrieveRequest; |
---|
7 | import eu.clarin.sru.client.SRUSearchRetrieveResponse; |
---|
8 | import eu.clarin.sru.client.fcs.ClarinFCSRecordData; |
---|
9 | import eu.clarin.sru.fcs.aggregator.client.ThrottledClient; |
---|
10 | import eu.clarin.sru.fcs.aggregator.scan.Corpus; |
---|
11 | import eu.clarin.sru.fcs.aggregator.scan.Diagnostic; |
---|
12 | import eu.clarin.sru.fcs.aggregator.scan.FCSProtocolVersion; |
---|
13 | import eu.clarin.sru.fcs.aggregator.scan.Statistics; |
---|
14 | import eu.clarin.sru.fcs.aggregator.util.SRUCQL; |
---|
15 | import java.util.ArrayList; |
---|
16 | import java.util.Collections; |
---|
17 | import java.util.Random; |
---|
18 | import java.util.concurrent.atomic.AtomicLong; |
---|
19 | import org.slf4j.LoggerFactory; |
---|
20 | |
---|
21 | /** |
---|
22 | * Class representing a search operation |
---|
23 | * |
---|
24 | * @author Yana Panchenko |
---|
25 | * @author edima |
---|
26 | */ |
---|
27 | public class Search { |
---|
28 | |
---|
29 | private static final org.slf4j.Logger log = LoggerFactory.getLogger(Search.class); |
---|
30 | |
---|
31 | private static final String SEARCH_RESULTS_ENCODING = "UTF-8"; |
---|
32 | |
---|
33 | private static final AtomicLong counter = new AtomicLong(Math.abs(new Random().nextInt())); |
---|
34 | |
---|
35 | private final Long id; |
---|
36 | private final String query; |
---|
37 | private final long createdAt = System.currentTimeMillis(); |
---|
38 | private final String searchLanguage; |
---|
39 | private final List<Request> requests = Collections.synchronizedList(new ArrayList<Request>()); |
---|
40 | private final List<Result> results = Collections.synchronizedList(new ArrayList<Result>()); |
---|
41 | private final Statistics statistics; |
---|
42 | |
---|
43 | public Search(ThrottledClient searchClient, SRUVersion version, |
---|
44 | Statistics statistics, List<Corpus> corpora, String searchString, |
---|
45 | String searchLanguage, int startRecord, int maxRecords |
---|
46 | ) { |
---|
47 | this.id = counter.getAndIncrement(); |
---|
48 | this.query = searchString; |
---|
49 | this.searchLanguage = searchLanguage; |
---|
50 | this.statistics = statistics; |
---|
51 | for (Corpus corpus : corpora) { |
---|
52 | executeSearch(searchClient, version, corpus, searchString, startRecord, maxRecords); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | private Request executeSearch(ThrottledClient searchClient, SRUVersion version, |
---|
57 | final Corpus corpus, String searchString, |
---|
58 | int startRecord, int maxRecords) { |
---|
59 | final Request request = new Request(corpus, searchString, startRecord, startRecord + maxRecords - 1); |
---|
60 | log.info("Executing search in '{}' query='{}' maxRecords='{}'", corpus, searchString, maxRecords); |
---|
61 | |
---|
62 | SRUSearchRetrieveRequest searchRequest = new SRUSearchRetrieveRequest(corpus.getEndpoint().getUrl()); |
---|
63 | searchRequest.setVersion(version); |
---|
64 | searchRequest.setMaximumRecords(maxRecords); |
---|
65 | boolean legacy = corpus.getEndpoint().getProtocol().equals(FCSProtocolVersion.LEGACY); |
---|
66 | searchRequest.setRecordSchema(legacy |
---|
67 | ? ClarinFCSRecordData.LEGACY_RECORD_SCHEMA |
---|
68 | : ClarinFCSRecordData.RECORD_SCHEMA); |
---|
69 | searchRequest.setQuery(searchString); |
---|
70 | searchRequest.setStartRecord(startRecord); |
---|
71 | if (corpus.getHandle() != null) { |
---|
72 | searchRequest.setExtraRequestData(legacy |
---|
73 | ? SRUCQL.SEARCH_CORPUS_HANDLE_LEGACY_PARAMETER |
---|
74 | : SRUCQL.SEARCH_CORPUS_HANDLE_PARAMETER, |
---|
75 | corpus.getHandle()); |
---|
76 | } |
---|
77 | requests.add(request); |
---|
78 | |
---|
79 | try { |
---|
80 | searchClient.searchRetrieve(searchRequest, new ThrottledClient.SearchCallback() { |
---|
81 | @Override |
---|
82 | public void onSuccess(SRUSearchRetrieveResponse response, ThrottledClient.Stats stats) { |
---|
83 | try { |
---|
84 | statistics.addEndpointDatapoint(corpus.getInstitution(), corpus.getEndpoint(), stats.getQueueTime(), stats.getExecutionTime()); |
---|
85 | Result result = new Result(request, response, null); |
---|
86 | results.add(result); |
---|
87 | requests.remove(request); |
---|
88 | List<Diagnostic> diagnostics = result.getDiagnostics(); |
---|
89 | if (diagnostics != null && !diagnostics.isEmpty()) { |
---|
90 | log.error("diagnostic for url: " + response.getRequest().getRequestedURI().toString()); |
---|
91 | for (Diagnostic diagnostic : diagnostics) { |
---|
92 | statistics.addEndpointDiagnostic(corpus.getInstitution(), corpus.getEndpoint(), |
---|
93 | diagnostic, response.getRequest().getRequestedURI().toString()); |
---|
94 | } |
---|
95 | } |
---|
96 | } catch (Throwable xc) { |
---|
97 | log.error("search.onSuccess exception:", xc); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public void onError(SRUSearchRetrieveRequest srureq, SRUClientException xc, ThrottledClient.Stats stats) { |
---|
103 | try { |
---|
104 | statistics.addEndpointDatapoint(corpus.getInstitution(), corpus.getEndpoint(), stats.getQueueTime(), stats.getExecutionTime()); |
---|
105 | statistics.addErrorDatapoint(corpus.getInstitution(), corpus.getEndpoint(), xc, srureq.getRequestedURI().toString()); |
---|
106 | results.add(new Result(request, null, xc)); |
---|
107 | requests.remove(request); |
---|
108 | log.error("search.onError: ", xc); |
---|
109 | } catch (Throwable xxc) { |
---|
110 | log.error("search.onError exception:", xxc); |
---|
111 | } |
---|
112 | } |
---|
113 | }); |
---|
114 | } catch (Throwable xc) { |
---|
115 | log.error("SearchRetrieve error for " + corpus.getEndpoint().getUrl(), xc); |
---|
116 | } |
---|
117 | return request; |
---|
118 | } |
---|
119 | |
---|
120 | public Long getId() { |
---|
121 | return id; |
---|
122 | } |
---|
123 | |
---|
124 | public List<Request> getRequests() { |
---|
125 | List<Request> copy = new ArrayList<>(); |
---|
126 | synchronized (requests) { |
---|
127 | copy.addAll(requests); |
---|
128 | } |
---|
129 | return copy; |
---|
130 | } |
---|
131 | |
---|
132 | public List<Result> getResults(String corpusId) { |
---|
133 | List<Result> copy = new ArrayList<>(); |
---|
134 | synchronized (results) { |
---|
135 | if (corpusId == null || corpusId.isEmpty()) { |
---|
136 | copy.addAll(results); |
---|
137 | } else { |
---|
138 | for (Result r : results) { |
---|
139 | if (corpusId.equals(r.getCorpus().getId())) { |
---|
140 | copy.add(r); |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | return copy; |
---|
146 | } |
---|
147 | |
---|
148 | public Statistics getStatistics() { |
---|
149 | return statistics; |
---|
150 | } |
---|
151 | |
---|
152 | public void shutdown() { |
---|
153 | // nothing to do |
---|
154 | } |
---|
155 | |
---|
156 | public long getCreatedAt() { |
---|
157 | return createdAt; |
---|
158 | } |
---|
159 | |
---|
160 | public String getQuery() { |
---|
161 | return query; |
---|
162 | } |
---|
163 | |
---|
164 | public String getSearchLanguage() { |
---|
165 | return searchLanguage; |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|