aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/main/java/ai/vespa/client/dsl/UserInput.java
blob: 66f7da5d0592b84e18d3271efb63efecd5532279 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.client.dsl;

import java.util.UUID;

public class UserInput extends Query {

    private final Annotation annotation; // accept only defaultIndex annotation
    private final String value;
    private final boolean valueIsReference;
    private final String indexField;
    private boolean setDefaultIndex;

    UserInput(Sources sources, String value) {
        this(sources, A.empty(), value);
    }

    UserInput(Sources sources, Annotation annotation, String value) {
        super(sources);
        this.annotation = annotation;
        this.value = value;
        this.valueIsReference = value.startsWith("@");
        this.nonEmpty = true;

        if (annotation.contains("defaultIndex")) {
            setDefaultIndex = true;
            indexField = (String) annotation.get("defaultIndex");
        } else {
            indexField = UUID.randomUUID().toString().substring(0, 5);
        }
    }

    UserInput(String value) {
        this(A.empty(), value);
    }

    UserInput(Annotation annotation, String value) {
        this(null, annotation, value);
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        if (setDefaultIndex)
            b.append("(").append(annotation);
        b.append("userInput(");
        if ( ! valueIsReference)
            b.append("\"");
        b.append(value);
        if ( ! valueIsReference)
            b.append("\"");
        b.append(")");
        if (setDefaultIndex)
            b.append(")");
        return b.toString();
    }

    @Override
    public boolean hasPositiveSearchField(String fieldName) {
        if (super.hasPositiveSearchField(fieldName)) return true;
        return !"andnot".equals(this.op) && this.indexField.equals(fieldName);
    }

    @Override
    public boolean hasPositiveSearchField(String fieldName, Object value) {
        if (super.hasPositiveSearchField(fieldName, value)) return true;
        return hasPositiveSearchField(fieldName) && this.value.equals(value);
    }

    @Override
    public boolean hasNegativeSearchField(String fieldName) {
        if (super.hasNegativeSearchField(fieldName)) return true;
        return "andnot".equals(this.op) && this.indexField.equals(fieldName);
    }

    @Override
    public boolean hasNegativeSearchField(String fieldName, Object value) {
        if (super.hasNegativeSearchField(fieldName, value)) return true;
        return hasNegativeSearchField(fieldName) && this.value.equals(value);
    }

}