source: SRUAggregator/trunk/src/main/java/eu/clarin/sru/fcs/aggregator/search/Search.java @ 6043

Last change on this file since 6043 was 6043, checked in by emanuel.dima@uni-tuebingen.de, 9 years ago
  1. alpha 21: downloading as text, csv, excel and tcf works
File size: 5.5 KB
Line 
1package eu.clarin.sru.fcs.aggregator.search;
2
3import eu.clarin.sru.client.SRUVersion;
4import java.util.List;
5import eu.clarin.sru.client.SRUClientException;
6import eu.clarin.sru.client.SRUSearchRetrieveRequest;
7import eu.clarin.sru.client.SRUSearchRetrieveResponse;
8import eu.clarin.sru.client.fcs.ClarinFCSRecordData;
9import eu.clarin.sru.fcs.aggregator.client.ThrottledClient;
10import eu.clarin.sru.fcs.aggregator.scan.Corpus;
11import eu.clarin.sru.fcs.aggregator.scan.Diagnostic;
12import eu.clarin.sru.fcs.aggregator.scan.FCSProtocolVersion;
13import eu.clarin.sru.fcs.aggregator.scan.Statistics;
14import eu.clarin.sru.fcs.aggregator.util.SRUCQL;
15import java.util.ArrayList;
16import java.util.Collections;
17import java.util.Random;
18import java.util.concurrent.atomic.AtomicLong;
19import org.slf4j.LoggerFactory;
20
21/**
22 * Class representing a search operation
23 *
24 * @author Yana Panchenko
25 * @author edima
26 */
27public 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, final Corpus corpus, String searchString, int startRecord, int maxRecords) {
57                final Request request = new Request(corpus, searchString, startRecord, startRecord + maxRecords - 1);
58                log.info("Executing search in '{}' query='{}' maxRecords='{}'", corpus, searchString, maxRecords);
59
60                SRUSearchRetrieveRequest searchRequest = new SRUSearchRetrieveRequest(corpus.getEndpoint().getUrl());
61                searchRequest.setVersion(version);
62                searchRequest.setMaximumRecords(maxRecords);
63                boolean legacy = corpus.getEndpoint().getProtocol().equals(FCSProtocolVersion.LEGACY);
64                searchRequest.setRecordSchema(legacy
65                                ? ClarinFCSRecordData.LEGACY_RECORD_SCHEMA
66                                : ClarinFCSRecordData.RECORD_SCHEMA);
67                searchRequest.setQuery(searchString);
68                searchRequest.setStartRecord(startRecord);
69                if (corpus.getHandle() != null) {
70                        searchRequest.setExtraRequestData(legacy
71                                        ? SRUCQL.SEARCH_CORPUS_HANDLE_LEGACY_PARAMETER
72                                        : SRUCQL.SEARCH_CORPUS_HANDLE_PARAMETER,
73                                        corpus.getHandle());
74                }
75                requests.add(request);
76
77                String url = null;
78                try {
79                        url = searchRequest.makeURI(SRUVersion.VERSION_1_2).toString();
80                } catch (SRUClientException ex) {
81                }
82                final String fullRequestUrl = url;
83
84                try {
85                        searchClient.searchRetrieve(searchRequest, new ThrottledClient.SearchCallback() {
86                                @Override
87                                public void onSuccess(SRUSearchRetrieveResponse response, ThrottledClient.Stats stats) {
88                                        try {
89                                                statistics.addEndpointDatapoint(corpus.getInstitution(), corpus.getEndpoint(), stats.getQueueTime(), stats.getExecutionTime());
90                                                Result result = new Result(request, response, null);
91                                                results.add(result);
92                                                requests.remove(request);
93                                                List<Diagnostic> diagnostics = result.getDiagnostics();
94                                                if (diagnostics != null && !diagnostics.isEmpty()) {
95                                                        log.error("diagnostic for url: " + response.getRequest().makeURI(SRUVersion.VERSION_1_2));
96                                                        for (Diagnostic diagnostic : diagnostics) {
97                                                                statistics.addEndpointDiagnostic(corpus.getInstitution(), corpus.getEndpoint(), diagnostic, fullRequestUrl);
98                                                        }
99                                                }
100                                        } catch (Throwable xc) {
101                                                log.error("search.onSuccess exception:", xc);
102                                        }
103                                }
104
105                                @Override
106                                public void onError(SRUSearchRetrieveRequest srureq, SRUClientException xc, ThrottledClient.Stats stats) {
107                                        try {
108                                                statistics.addEndpointDatapoint(corpus.getInstitution(), corpus.getEndpoint(), stats.getQueueTime(), stats.getExecutionTime());
109                                                statistics.addErrorDatapoint(corpus.getInstitution(), corpus.getEndpoint(), xc, fullRequestUrl);
110                                                results.add(new Result(request, null, xc));
111                                                requests.remove(request);
112                                                log.error("search.onError: ", xc);
113                                        } catch (Throwable xxc) {
114                                                log.error("search.onError exception:", xxc);
115                                        }
116                                }
117                        });
118                } catch (Throwable xc) {
119                        log.error("SearchRetrieve error for " + corpus.getEndpoint().getUrl(), xc);
120                }
121                return request;
122        }
123
124        public Long getId() {
125                return id;
126        }
127
128        public List<Request> getRequests() {
129                List<Request> copy = new ArrayList<>();
130                synchronized (requests) {
131                        copy.addAll(requests);
132                }
133                return copy;
134        }
135
136        public List<Result> getResults() {
137                List<Result> copy = new ArrayList<>();
138                synchronized (results) {
139                        copy.addAll(results);
140                }
141                return copy;
142        }
143
144        public Statistics getStatistics() {
145                return statistics;
146        }
147
148        public void shutdown() {
149                // nothing to do
150        }
151
152        public long getCreatedAt() {
153                return createdAt;
154        }
155
156        public String getQuery() {
157                return query;
158        }
159
160        public String getSearchLanguage() {
161                return searchLanguage;
162        }
163
164}
Note: See TracBrowser for help on using the repository browser.