aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/documentmodel/SearchField.java
blob: acfdd4f867170be6763415019dea61ad7b4d8edc (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.documentmodel;

import com.yahoo.document.DataType;
import com.yahoo.document.Field;

import java.util.ArrayList;
import java.util.List;

/**
 * @author baldersheim
 */
public class SearchField extends Field {

    /// Indicate if field shall be stored in memory for attribute usage.
    private boolean attribute = false;
    /// Indicate if the field is Vespa indexed.
    private boolean indexed = false;
    /// Indication to backend on how much optimization should be done.

    /**
     * This is a representation of features to generate for this field.
     * It can be both optimize hints, and real functional hints.
     */
    public enum Feature {
        WEIGHT_IN_ATTRIBUTE_POSTINGS("WeightInAttributePosting"),    // Hint to put the weight in postings for attribute.
        WORDPOS_IN_POSTINGS("WordPosInPosting"),                     // Default for generating posocc
        FILTER_ONLY("FilterOnly");                                   // Might only generate bitvector
        private String name;
        Feature(String name) { this.name = name;}
        public String getName() { return name; }
    }
    private List<Feature> featureList = new ArrayList<>();

    public SearchField(Field field, boolean indexed, boolean attribute) {
        this(field, indexed, attribute, null);
    }
    public SearchField(Field field, boolean indexed, boolean attribute, List<Feature> features) {
        super(field.getName(), field);
        this.attribute = attribute;
        this.indexed = indexed;
        if (features != null) {
            featureList.addAll(features);
        }
        validate();
    }

    private void validate() {
        if (attribute || !indexed) {
            return;
        }
        DataType fieldType = getDataType();
        DataType primiType = fieldType.getPrimitiveType();
        if (DataType.STRING.equals(primiType) || DataType.URI.equals(primiType)) {
            return;
        }
        throw new IllegalArgumentException("Expected type " + DataType.STRING.getName() + " for indexed field '" +
                                           getName() + "', got " + fieldType.getName() + ".");
    }

    public SearchField setIndexed() { indexed = true; validate(); return this; }
    public SearchField setAttribute() { attribute = true; validate(); return this; }
    public boolean isAttribute() { return attribute; }

    public boolean   isIndexed() { return indexed; }
    public SearchField addFeature(Feature feature) { featureList.add(feature); validate(); return this; }
}