aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/MultipleResultsSearcher.java
blob: 34eb3ab91904244fc5706b59e4eaa0d6b65a54d6 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher;

import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.searchchain.Execution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Groups hits according to document type.
 * For each group, the desired number of hits can be specified.
 *
 * @author Tony Vaagenes
 */
public class MultipleResultsSearcher extends Searcher {

    private final static String propertyPrefix = "multipleresultsets.";
    private static final CompoundName additionalHitsFactorName = CompoundName.from(propertyPrefix + "additionalHitsFactor");
    private static final CompoundName maxTimesRetrieveHeterogeneousHitsName = CompoundName.from(propertyPrefix + "maxTimesRetrieveHeterogeneousHits");
    private static final CompoundName numHits = CompoundName.from(propertyPrefix + "numHits");

    @Override
    public Result search(Query query, Execution e) {
        try {
            Parameters parameters = new Parameters(query);

            query.trace("MultipleResultsSearcher: " + parameters, false, 2);
            HitsRetriever hitsRetriever = new HitsRetriever(query,e,parameters);

            for (DocumentGroup documentGroup : parameters.documentGroups) {
                if (  hitsRetriever.numHits(documentGroup) < documentGroup.targetNumberOfDocuments) {
                    hitsRetriever.retrieveMoreHits(documentGroup);
                }
            }

            return hitsRetriever.createMultipleResultSets();
        } catch(ParameterException exception) {
            Result result = new Result(query);
            result.hits().addError(ErrorMessage.createInvalidQueryParameter(exception.msg));
            return result;
        }
    }

    private static class HitsRetriever {

        PartitionedResult partitionedResult;

        private int numRetrieveMoreHitsCalls = 0;
        private int nextOffset;
        private Query query;
        private final Parameters parameters;
        private final int hits;
        private final int offset;
        private final Execution execution;
        private final Result initialResult;

        HitsRetriever(Query query, Execution execution, Parameters parameters) throws ParameterException {
            this.offset = query.getOffset();
            this.hits = query.getHits();
            this.nextOffset = query.getOffset() + query.getHits();
            this.query = query;
            this.parameters = parameters;
            this.execution = execution;

            initialResult = retrieveHits();
            partitionedResult = new PartitionedResult(parameters.documentGroups, initialResult);

            this.query = query;
        }

        void retrieveMoreHits(DocumentGroup documentGroup) {
            if ( ++numRetrieveMoreHitsCalls <
                 parameters.maxTimesRetrieveHeterogeneousHits) {

                retrieveHeterogenousHits();

                if (numHits(documentGroup) <
                    documentGroup.targetNumberOfDocuments) {

                    retrieveMoreHits(documentGroup);
                }

            } else {
                retrieveRemainingHitsForGroup(documentGroup);
            }
        }

        void retrieveHeterogenousHits() {
            int numHitsToRetrieve = (int)(hits * parameters.additionalHitsFactor);

            final int maxNumHitsToRetrieve = 1000;
            numHitsToRetrieve = Math.min(numHitsToRetrieve,maxNumHitsToRetrieve);

            try {
                query.setWindow(nextOffset,numHitsToRetrieve);
                partitionedResult.addHits(retrieveHits());
            }
            finally {
                restoreWindow();
                nextOffset += numHitsToRetrieve;
            }
        }

        private void restoreWindow() {
            query.setWindow(offset,hits);
        }

        void retrieveRemainingHitsForGroup(DocumentGroup documentGroup) {
            Set<String> oldRestrictList = query.getModel().getRestrict();
            try {
                int numMissingHits = documentGroup.targetNumberOfDocuments - numHits(documentGroup);
                int offset = numHits(documentGroup);

                query.getModel().getRestrict().clear();
                query.getModel().getRestrict().add(documentGroup.documentName);
                query.setWindow(offset, numMissingHits);
                partitionedResult.addHits(retrieveHits());

            } finally {
                restoreWindow();
                query.getModel().getRestrict().clear();
                query.getModel().getRestrict().addAll(oldRestrictList);
            }
        }

        int numHits(DocumentGroup documentGroup) {
            return partitionedResult.numHits(documentGroup.documentName);
        }

        Result createMultipleResultSets() {
            Iterator<Hit> i = initialResult.hits().iterator();
            while (i.hasNext()) {
                i.next();
                i.remove();
            }

            for (DocumentGroup group: parameters.documentGroups) {
                partitionedResult.cropResultSet(group.documentName,group.targetNumberOfDocuments);
            }

            partitionedResult.insertInto(initialResult.hits());
            return initialResult;
        }

        private Result retrieveHits() {
            Result result = execution.search(query);
            // ensure that field sddocname is available
            execution.fill(result); // TODO: Suffices to fill attributes
            if (initialResult != null) {
                initialResult.hits().addErrorsFrom(result.hits());
            }
            return result;
        }
    }

    // Assumes that field sddocname is available
    private static class PartitionedResult {

        private final Map<String, HitGroup> resultSets = new HashMap<>();

        private final List<Hit> otherHits = new ArrayList<>();

        PartitionedResult(List<DocumentGroup> documentGroups,Result result) throws ParameterException {
            for (DocumentGroup group : documentGroups)
                addGroup(group);

            addHits(result, true);
        }

        void addHits(Result result, boolean addOtherHits) {
            for (Hit hit : result.hits()) {
                add(hit, addOtherHits);
            }
        }

        void addHits(Result result) {
            addHits(result, false);
        }


        void add(Hit hit, boolean addOtherHits) {
            String documentName = (String)hit.getField(Hit.SDDOCNAME_FIELD);

            if (documentName != null) {
                HitGroup resultSet = resultSets.get(documentName);

                if (resultSet != null) {
                    resultSet.add(hit);
                    return;
                }
            }

            if (addOtherHits) {
                otherHits.add(hit);
            }
        }

        int numHits(String documentName) {
            return resultSets.get(documentName).size();
        }

        void insertInto(HitGroup group) {
            for (Hit hit: otherHits) {
                group.add(hit);
            }

            for (HitGroup hit: resultSets.values() ) {
                hit.copyOrdering(group);
                group.add(hit);
            }
        }

        void cropResultSet(String documentName, int numDocuments) {
            resultSets.get(documentName).trim(0, numDocuments);
        }

        private void addGroup(DocumentGroup group) throws ParameterException {
            final String documentName = group.documentName;
            if ( resultSets.put(group.documentName,
                    new HitGroup(documentName) {
                        /**
                         *
                         */
                        private static final long serialVersionUID = 5732822886080288688L;
                    })
                 != null ) {

                throw new ParameterException("Document name " + group.documentName + "mentioned multiple times");
            }
        }

    }


    //examples:
    //multipleresultsets.numhits=music:10,movies:20
    //multipleresultsets.additionalhitsFactor=0.8
    //multipleresultsets.maxtimesretrieveheterogeneoushits=2
    private static class Parameters {
        Parameters(Query query)
            throws ParameterException {

            readNumHitsSpecification(query);
            readMaxTimesRetrieveHeterogeneousHits(query);
            readAdditionalHitsFactor(query);
        }


        List<DocumentGroup> documentGroups = new ArrayList<>();
        double additionalHitsFactor = 0.8;
        int maxTimesRetrieveHeterogeneousHits = 2;

        private void readAdditionalHitsFactor(Query query)
            throws ParameterException {

            String additionalHitsFactorStr = query.properties().getString(additionalHitsFactorName);

            if (additionalHitsFactorStr == null)
                return;

            try {
                additionalHitsFactor =
                    Double.parseDouble(additionalHitsFactorStr);
            } catch (NumberFormatException e) {
                throw new ParameterException(
                    "Expected floating point number, got '" +
                    additionalHitsFactorStr + "'.");
            }
        }

        private void readMaxTimesRetrieveHeterogeneousHits(Query query) {
            maxTimesRetrieveHeterogeneousHits = query.properties().getInteger(
                maxTimesRetrieveHeterogeneousHitsName,
                maxTimesRetrieveHeterogeneousHits);
        }


        private void readNumHitsSpecification(Query query)
            throws ParameterException {

            //example numHitsSpecification: "music:10,movies:20"
            String numHitsSpecification =
                query.properties().getString(numHits);

            if (numHitsSpecification == null)
                return;

            String[] numHitsForDocumentNames = numHitsSpecification.split(",");

            for (String s:numHitsForDocumentNames) {
                handleDocumentNameWithNumberOfHits(s);
            }

        }

        public String toString() {
            String s = "additionalHitsFactor=" + additionalHitsFactor +
                ", maxTimesRetrieveHeterogeneousHits="
                + maxTimesRetrieveHeterogeneousHits +
                ", numHitsSpecification='";

            for (DocumentGroup group : documentGroups) {
                s += group.documentName + ":" +
                    group.targetNumberOfDocuments + ", ";
            }

            s += "'";

            return s;
        }

        //example input: music:10
        private void handleDocumentNameWithNumberOfHits(String s)
            throws ParameterException {

            String[] documentNameWithNumberOfHits = s.split(":");

            if (documentNameWithNumberOfHits.length != 2) {
                String msg = "Expected a single ':' in '" + s + "'.";

                if (documentNameWithNumberOfHits.length > 2)
                    msg += " Please check for missing commas.";

                throw new ParameterException(msg);
            } else {
                String documentName =
                    documentNameWithNumberOfHits[0].trim();
                try {
                    int numHits = Integer.parseInt(
                        documentNameWithNumberOfHits[1].trim());

                    numRequestedHits(documentName, numHits);
                } catch (NumberFormatException e) {
                    throw new ParameterException(
                        "Excpected an integer but got '" +
                        documentNameWithNumberOfHits[1] + "'");
                }
            }
        }

        private void numRequestedHits(String documentName, int numHits) {
            documentGroups.add(new DocumentGroup(documentName, numHits));
        }

    }

    private static class DocumentGroup {
        String documentName;
        int targetNumberOfDocuments;

        DocumentGroup(String documentName, int targetNumberOfDocuments) {
            this.documentName = documentName;
            this.targetNumberOfDocuments = targetNumberOfDocuments;
        }
    }

    private static class ParameterException extends Exception {

        String msg;

        ParameterException(String msg) {
            this.msg = msg;
        }

    }

}