aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/processing/IntegerIndex2Attribute.java
blob: 37815ef539636f679e9cce30cd2d3618e77d4c1d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.schema.RankProfileRegistry;
import com.yahoo.document.NumericDataType;
import com.yahoo.schema.document.SDField;
import com.yahoo.schema.Index;
import com.yahoo.schema.Schema;
import com.yahoo.vespa.indexinglanguage.ExpressionConverter;
import com.yahoo.vespa.indexinglanguage.ExpressionVisitor;
import com.yahoo.vespa.indexinglanguage.expressions.AttributeExpression;
import com.yahoo.vespa.indexinglanguage.expressions.Expression;
import com.yahoo.vespa.indexinglanguage.expressions.IndexExpression;
import com.yahoo.vespa.indexinglanguage.expressions.ScriptExpression;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.util.HashSet;
import java.util.Set;

/**
 * Replaces the 'index' statement of all numerical fields to 'attribute' because we no longer support numerical indexes.
 *
 * @author baldersheim
 */
public class IntegerIndex2Attribute extends Processor {

    public IntegerIndex2Attribute(Schema schema, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(schema, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process(boolean validate, boolean documentsOnly) {
        for (SDField field : schema.allConcreteFields()) {
            if (field.doesIndexing() && field.getDataType().getPrimitiveType() instanceof NumericDataType) {
                if (field.getIndex(field.getName()) != null
                    && ! (field.getIndex(field.getName()).getType().equals(Index.Type.VESPA))) continue;
                ScriptExpression script = field.getIndexingScript();
                Set<String> attributeNames = new HashSet<>();
                new MyVisitor(attributeNames).visit(script);
                field.setIndexingScript(schema.getName(), (ScriptExpression)new MyConverter(attributeNames).convert(script));
                warn(schema, field, "Changed to attribute because numerical indexes (field has type " +
                                    field.getDataType().getName() + ") is not currently supported." +
                                    " Index-only settings may fail. Ignore this warning for streaming search.");
            }
        }
    }

    private static class MyVisitor extends ExpressionVisitor {

        final Set<String> attributeNames;

        public MyVisitor(Set<String> attributeNames) {
            this.attributeNames = attributeNames;
        }

        @Override
        protected void doVisit(Expression exp) {
            if (exp instanceof AttributeExpression) {
                attributeNames.add(((AttributeExpression)exp).getFieldName());
            }
        }
    }

    private static class MyConverter extends ExpressionConverter {

        final Set<String> attributeNames;

        public MyConverter(Set<String> attributeNames) {
            this.attributeNames = attributeNames;
        }

        @Override
        protected boolean shouldConvert(Expression exp) {
            return exp instanceof IndexExpression;
        }

        @Override
        protected Expression doConvert(Expression exp) {
            String indexName = ((IndexExpression)exp).getFieldName();
            if (attributeNames.contains(indexName)) {
                return null;
            }
            return new AttributeExpression(indexName);
        }
    }

}