aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/searcher/ValidateSortingSearcher.java
blob: 96bf40195e9ab5dc1d51dfe83e33e8ec64f52844 (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
// 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.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.config.ClusterConfig;
import com.yahoo.search.query.Sorting;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.PhaseNames;
import com.yahoo.vespa.config.search.AttributesConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yahoo.prelude.querytransform.NormalizingSearcher.ACCENT_REMOVAL;

/**
 * Check sorting specification makes sense to the search cluster before
 * passing it on to the backend.
 *
 * @author Steinar Knutsen
 */
@Before(PhaseNames.BACKEND)
@After(ACCENT_REMOVAL)
public class ValidateSortingSearcher extends Searcher {

    private Map<String, AttributesConfig.Attribute> attributeNames = null;
    private String clusterName = "";
    private final boolean enabled;

    public ValidateSortingSearcher(ClusterConfig clusterConfig, AttributesConfig attributesConfig) {
        initAttributeNames(attributesConfig);
        setClusterName(clusterConfig.clusterName());
        enabled = clusterConfig.indexMode() != ClusterConfig.IndexMode.Enum.STREAMING;
    }

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    private Map<String, AttributesConfig.Attribute> getAttributeNames() {
        return attributeNames;
    }

    public void setAttributeNames(Map<String, AttributesConfig.Attribute> attributeNames) {
        this.attributeNames = attributeNames;
    }

    public void initAttributeNames(AttributesConfig config) {
        HashMap<String, AttributesConfig.Attribute> attributes = new HashMap<>(config.attribute().size());

        for (AttributesConfig.Attribute attr : config.attribute()) {
            attributes.put(attr.name(), attr);
        }
        setAttributeNames(attributes);
    }

    @Override
    public Result search(Query query, Execution execution) {
        ErrorMessage e = validate(query);
        if (enabled && e != null) {
            Result r = new Result(query);
            r.hits().addError(e);
            return r;
        }
        return execution.search(query);
    }

    private static Sorting.UcaSorter.Strength config2Strength(AttributesConfig.Attribute.Sortstrength.Enum s) {
        if (s == AttributesConfig.Attribute.Sortstrength.PRIMARY) {
            return Sorting.UcaSorter.Strength.PRIMARY;
        } else if (s == AttributesConfig.Attribute.Sortstrength.SECONDARY) {
            return Sorting.UcaSorter.Strength.SECONDARY;
        } else if (s == AttributesConfig.Attribute.Sortstrength.TERTIARY) {
            return Sorting.UcaSorter.Strength.TERTIARY;
        } else if (s == AttributesConfig.Attribute.Sortstrength.QUATERNARY) {
            return Sorting.UcaSorter.Strength.QUATERNARY;
        } else if (s == AttributesConfig.Attribute.Sortstrength.IDENTICAL) {
            return Sorting.UcaSorter.Strength.IDENTICAL;
        }
        return Sorting.UcaSorter.Strength.PRIMARY;
    }

    private ErrorMessage validate(Query query) {
        Sorting sorting = query.getRanking().getSorting();
        List<Sorting.FieldOrder> l = (sorting != null) ? sorting.fieldOrders() : null;

        if (l == null) {
            return null;
        }
        Map<String, AttributesConfig.Attribute> names = getAttributeNames();
        if (names == null) {
            return null;
        }

        String queryLocale = null;
        if (query.getModel().getLocale() != null) {
            queryLocale = query.getModel().getLocale().toString();
        }

        for (Sorting.FieldOrder f : l) {
            String name = f.getFieldName();
            if ("[rank]".equals(name) || "[docid]".equals(name)) {
                // built-in constants
            } else if ("[relevance]".equals(name) || "[relevancy]".equals(name)) { // aliases
                f.getSorter().setName("[rank]");
            } else if (names.containsKey(name)) {
                AttributesConfig.Attribute attrConfig = names.get(name);
                if (attrConfig == null)
                    return ErrorMessage.createInvalidQueryParameter("Cluster '" + getClusterName() +
                                                                    "' has no attribute config for field '" + name + "'");
                if (f.getSortOrder() == Sorting.Order.UNDEFINED) {
                    f.setAscending(attrConfig.sortascending());
                }
                if (f.getSorter().getClass().equals(Sorting.AttributeSorter.class)) {
                    // This indicates that it shall use default.
                    if ((attrConfig.datatype() == AttributesConfig.Attribute.Datatype.STRING)) {
                        if (attrConfig.sortfunction() == AttributesConfig.Attribute.Sortfunction.UCA) {
                            String locale = attrConfig.sortlocale();
                            if (locale == null || locale.isEmpty()) {
                                locale = queryLocale;
                            }
                            // can only use UcaSorter if we have knowledge about wanted locale
                            if (locale != null) {
                                f.setSorter(new Sorting.UcaSorter(name, locale, Sorting.UcaSorter.Strength.UNDEFINED));
                            } else {
                                // wanted UCA but no locale known, so use lowercase as fallback
                                f.setSorter(new Sorting.LowerCaseSorter(name));
                            }
                        } else if (attrConfig.sortfunction() == AttributesConfig.Attribute.Sortfunction.LOWERCASE) {
                            f.setSorter(new Sorting.LowerCaseSorter(name));
                        } else if (attrConfig.sortfunction() == AttributesConfig.Attribute.Sortfunction.RAW) {
                            f.setSorter(new Sorting.RawSorter(name));
                        } else {
                            // default if no config found for this string attribute
                            f.setSorter(new Sorting.LowerCaseSorter(name));
                        }
                    }
                }
                if (f.getSorter() instanceof Sorting.UcaSorter sorter) {
                    String locale = sorter.getLocale();

                    if (locale == null || locale.isEmpty()) {
                        // first fallback
                        locale = attrConfig.sortlocale();
                    }
                    if (locale == null || locale.isEmpty()) {
                        // second fallback
                        locale = queryLocale;
                    }
                    // final fallback
                    if (locale == null || locale.isEmpty()) {
                        locale = "en_US";
                    }

                    Sorting.UcaSorter.Strength strength = sorter.getStrength();
                    if (sorter.getStrength() == Sorting.UcaSorter.Strength.UNDEFINED) {
                        strength = config2Strength(attrConfig.sortstrength());
                    }
                    if ((sorter.getStrength() == Sorting.UcaSorter.Strength.UNDEFINED) || (sorter.getLocale() == null) || sorter.getLocale().isEmpty()) {
                        sorter.setLocale(locale, strength);
                    }
                }
            } else {
                return ErrorMessage.createInvalidQueryParameter("Cluster '" + getClusterName() +
                                                                "' has no sortable attribute named '" + name + "'");
            }
        }
        return null;
    }

}