aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/yql/FieldFilter.java
blob: 470ce489e2f268da52f45d50438ef24e0ab3575a (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.yql;

import java.util.Iterator;
import java.util.Set;

import com.yahoo.api.annotations.Beta;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;

/**
 * Remove fields which are not explicitly requested, if any field is explicitly
 * requested. Disable using FieldFilter.disable=true in request.
 *
 * @author Steinar Knutsen
 */
@Beta
@After(MinimalQueryInserter.EXTERNAL_YQL)
@Before("com.yahoo.search.yql.FieldFiller")
public class FieldFilter extends Searcher {

    public static final CompoundName FIELD_FILTER_DISABLE = CompoundName.from("FieldFilter.disable");

    /** Fields that should be kept even if not explicitly requested */
    private static final Set<String> syntheticFields = Set.of("matchfeatures", "rankfeatures", "summaryfeatures");

    @Override
    public Result search(Query query, Execution execution) {
        Result result = execution.search(query);
        filter(result);
        return result;
    }

    @Override
    public void fill(Result result, String summaryClass, Execution execution) {
        execution.fill(result, summaryClass);
        filter(result);
    }

    private void filter(Result result) {
        if (result.getQuery().properties().getBoolean(FIELD_FILTER_DISABLE)) return;
        if (result.getQuery().getPresentation().getSummaryFields().isEmpty()) return;

        for (Iterator<Hit> i = result.hits().unorderedDeepIterator(); i.hasNext();) {
            Hit h = i.next();
            if (h.isMeta()) continue;
            for (var fields = h.fieldIterator(); fields.hasNext(); ) {
                var field = fields.next();
                if ( ! result.getQuery().getPresentation().getSummaryFields().contains(field.getKey()) &&
                     ! syntheticFields.contains(field.getKey()))
                    fields.remove();
            }
        }
    }

}